How to map java's AffineTransform to android's Matrix?

后端 未结 3 2113
面向向阳花
面向向阳花 2020-12-15 06:41

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

3条回答
  •  萌比男神i
    2020-12-15 07:25

    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)

提交回复
热议问题