如何在一个特定的点上使用旋转画布android.graphics.Camera.rotateX(角度) [英] How to rotate a canvas at a specific point using android.graphics.Camera.rotateX(angle)

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

问题描述

我试图用相机(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(帆布油画) - 为了简便起见,我离开了所有的非重要组成部分。

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(角度)调用,尝试使用这些方法的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

任何帮助将是AP preciated,美国商务部的不完全是冗长。

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和岗位转换方法矩阵上的改变类似于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.

缺少位是先做画布翻译,而我也没有使用更大的画布大小来计算的偏移量为pre和后翻译的方法。

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.

下面是修改code。如果它可以帮助其他人出来。

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(角度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆