Android graphics Matrix (ii)

Source: Internet
Author: User

Previous Android Paint Matrix (a) The theory of the Matrix and the method of operation, involving advanced mathematics, a little difficult to understand. Fortunately, Android provides a series of convenient interfaces to the matrix operation.

The matrix operates in a total of translate (panning), rotate (rotation), scale (scaling) and skew (tilt) four, each of which provides set, post, and pre three operating modes in the Android API, In addition to translate, you can specify a center point for all three other operations.

Set is the value of setting the matrix directly, each time set, the entire matrix array will be changed.

Post is a post-multiply, the current matrix multiplied by the parameters given by the matrix. You can use post several times in a row to accomplish the entire transformation you want. For example, to rotate a picture 30 degrees and then pan to (100,100), you can do this:

  Matrix  m  =   new   matrix ();        M.postrotate (+ );     M.posttranslate (   100);     

This will achieve the desired effect.

The pre is the pre-multiply, and the parameter gives the matrix multiplied by the current matrix. So the operation takes place at the very front of the current matrix. For example, in the example above, if you use the pre, this will be the case:

Matrix  m  =   new   matrix (); M.settranslate (+    );   M.prerotate (30);      

Rotation, scaling, and skewing can all be done around a central point, and if not specified, is done around (0,0) points by default.

ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.Rect;Importandroid.graphics.drawable.BitmapDrawable;ImportAndroid.util.DisplayMetrics;Importandroid.view.MotionEvent;ImportAndroid.view.View; Public classMyViewextendsView {PrivateBitmap Mbitmap; PrivateMatrix Mmatrix =NewMatrix ();  PublicMyView (Context context) {Super(context);    Initialize (); }    Private    voidInitialize () {Bitmap bmp=((bitmapdrawable) getresources (). getdrawable (R.drawable.show)). Getbitmap (); Mbitmap=bmp; /*first, you will scale to 100*100.        Here the scale parameter is proportional. One thing to note, if the direct use of 100/bmp.getwidth (), will get 0, because it is the integer division, so it must be one of the float type, directly with 100f is good. */Mmatrix.setscale (100f/bmp.getwidth (), 100f/bmp.getheight ()); //pan to (100,100)Mmatrix.posttranslate (100, 100 ); //tilt the x and y axes to the center of (100,100). Mmatrix.postskew (0.2f, 0.2f, 100, 100 ); } @Overrideprotected voidOnDraw (canvas canvas) {//Super.ondraw (canvas);//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 are as follows:

The red X and y represent the angle of inclination, and the following is X, which is Y. See, The matrix is so simple.

Android graphics Matrix (ii)

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.