Matrix is called a matrix in Chinese. It is introduced in advanced mathematics. In terms of image processing, it is mainly used for operations such as plane scaling, translation, and rotation.
In Android, matrix is a 3*3 matrix consisting of 9 float values. Remember. For example:
The SiNx and cosx above indicate the COs and sin values of the rotation angle. Note that the rotation angle is calculated clockwise.
Translatex and translatey indicate the translation quantity of X and Y. Scale is the scale ratio, 1 is the same, 2 is the scale 1/2, this way.
For example: Rotate 90 degrees from the simplest
There is a corresponding rotation function in Android. Graphics. Matrix:
Matrix matrix = new matrix ();
Matrix. setrotate (90 );
Output matrix in the program: system. Out. println ("Matrix:" + matrix. tostring ());
You will find one thing:
It is basically the same as the above formula (Android. Graphics. matrix uses floating-point numbers while we use integers ).
With the above example, you can try it yourself. Through the above example, we also found that we can also initialize the Matrix directly, for example, to rotate 30 degrees:
This means that we can directly operate the matrix to replace those rotating functions and scaling functions.
Let's take a specific example: rotate 45 degrees
[Java]View plaincopy
- // Define a matrix object
- Matrix matrix = new matrix ();
- Float [] values = {0.707f,-0.707f, 0.0f, 0.707f, 0.707f, 0.0f, 0.0f, 0.0f, 1.0f };
- Matrix. setvalues (values );
- System. Out. println ("Matrix:" + matrix. tostring ());
- // BMP. getwidth () and 500 respectively indicate the width and height of the repainted bitmap.
- Bitmap dstbmp = bitmap. createbitmap (BMP, 0, 0, BMP. getwidth (), 500,
- Matrix, true );
- // Draw the rotated bitmap on the canvas
- // Place it at the coordinate of 0,200
- Canvas. drawbitmap (dstbmp, 0,200, null );