Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.setRotate() will not do, because the matrix needs to retain its information.

like image 671
Paramount Avatar asked Sep 03 '11 03:09

Paramount


3 Answers

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/

like image 88
Sergei S Avatar answered Oct 16 '22 07:10

Sergei S


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

like image 21
Ronnie Avatar answered Oct 16 '22 06:10

Ronnie


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.

like image 1
Andrew Avatar answered Oct 16 '22 07:10

Andrew