Android - getting the current rotation of a matrix
I need to be able to set the rotation of a matrix rather than add to it. I believe the only way to set the rotation is to know the current rotation of the matrix.
Note: matrix.se开发者_运维百科tRotate()
will not do, because the matrix needs to retain its information.
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));
from here http://judepereira.com/blog/calculate-the-real-scale-factor-and-the-angle-of-rotation-from-an-android-matrix/
What you can do is call getValues
and cache the values. Later when you want them back just call setValues
on the matrix.
Update
The rotation matrix and transform matrix relation well explained here
I'm afraid I can't help you with the integration (I've done this in Flash only), but it sounds like you should try to do your matrix calculations yourself, which is best done using "quaternions", which makes adding rotations pretty trivial. See http://introcs.cs.princeton.edu/java/32class/Quaternion.java.html for an example Java implementation. You would want to create a second quaternion matrix that describes your change in rotation and add (in this case, plus
) it to your current matrix.
精彩评论