Matrix (1) in the previous Android plot, I talked about the principle and Calculation Method of matrix, which is difficult to understand when it comes to advanced mathematics. Fortunately, Android provides a series of matrix operations
Convenient column interface.
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:
Java code
- Matrix M = new matrix ();
- M. postrotate (30 );
- M. posttranslate (100,100 );
Matrix M = new matrix (); 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
In this way:
Java code
- Matrix M = new matrix ();
- M. settranslate (100,100 );
- M. prerotate (30 );
Matrix M = new matrix (); 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.
Java code
- 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 );
- }
- }
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 () is used directly, 0 is obtained. Because it is an integer division, one of them must be of the float type and 100f is used directly. */Mmatrix. setscale (100f/BMP. getwidth (), 100f/BMP. getheight (); // translate to the mmatrix at (100,100. posttranslate (100,100); // tilt 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. No, matrix is that simple.