简体   繁体   中英

Rotation and translation at a specific point

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);
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM