本文介绍了如何使用 android.graphics.Camera.rotateX(angle) 在特定点旋转画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用相机(android.graphics.Camera 而不是硬件相机)围绕特定点旋转视图画布,在本例中是画布的中间.

I am trying to use the Camera (android.graphics.Camera not the hardware camera) to rotate a views canvas around a specific point, in this instance the middle of the canvas.

在 dispatchDraw(Canvas canvas) 中——为简洁起见,我省略了所有不重要的部分.

In dispatchDraw(Canvas canvas) -- for brevity I am leaving out all the non important parts.

camera.save();
   camera.rotateX(0);
   camera.rotateY(0);
   camera.rotateZ(angle);
   camera.getMatrix(cameraMatrix);
 camera.restore(); 

 canvas.concat( cameraMatrix );

画布会旋转,但总是从左上角旋转.

The canvas rotates, but always from the upper left hand corner.

注意:因为画布的构造比显示区域大,我还需要转换最终结果,使其在显示中居中,我可以使用

NOTE: Because the canvas has been constructed to be larger than the display area I also need to translate the final result so that it is centered in the display, I can do this with either

canvas.translate(xOffset,yOffset) PRIOR to calling the camera methods

cameraMatrix.preTranslate(xOffset,yOffset) AFTER the camera methods

两者都正确地将画布在显示器中居中,但我似乎无法将旋转点设为 camera.rotateZ(angle) 调用的中心,尝试使用 3D android 示例中的方法,但它们似乎适用于 X/Y 轴,它们似乎不会影响 Z 轴

Both correctly center the canvas in the display but I can't seem to get the rotation point to be the center for the camera.rotateZ(angle) call, tried using the methods in the 3D android sample but while they seem to work for the X / Y axis them don't seem to affect the Z axis

任何帮助将不胜感激,文档并不完全冗长.

Any help would be appreciated, the doc's are not exactly verbose.

推荐答案

解决了这个问题,不确定这是否是最好的方法,但它有效.解决方案是

Solved this, not sure if it's the best way but it works. The solution was to

首先平移画布以使较大的画布在显示中居中

Translate the canvas first to center the larger canvas in the display

然后应用相机旋转

然后使用矩阵上的 pre 和 post 转换方法来改变旋转点,类似于 android 示例所做的.

Then to use the pre and post translate methods on the matrix to change the rotation point similar to what the android sample did.

缺少的部分是首先进行画布翻译,而且我也没有使用较大的画布尺寸来计算翻译前和翻译后方法的偏移量.

The missing bits were to do the canvas translation first, and I was also not using the larger canvas size to calculate the offsets for the pre and post translate methods.

这是修改后的代码,如果它可以帮助其他人.

Here is the modified code if it helps anyone else out.

// Center larger canvas in display (was made larger so
// corners will not show when rotated) 
canvas.translate(-translateX, -translateY); 

// Use the camera to rotate a view on any axis
camera.save();
    camera.rotateX(0);
    camera.rotateY(0);
    camera.rotateZ(angle); // Rotate around Z access (similar to canvas.rotate)                                 

    camera.getMatrix(cameraMatrix);

    // This moves the center of the view into the upper left corner (0,0) 
    // which is necessary because Matrix always uses 0,0, as it's transform point 
    cameraMatrix.preTranslate(-centerScaled, -centerScaled);

    // NOTE: Camera Rotations logically happens here when the canvas has the 
    // matrix applied in the canvas.concat method 

    // This happens after the camera rotations are applied, moving the view 
    // back to where it belongs, allowing us to rotate around the center or 
    // any point we choose 
    cameraMatrix.postTranslate(centerScaled, centerScaled);
camera.restore();

canvas.concat(cameraMatrix);

如果有人有更好的方法或看到问题,请发表评论.

If anyone has a better way or sees a problem please leave a comment.

这篇关于如何使用 android.graphics.Camera.rotateX(angle) 在特定点旋转画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 17:18