Tuesday 12 June 2012

Scale, Flip and Mirror on Sprites

Wow! That was easy.

The following snatch of code takes image im and transforms it to scaledbitmap.
 scale (x and y values) are based on 100 as full size.
flip x and y values are either 1 or -1.
 1,1 means as original bitmap
-1, 1 means mirrored in the y axis
1, -1 means flipped on the x-axis
-1, -1 means mirrored on both axes - i.e. inverted
 
// scale image before return
Matrix matrix = new Matrix();
// scale and flip
matrix.postScale((float)flip.x*scale.x/100, (float)flip.y*scale.y/100);
// create a new bitmap from the original using the matrix to transform the result
Bitmap scaledbitmap = Bitmap.createBitmap(im , 0, 0,
           im.getWidth(), im.getHeight(), matrix, true);
return scaledbitmap;  

No comments: