我们以一个宽高为 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);