Android Graphics Matrix (i)

Source: Internet
Author: User

Matrix, Chinese called matrices, Advanced Mathematics has introduced, in the image processing, mainly for the plane of the scaling, translation, rotation and other operations.
First, we introduce the matrix operation. Addition and subtraction do not have to say, too simple, corresponding to the sum of the bit is good. 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.

No professional tools, the painting is very ugly.   Explain that the above SinX and COSX, which represent the Cos and sin values of the rotation angle, note that 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, which is what it looks like.
Try the Matrix effect on Android below.

 Public classMyViewextendsView {PrivateBitmap Mbitmap; PrivateMatrix Mmatrix =NewMatrix ();  PublicMyView (Context context) {Super(context);                  Initialize (); }                  Private voidInitialize () {Mbitmap=((bitmapdrawable) getresources (). getdrawable (R.drawable.show)). Getbitmap (); floatCosvalue = (float) Math.Cos (-MATH.PI/6); floatSinvalue = (float) Math.sin (-MATH.PI/6); Mmatrix.setvalues (New float[]{Cosvalue,-sinvalue, 100, Sinvalue, Cosvalue,100,                                      0, 0, 2}); } @Overrideprotected voidOnDraw (canvas canvas) {//Super.ondraw (canvas);//of course, if there are other elements on the interface that need to be drawn, just write this sentence on the line. Canvas.drawbitmap (Mbitmap, Mmatrix,NULL); }           }  

The results of the operation are as follows:

At the top left corner, zoom in half, rotate 30 degrees counterclockwise, and then translate 50 pixels along the x and Y axes, 100 in the code, and why panning 50, because it's half scaled.
You can set the matrix value yourself, or try multiplying the two matrix to get the value set in order to be more proficient with the matrix. The direct assignment here may be a bit difficult to understand, but fortunately, Andrid provides a more convenient way to matrix, the next introduction.

Android Graphics Matrix (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.