Suppose I initialize an AffineTransform as below:
AffineTransform af = new AffineTransform(2, 3, 4, 5, 6, 7);
How would I create an equivalent Matrix using a
From AffineTransform's javadoc:
[ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ]
[ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ]
[ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]
Not sure, but perhaps
Matrix m = new Matrix();
m.setValues(new float[]{2,3,4,5,6,7,0,0,1});
EDIT: Commenter points out that the order should be
m.setValues(new float[]{2,4,6,3,5,7,0,0,1});
The order provided in AffineTransform is provided as:
java.awt.geom.AffineTransform.AffineTransform(float m00, float m10, float m01, float m11, float m02, float m12)
Constructs a new AffineTransform from 6 floating point values representing the 6 specifiable entries of the 3x3 transformation matrix.
Parameters:
m00 the X coordinate scaling element of the 3x3 matrix
m10 the Y coordinate shearing element of the 3x3 matrix
m01 the X coordinate shearing element of the 3x3 matrix
m11 the Y coordinate scaling element of the 3x3 matrix
m02 the X coordinate translation element of the 3x3 matrix
m12 the Y coordinate translation element of the 3x3 matrix
An example implementation:
new AffineTransform(
q0, q1, q2,
q3, q4, q5);
counter-intuitively yields:
[ m00 m01 m02 ] [ q0 q2 q4 ]
[ m10 m11 m12 ] = [ q1 q3 q5 ]
[ 0 0 1 ] [ 0 0 1 ]
To accomplish the same result with android.graphics.Matrix
:
Matrix m = new Matrix();
m.setValues(new float[] {
q0, q2, q4,
q1, q3, q5,
0, 0, 1
}
I think Matrix's setValues()
method documentation could be improved; it should reflect that the order of its parameters is:
void android.graphics.Matrix.setValues(float[] values)
Copy 9 values from the array into the matrix. Depending on the implementation of Matrix, these may be transformed into 16.16 integers in the Matrix, such that a subsequent call to getValues() will not yield exactly the same values.
The values are provided into the 3x3 matrix in the following order:
float[] { m00, m01, m02, m10, m11, m12, m20, m21, m22 }
Where:
m00 the X coordinate scaling element of the 3x3 matrix (
Matrix.MSCALE_X
)
m01 the X coordinate shearing element of the 3x3 matrix (Matrix.MSKEW_X
)
m02 the X coordinate translation element of the 3x3 matrix (Matrix.MTRANS_X
)
m10 the Y coordinate shearing element of the 3x3 matrix (Matrix.MSKEW_Y
)
m11 the Y coordinate scaling element of the 3x3 matrix (Matrix.MSCALE_Y
)
m12 the Y coordinate translation element of the 3x3 matrix (Matrix.MTRANS_Y
)
m20 the first perspective element of the 3x3 matrix (Matrix.MPERSP_0
)
m21 the second perspective element of the 3x3 matrix (Matrix.MPERSP_1
)
m22 the third perspective element of the 3x3 matrix (Matrix.MPERSP_2
)
This is a demo Java code:
public static float[] createMatrixValues(AffineTransform Tx) {
double[] at = new double[9];
Tx.getMatrix(at);
float[] f = new float[at.length];
f[0] = (float) at[0];
f[1] = (float) at[2];
f[2] = (float) at[4];
f[3] = (float) at[1];
f[4] = (float) at[3];
f[5] = (float) at[5];
f[6] = 0;
f[7] = 0;
f[8] = 1;
return f;
}
and to create the Matrix object:
public static Matrix createMatrixObj(AffineTransform Tx) {
Matrix m = new Matrix();
m.reset();
m.setValues(createMatrix(Tx));
return m;
}
I hope this helper methods solves your question.