Android - 获取矩阵的当前旋转

Par*_*unt 3 java android matrix

我需要能够设置矩阵的旋转而不是添加它.我相信设置旋转的唯一方法是知道矩阵的当前旋转.

注意:matrix.setRotate()不会这样做,因为矩阵需要保留其信息.

Эва*_*ist 6

float[] v = new float[9];
matrix.getValues(v);
// translation is simple
float tx = v[Matrix.MTRANS_X];
float ty = v[Matrix.MTRANS_Y];

// calculate real scale
float scalex = v[Matrix.MSCALE_X];
float skewy = v[Matrix.MSKEW_Y];
float rScale = (float) Math.sqrt(scalex * scalex + skewy * skewy);

// calculate the degree of rotation
float rAngle = Math.round(Math.atan2(v[Matrix.MSKEW_X], v[Matrix.MSCALE_X]) * (180 / Math.PI));
Run Code Online (Sandbox Code Playgroud)

从这里 http://judepereira.com/blog/calculate-the-real-scale-factor-and-the-angle-of-rotation-from-an-android-matrix/

  • 我使用float rAngle = Math.round(Math.atan2(v [Matrix.MSKEW_X],v [Matrix.MSCALE_X])*(180/Math.PI))计算旋转; 需要乘以-1才能得到correcct值.为什么会发生? (2认同)

Ron*_*nie 2

您可以做的就是调用getValues并缓存这些值。稍后当你想要他们回来时,只需调用setValues矩阵即可。

更新

这里很好地解释了旋转矩阵和变换矩阵的关系