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.
First, we will introduce matrix operations. Addition and subtraction are not required. It is too simple to add the corresponding bits. Image processing mainly uses multiplication. The following is a multiplication formula:
In Android, Matrix is a 3*3 Matrix consisting of 9 float values. For example.
Without professional tools, the painting is ugly. 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.
Next, we will try the effect of Matrix on Android.
Java code
Public class MyView extends View {
Private Bitmap mBitmap;
Private Matrix mMatrix = new Matrix ();
Public MyView (Context context ){
Super (context );
Initialize ();
}
Private void initialize (){
MBitmap = (BitmapDrawable) getResources (). getDrawable (R. drawable. show). getBitmap ();
Float cosValue = (float) Math. cos (-Math. PI/6 );
Float sinValue = (float) Math. sin (-Math. PI/6 );
MMatrix. setValues (
New float [] {
CosValue,-sinValue, 100,
SinValue, cosValue, 100,
0, 0, 2 });
}
@ Override protected void onDraw (Canvas canvas ){
// Super. onDraw (canvas); // Of course, if there are other elements to draw on the interface, just write this sentence.
Canvas. drawBitmap (mBitmap, mMatrix, null );
}
}
The running result is as follows:
Take the top left corner as the vertex, zoom in half, rotate 30 degrees counter-clockwise, and then translate 50 pixels along the X axis and Y axis, respectively. The Code writes 100. Why translate 50, because it is scaled in half.
You can set the value of Matrix by yourself, or try to multiply the two matrices to set the value so that you can be more familiar with Matrix.
The direct assignment method mentioned here may be a bit difficult to understand, but fortunately, andrid provides a more convenient method for the matrix. The next article introduces
From column zhoujiyu123