Matrix for Android plotting

Source: Internet
Author: User

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.

Public class myview extends view {

Private bitmap mbitmap;

Private matrix mmatrix = new matrix ();

Public myview (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 ){

// 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, scale in half, rotate 30 degrees counterclockwise, and then translate 50 pixels along the X axis and Y axis, respectively,CodeIt is written in 100. Why is the translation 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.

 

Matrix Operations are divided into four types: translate, rotate, scale, and skew.
The android API provides set, post, and Pre operations. In addition to translate, you can specify the center point for all the other three operations.

Set is to directly set the value of the matrix. Each time a set is set, the array of the entire matrix is changed.

Post is the posterior multiplication. The current matrix is multiplied by the matrix given by the parameter. You can use post multiple times in a row to complete the entire transformation. For example
To 30 degrees, and then to the (100,100) place, you can do this:

 
Matrix M =NewMatrix ();
 
 
 
M. postrotate (30 );
 
 
 
M. posttranslate (100,100 );

This achieves the desired effect.

PRE is a forward multiplication. The matrix given by the parameter is multiplied by the current matrix. Therefore, the operation occurs at the beginning of the current matrix. For example, in the above example, if pre is used, it should be as follows:

Matrix M =NewMatrix ();
 
 
 
M. settranslate (100,100 );
 
 
 
M. prerotate (30 );

 

Rotation, scaling, and skew can all be performed around a central point. If not specified, the rotation is performed around (0, 0) points by default.

 

The following is an example.

Package chroya. Demo. graphics;

Import Android. content. context;

Import Android. Graphics. Bitmap;

Import Android. Graphics. Canvas;

Import Android. Graphics. matrix;

Import Android. Graphics. rect;

Import Android. Graphics. drawable. bitmapdrawable;

Import Android. util. displaymetrics;

Import Android. View. motionevent;

Import Android. View. view;

Public class myview extends view {


Private bitmap mbitmap;

Private matrix mmatrix = new matrix ();


Public myview (context ){

Super (context );

Initialize ();

}

Private void initialize (){


Bitmap BMP = (bitmapdrawable) getresources (). getdrawable (R. drawable. Show). getbitmap ();

Mbitmap = BMP;

/* First, scale to 100*100. Here, the scale parameter is proportional. Note that if 100/

BMP. getwidth () will get 0, because it is an integer division, so one of them must be float type, just use 100f directly. */

Mmatrix. setscale (100f/BMP. getwidth (), 100f/BMP. getheight ());

// Pan to (100,100)

Mmatrix. posttranslate (100,100 );

// Tilt the X and Y axes, centered on (100,100.

Mmatrix. postskew (0.2f, 0.2f, 100,100 );

}


@ Override protected void ondraw (canvas ){

// Super. ondraw (canvas); // if there are other elements to draw on the interface, just write this sentence.


Canvas. drawbitmap (mbitmap, mmatrix, null );

}

}

The running effect is as follows:

The red X and Y indicate the skew angle. The following is X, and the above is Y.

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.