I've got a strange problem with Bitmap
rotation.
That's my code:
in onDraw
method
int cx_gear_big = (getWidth() - gear_big.getWidth()) >> 1;
int cy_gears_big = (getHeight() - gear_big.getHeight()) >> 1;
canvas.drawBitmap(rotateGear(gear_big, bigGearAngle), cx_gear_big,cy_gears_big, null);
That's my method which returns rotated Bitmap
:
public static Bitmap rotateGear(Bitmap source, int angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
I have few gears in my canvas
, but it doesn't really matter.
My problem is:
When I'm rotating my Bitmap
(a gear) it moves down and left (it depends on the angle of rotation) then it comes back...
Where can my problem be in this case?
UPD:
Translation of Bitmap
is not linear. It's like bouncing.
So ideal way to solve this problem is translate matrix before rotation and translate it back. Code example:
Matrix m = new Matrix();
float px = getWidth()/2;
float py = getHeight()/2;
m.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2);
m.postRotate(angle);
m.postTranslate(px, py);
canvas.drawBitmap(bitmap, m, null);