Previously in linear algebra to learn the matrix, the basic operation of the matrix have some understanding, now in Android has a matrix class, its Chinese meaning is the matrix. Matrix is mainly used for image zooming, panning, rotating, twisting and other operations. Image processing, the main use is multiplication.
Here is a formula for multiplication:
In Android, the Matrix is composed of 9 float values, which is a 3*3. Such as
It has the following meanings:
Sinx and Cosx, which represent the Cos value and the sin value of the rotation angle (the rotation angle is calculated in a clockwise direction). Translatex and Translatey represent the amount of translation for x and Y. Scale is the scale, 1 is constant, and 2 is the scale 1/2.
There is also a diagram of another style:
The Mscale in the matrix is used to process the scaling transformation, Mskew is used to handle the error-tangent transformation, Mtrans is used to process the translation transformation, MPERSP is used to process the perspective transformation.
Class Overview
The Matrix class holds a 3x3 Matrix for transforming coordinates.
There are two types of matrix constructors:
public Matrix ()
Create an identity matrix
public matrix (Matrix src)
Create a matrix that's a (deep) copy of SRC
Parameters
src The matrix to copy into this matrix
By understanding these basic concepts, you can simply use the matrix.
First an image display with no action:
The image is named: Img_saber
The full image is: size is 960*540 (or relatively large, the focus is still my Queen!) Ha ha. )
Create a new MyView class first:
Public classMyViewextendsView {PrivateBitmap Bitmap; PrivateMatrix Matrix =NewMatrix (); PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); //Get Bitmap Object This. Bitmap = Bitmapfactory.decoderesource (Super. Getresources (), r.drawable.img_saber); } @Overrideprotected voidOnDraw (canvas canvas) {//TODO auto-generated Method Stub Super. OnDraw (canvas); Canvas.drawbitmap ( This. Bitmap, This. Matrix,NULL); }}
Implemented in Activity_main.xml:
(Com.topcsa.zhj_matrix. MyView is the package name plus class name)
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <Com.topcsa.zhj_matrix. MyViewAndroid:id= "@+id/myview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>
Implemented in Mainactivity:
Public class extends Activity { @Override protectedvoid onCreate (Bundle Savedinstancestate) { super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); }}
Run the program, the result
(Obviously the image is not displayed intact)
With the original image of the contrast, it is good to do.
As I said before, the matrix's processing of images can be divided into four basic transformations:
Translate Translation Transformation
Rotate Rotation transformation
Scale scaling transformation
Skew Error-cutting transformation
An implementation of one of the following:
Because the image is large, the operation of the following instance, in addition to zooming, will perform a zoom operation to see the effect.
1. Scale
There are 6 methods for the Scale,matrix class:
Public void setscale (float sx, float sy, float px, float py)
Set The matrix to scale by SX and Sy, with a pivot point at (px, py). The pivot point is the coordinate, should remain unchanged by the specified transformation.
Public void setscale (float sx, float sy)
Set The matrix to scale by SX and Sy.
Public boolean prescale (float sx, float sy)
Preconcats The matrix with the specified scale. M ' = m * S (SX, SY)
Public boolean preskew (float kx, Float KY, float px, float py)
Preconcats The matrix with the specified scale. M ' = m * S (SX, SY, px, py)
Public boolean postscale (float sx, float sy)
Postconcats The matrix with the specified scale. M ' = S (SX, SY) * m
Public boolean postscale (float sx, float sy, float px, float py)
Postconcats The matrix with the specified scale. M ' = S (SX, SY, px, py) * m
Scale is the most way to go. Add the code below the MyView method, and the rest of the code is the same:
Public MyView (Context context, AttributeSet attrs) { Super(context, attrs); // get Bitmap Object this. Bitmap = Bitmapfactory.decoderesource (super. Getresources (), r.drawable.img_ Saber); Matrix.setscale (0.5f, 0.5f); // with (0.0) as the benchmark, the image shrinks by half }
The results of the operation are as follows: (significantly smaller than previous images)
If you replace the method with
Matrix.setscale (0.5f, 0.5f, 50, 500); // (50,500) replace (0.0) to zoom
Run as follows:
If you replace the code with:
Matrix.prescale (0.5f, 0.5f);
The results of the operation are as follows:
Here is the result of the run with the code: Matrix.setscale (0.5f, 0.5f); What I want to say is that if you run Matrix.postscale (0.5f, 0.5f) alone, the result is still the same. Then you will certainly think that the three ways the results are the same, then what is the difference? Don't worry, this problem is solved at the end of our article.
This completes the origin scaling and specifies the point scaling.
(The other three methods do not have an operation, the method contains more than the method of scaling, and the meaning of the method is basically similar to scaling, the following three kinds of operations do not show their methods.) )
2, Rotate
Modify the constructor in MyView as follows:
Public MyView (Context context, AttributeSet attrs) { Super(context, attrs); // get Bitmap Object this. Bitmap = Bitmapfactory.decoderesource (super. Getresources (), r.drawable.img_ Saber); Matrix.setrotate (30); // rotates the picture at the origin (0.0) to a certain angle, the negative number to the left, and the positive to the right . Matrix.prescale (0.5f, 0.5f); }
The results of the operation are as follows:
Replace with code:
Matrix.setrotate (30,100,0); // Specify a point (100,0) to rotate around the point
The results are as follows:
3, Skew
Modify the code as follows:
Public MyView (Context context, AttributeSet attrs) { Super(context, attrs); // get Bitmap Object this. Bitmap = Bitmapfactory.decoderesource (super. Getresources (), r.drawable.img_ Saber); Matrix.setskew (0.15f, 0.15f); // Distort the image Matrix.prescale (0.5f, 0.5f); }
To run the program:
Replace with code:
Matrix.setskew (0.05f, -0.15f,700,500); // distorts the image by pointing at the point as a reference
Run as follows:
4.Translate
As usual, change the code as follows:
Public MyView (Context context, AttributeSet attrs) { Super(context, attrs); // get Bitmap Object this. Bitmap = Bitmapfactory.decoderesource (super. Getresources (), r.drawable.img_ Saber); Matrix.settranslate (50, 100); // Pan to Point (100,100) Matrix.prescale (0.5f, 0.5f); }
The results of the operation are as follows:
OK, here are four kinds of image effects for a simple demonstration complete.
Back to the previous question: (similar to several other effects)
The difference between Setscale,prescale and Postscale
Uh...... This question, in fact, I did not delve into, but found on the internet a commentary on the article, to share to you:
Http://www.eoeandroid.com/blog-659748-5465.html
PS: A method used or useless before
Matrix.setscale (0.5f, 0.5f);//With (0.0) as the benchmark, the image is reduced by half
Matrix.setscale (0.5f, 0.5f, 50, 500);//(50,500) instead of (0.0) scaling
Matrix.prescale (0.5f, 0.5f);
Matrix.setrotate (30);//causes the picture to rotate to an angle at the origin (0.0), negative to the left, and positive to the right.
Matrix.setrotate (30,100,0);//Specify a point (100,0) to rotate around the point
After the point (x, y) is skew (kx,ky,px,py) transformed, the coordinates are (kx* (y-py) +px,ky* (x-px) +py), and if PX and py are not, the default is 0.
Matrix.setskew (0.15f, 0.15f);
Matrix.setskew (0.15f, 0.15f,-100,-150);//pointing at a point as a reference point, distorting the image
Matrix.setsincos (float) math.sin (MATH.PI/3),
(float) Math.sin (MATH.PI/3));//The sin and Cos values rotated at the original point as coordinates
Matrix.setsincos (float) math.sin (MATH.PI/3),
(float) Math.sin (MATH.PI/3), 100, 100);//The sin and Cos values rotated at (100,100) point coordinates
Matrix.settranslate (50, 100);//pan to point (100,100)
Image effects in Android (Matrix)