As the matrix of the linear algebra of the university, the matrix, the basic knowledge of matrices and subtraction operations are not described here.
The Matrix class in Android is a simple 3x3 3-order 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
- };
structure as above: where mscale_x and mscale_y respectively control the x-axis and y-axis scaling, mskew_x and mskew_y are linear tilt coefficients that control X-and y-coordinates respectively, mtrans_x and Mtrans_ Y is a linear shift that controls the x direction and the y direction. Mpersp_0,mpersp_1 and mpersp_2 temporary use not much, later add explanation.
With the appeal described, the so-called matrix transformation is mainly for the 3-order matrix for arithmetic operations! Panning from (0,0) to (10,120) directly converts the mscale_x=10 and mscale_y=120 of the above matrices to matrix.setvalues (matrix array). This allows the canvas to use the matrix for drawing operations and for panning.
Where the rotation matrix is special, its structure is as follows:
- float matrix = {CosA,-sina, 0,
- SinA, CosA, 0,
- 0 , 0 , 1
- };
Where a is the angle of rotation, if you need to rotate the operation simply pass the angle to the above matrix.
At the same time we know that the multiplication of matrices is asymmetrical, that is, a*b! =b*a, this is related to the matrix multiplication of the left and right multiplication, as well as the matrix in the Android also implemented the left and right multiplication functions, all postxxx are left multiplication, all prexxx are right multiplication operations. This is something we need to be aware of when we use matrix for combinatorial transformations.
At the same time, since the final state of the Matrix object is the matrix obtained after all the transformations, all if you need to re-use the matrix and do not want to preserve the previous state, you need to call reset () to reset the state of the matrix to avoid an error. It is also important to note that the Setxxx method of the matrix class will call reset () to reset the matrix by default, so when implementing a composite transformation, be careful not to call the Setxxx method in the middle of the transformation, so that the transformation before the Setxxx method is ignored.
Introduction to the Matrix class in Android