简体   繁体   English

在特定点旋转和平移

[英]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. 它之所以无法像您目前所拥有的那样起作用,是因为您的setTranslate()调用正在丢弃您之前所做的旋转,而只是将其替换为对单位矩阵执行的转换。 Matrix transformation methods starting with the "set" prefix will just apply the transformation as if nothing happened before them. 以“ set”前缀开头的矩阵转换方法将仅应用转换,就好像之前没有发生任何事情一样。

If you want to read more, this is a useful answer: https://stackoverflow.com/a/8197896/2464728 如果您想了解更多,这是一个有用的答案: 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. 我最初想知道将0用作y旋转点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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