The most recent operation using the matrix for drawing. Some of the methods of the matrix have some deeper experience, write it down so that you can review it later.
The matrix commonly used methods:
First, the transformation method:
The matrix provides translate (pan), rotate (rotate), scale (zoom), skew (skew) four operations, and the internal implementation of these four operations is done through Matrix.setvalues (...). To set the value of the matrix to achieve the effect of changing the picture.
Each of the matrix operations has set, pre, post three operations, set is empty the queue to add, the pre is inserted at the front of the queue, post is inserted at the end of the queue.
The Pre method represents a matrix forward, for example: The transformation matrix is a, the original matrix is the B,pre method is the meaning of the a*b
The Post method represents the matrix multiplier, for example: The transformation matrix is a, the original matrix is the B,post method of the meaning is B*a
1.matrix.prescale (0.5f, 1);
2.matrix.pretranslate (10, 0);
3.matrix.postscale (0.7f, 1);
4.matrix.posttranslate (15, 0);
Equivalent to:
Translate (0)-> scale (0.5f, 1)-> scale (0.7f, 1)-> translate (15, 0)
Note: The pre-call operation executes first, then the post that is invoked.
Once called, the Set method empties all transformations in the previous matrix, for example:
1.matrix.prescale (0.5f, 1);
2.matrix.setscale (1, 0.6f);
3.matrix.postscale (0.7f, 1);
4.matrix.pretranslate (15, 0);
Equivalent to
Translate (0)-> scale (1, 0.6f)-> scale (0.7f, 1)
Matrix.prescale (0.5f, 1) will not work.
Second, mapping method
The matrix provides a mapxxx method for obtaining values after the matrix mapping. Mainly include: Mappoints,maprects,mapvectors and Other methods.
These methods you will use: when you need to remember the values after the matrix operation. For example: Remember the coordinates of the four points after the rectangular rotation 34° (rotate). (You can try your own calculations, you'll find it complicated and imprecise)
It should be noted that some of the matrix methods use the center point, if not set, the default is (0,0) as the center point.
Write it down lest you forget it.
third, make the reflection effect
using the matrix can achieve a variety of pictures of special effects, and then use Marix plus gradient to achieve the effect of image reflection, steps are as follows:
1. Get the picture that needs the reflection effect, take half of the original picture here
2. Add a color gradient to the reflection picture
The specific implementation, as described in the following code, gives an effect chart in the form of a custom view, as follows:
Package Com.flection.view;
Import COM.FLECTION.MAIN.R;
Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.Config;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import android.graphics.LinearGradient;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuffXfermode;
Import Android.graphics.Shader.TileMode;
Import android.graphics.drawable.BitmapDrawable;
Import Android.util.AttributeSet;
Import Android.view.View;
public class Flectionview extends View {context mcontext=null;
Public Flectionview {Super (context);
Public Flectionview (context, AttributeSet attrs) {Super (context, attrs);
This.mcontext=context; @SuppressLint ("drawallocation") @Override protected void OnDraw (Canvas Canvas) {//Set background color This.setbackgro Undcolor (Color.parsecoLor ("#8B8378"));
Bitmap Oldbitmap = Bitmapfactory.decoderesource (Mcontext.getresources (), R.drawable.dropbox);
Bitmap Newbitmap = Createflectionbitmap (Oldbitmap);
Canvas.drawbitmap (Newbitmap,newbitmap.getwidth (), Newbitmap.getheight (), New Paint ());
This.invalidate ();
//Get the original image + Bitmap private Bitmap createflectionbitmap (Bitmap oldbitmap) {int mwidth = Oldbitmap.getwidth ();
int mheight = Oldbitmap.getheight ();
The gap between the original image and the reflection map int gap = 2;
Matrix matrix = new Matrix ();
Matrix.prescale (1,-1);
Bitmap flection = Bitmap.createbitmap (oldbitmap, 0, MHEIGHT/2, mwidth, MHEIGHT/2, Matrix, false);
Bitmap background = Bitmap.createbitmap (Mwidth, MHEIGHT+GAP+MHEIGHT/2, config.argb_8888);
Canvas Canvas = new Canvas (background);
Paint P1 = new Paint ();
Draw the original artwork Canvas.drawbitmap (oldbitmap, 0, 0, p1);
Draw a reflection of the map Canvas.drawbitmap (flection, 0, Mheight+gap, p1);
Paint shaderpaint = new Paint (); LinearGradient Shader = new LinearGradient (0, mheight, 0, Flection.getheight (), 0X70FFFFFF, 0X00FFFFFF, Tilemode.mirror);
Shaderpaint.setshader (shader);
Shaderpaint.setxfermode (New Porterduffxfermode (Android.graphics.PorterDuff.Mode.DST_IN));
Draw the gradient color canvas.drawrect (0, Mheight+gap, Mwidth, Background.getheight (), shaderpaint);
return background;
}
}
The effect of the implementation of the following figure: