Introduction to the Matrix class in Android
Matrix, as its name implies, is a Matrix in linear algebra. The basic knowledge about matrices and addition, subtraction, multiplication, division operations are not described here.
The Matrix class in Android is a simple 3x3 Level 3 Matrix with the following structure:
- Float matrix = {MSCALE_X, MSKEW_X, MTRANS_X, MSKEW_Y, MSCALE_Y, MTRANS_Y,
- MPERSP_0, MPERSP_1, MPERSP_2 };
The structure is as follows: MSCALE_X and MSCALE_Y control the zooming of the X axis and Y axis respectively, and MSKEW_X and MSKEW_Y control the linear skew coefficients of the X and Y coordinates respectively, MTRANS_X and MTRANS_Y control linear translation in the X and Y directions. MPERSP_0, MPERSP_1, and MPERSP_2 are not used much at the moment.With the appeal description, the so-called matrix transformation is mainly used to perform arithmetic operations on the Level 3 matrix! For example, if the translation operation is switched from (0, 0) to (10,120), the above Matrix's MSCALE_X = 10 and MSCALE_Y = 120 can be matrix. setValues (Matrix array ). In this way, Canvas can use this matrix for plotting and translation.
The rotation matrix has the following special structure:
- Float matrix = {cosA,-sinA, 0, sinA, cosA, 0,
- 0, 0, 1}; where A is the rotation angle. to rotate, you only need to input the angle to the above matrix.
At the same time, we know that the multiplication of matrices is asymmetric, that is, A * B! = B * A. In this case, the left multiplication and right multiplication in Matrix multiplication are involved. Similarly, in Android, Matrix also implements the left multiplication and right multiplication functions, all postXXX operations are left multiplication, and all preXXX operations are right multiplication. This should be paid attention to when using Matrix for combination transformation.
At the same time, because the final state of the matrix object is the matrix obtained after all transformations, if you need to re-use the matrix and do not want to retain the previous state, you need to call reset () resets the matrix status to avoid errors. At the same time, it should be noted that the setXXX method of the Matrix class will call reset () by default to reset the Matrix. Therefore, when implementing the combination transformation, be sure not to call the setXXX method in the middle of the transformation, in this way, all the transformations before the setXXX method are ignored.