矩阵变换android中旋转(角度)和旋转(角度,px,py)之间的差异

时间:2016-08-16 11:47:35

标签: android matrix rotation imageview

根据Android on Matrix的官方文件。

XXRotate(浮动度) - 处理矩阵以约(0,0)旋转指定的度数。

XXRotate(float degrees,float px,float py) - 处理矩阵以指定的度数旋转,枢轴点位于(px,py)。

  • XX根据需要设置,发布或预设。

例如:XXRotate(90,center_point_x_of_view,center_point_y_of_view)

  • 在指定某些支点时,是否需要对枢轴点进行一些翻译?

  • 第一次旋转后视图上原点坐标点的变化是什么?

1 个答案:

答案 0 :(得分:1)

举一个例子,我们取一个宽度和高度为10的正方形。左上角位于(0,0)的原点,右下角位于(10,10)。

如果我们用matrix.setRotate(180F)转换那个方格,我们会期望原点 - 作为枢轴点 - 不会移动,而右下角会移动到(-10,-10)。

现在让我们说我们用matrix.setRotate(180F, 5F, 5F)转换正方形。我们已将枢轴点放在正方形的中心,因此我们期望原点移动到(10,10),右下角移动到(0,0)。

所以看完所有数学后,事实证明

matrix.setRotate(theta, pivotX, pivotY);

实际上只是

的缩短版本
matrix.setRotate(theta);
matrix.preTranslate(-pivotX, -pivotY);
matrix.postTranslate(pivotX, pivotY);

如果你想知道点的变化,对于旋转,X会改变角度的余弦,而y会改变角度的正弦。

如此简单的轮换:

float thetaR = theta * Math.PI / 180F; // convert degrees to radians
int x2 = Math.round(x * (float) Math.cos(thetaR));
int y2 = Math.round(y * (float) Math.sin(thetaR));

把它们放在一起,你有

float thetaR = theta * Math.PI / 180F; // convert degrees to radians
int x2 = Math.round((x - pivotX) * (float) Math.cos(thetaR) + pivotX);
int y2 = Math.round((y - pivotY) * (float) Math.sin(thetaR) + pivotY);