Reputation: 55
I need to rotate an image at a specific point and then translate it to a specific point on the screen.
The point I want to rotate it at is in the center of the picture.
The traslation works, but the rotation doesn't.
I have a Vector of bitmap and I'm using Canvas and Matrix.
Code:
for (Bitmap image:images)
{
//rotation
double angle=Math.toDegrees(rotation);
Matrix matrix=new Matrix();
matrix.postRotate((float)angle,finalMap.getWidth()/2-1,0);
//transform
matrix.setTranslate(position.x,position.y);
//print on screen
c.drawBitmap(image,matrix, paint);
}
Upvotes: 0
Views: 408
Reputation: 1757
Try changing your rotation/translation calls like this (in exactly this order):
matrix.setTranslate(position.x,position.y);
matrix.preRotate((float)angle,finalMap.getWidth()/2-1,0);
The reason it doesn't work the way you have it currently, is that your setTranslate() call is discarding the rotation that you previously did and just replacing it with a translation performed on an identity matrix. Matrix transformation methods starting with the "set" prefix will just apply the transformation as if nothing happened before them.
If you want to read more, this is a useful answer: https://stackoverflow.com/a/8197896/2464728
Upvotes: 1
Reputation: 595
What does 'the rotation does not' mean? How is it failing to do as you expect? I would initially wonder about the use of 0 for the y rotation point.
Upvotes: 0