This article mainly explains the use of Android matrix control graphics rotation zoom movement, see the code specifically:
Copy Code code as follows:
/**
* Use matrix to control picture movement, scaling, rotation
*/
public class Commonimgeffectview extends View {
private context;
Private Bitmap mainbmp, controlbmp;
private int mainbmpwidth, Mainbmpheight, Controlbmpwidth, controlbmpheight;
Private matrix matrix;
private float [] Srcps, Dstps;
Private RECTF srcrect, dstrect;
Private Paint Paint, Paintrect, Paintframe;
private float DeltaX = 0, DeltaY = 0; Displacement value
private float scalevalue = 1; Scaling values
Private point Lastpoint;
Private point Prepivot, Lastpivot;
Private float predegree, lastdegree;
Private short Currentselectedpointindex; Current Action Click Point
Private point symmetricpoint = new Point (); Current operation Point Symmetry point
/**
* Picture Operation type
*/
public static final int oper_default =-1; Default
public static final int oper_translate = 0; Move
public static final int oper_scale = 1; Scaling
public static final int oper_rotate = 2; Rotating
public static final int oper_selected = 3; Choose
public int lastoper = Oper_default;
/* Picture Control Point
* 0---1---2
* | |
* 7 8 3
* | |
* 6---5---4
*/
public static final int ctr_none =-1;
public static final int ctr_left_top = 0;
public static final int ctr_mid_top = 1;
public static final int ctr_right_top = 2;
public static final int ctr_right_mid = 3;
public static final int ctr_right_bottom = 4;
public static final int ctr_mid_bottom = 5;
public static final int ctr_left_bottom = 6;
public static final int ctr_left_mid = 7;
public static final int ctr_mid_mid = 8;
public int current_ctr = Ctr_none;
Public Commonimgeffectview {
Super (context);
This.context = context;
}
Public Commonimgeffectview (context context, AttributeSet Attrs) {
Super (context, attrs);
This.context = context;
InitData ();
}
/**
* Initialization of data
* @author Zhang Jin
*/
private void InitData () {
Mainbmp = Bitmapfactory.decoderesource (This.context.getResources (), r.drawable.flower);
Controlbmp = Bitmapfactory.decoderesource (This.context.getResources (), R.drawable.control);
Mainbmpwidth = Mainbmp.getwidth ();
Mainbmpheight = Mainbmp.getheight ();
Controlbmpwidth = Controlbmp.getwidth ();
Controlbmpheight = Controlbmp.getheight ();
Srcps = new float[]{
0,0,
mainbmpwidth/2,0,
mainbmpwidth,0,
MAINBMPWIDTH,MAINBMPHEIGHT/2,
Mainbmpwidth,mainbmpheight,
Mainbmpwidth/2,mainbmpheight,
0,mainbmpheight,
0,MAINBMPHEIGHT/2,
Mainbmpwidth/2,mainbmpheight/2
};
Dstps = Srcps.clone ();
Srcrect = new RECTF (0, 0, mainbmpwidth, mainbmpheight);
Dstrect = new RECTF ();
Matrix = new Matrix ();
Prepivot = new Point (MAINBMPWIDTH/2, MAINBMPHEIGHT/2);
Lastpivot = new Point (MAINBMPWIDTH/2, MAINBMPHEIGHT/2);
Lastpoint = new Point (0,0);
Paint = new paint ();
Paintrect = new Paint ();
Paintrect.setcolor (color.red);
Paintrect.setalpha (100);
Paintrect.setantialias (TRUE);
Paintframe = new Paint ();
Paintframe.setcolor (Color.green);
Paintframe.setantialias (TRUE);
SetMatrix (Oper_default);
}
/**
* Matrix transformation to achieve the goal of graphic translation
* @author Zhang Jin
*/
private void SetMatrix (int operationtype) {
Switch (operationtype) {
Case Oper_translate:
Matrix.posttranslate (DeltaX, DeltaY);
Break
Case Oper_scale:
Matrix.postscale (Scalevalue, Scalevalue, Symmetricpoint.x, SYMMETRICPOINT.Y);
Break
Case Oper_rotate:
Matrix.postrotate (Predegree-lastdegree, Dstps[ctr_mid_mid * 2], Dstps[ctr_mid_mid * 2 + 1]);
Break
}
Matrix.mappoints (Dstps, SRCPS);
Matrix.maprect (Dstrect, srcrect);
}
Private Boolean isonpic (int x, int y) {
if (Dstrect.contains (x, y)) {
return true;
}else
return false;
}
private int Getoperationtype (Motionevent event) {
int evx = (int) event.getx ();
int evy = (int) event.gety ();
int curoper = Lastoper;
Switch (event.getaction ()) {
Case Motionevent.action_down:
current_ctr = ISONCP (Evx, Evy);
LOG.I ("img", "CURRENT_CTR is" +current_ctr);
if (current_ctr!= ctr_none | | isonpic (EVX, Evy)) {
Curoper = oper_selected;
}
Break
Case Motionevent.action_move:
if (current_ctr > Ctr_none && current_ctr < Ctr_mid_mid) {
Curoper = Oper_scale;
}else if (current_ctr = = Ctr_mid_mid) {
Curoper = oper_rotate;
}else if (lastoper = = oper_selected) {
Curoper = oper_translate;
}
Break
Case MOTIONEVENT.ACTION_UP:
Curoper = oper_selected;
Break
Default
Break
}
LOG.D ("img", "Curoper is" +curoper);
return curoper;
}
/**
* The point of control where the judge points
* @param EVX
* @param Evy
* @return
*/
private int ISONCP (int evx, int evy) {
Rect Rect = new Rect (EVX-CONTROLBMPWIDTH/2,EVY-CONTROLBMPHEIGHT/2,EVX+CONTROLBMPWIDTH/2,EVY+CONTROLBMPHEIGHT/2);
int res = 0;
for (int i = 0; i < dstps.length; i+=2) {
if (rect.contains (int) dstps[i], (int) dstps[i+1]) {
return res;
}
++res;
}
return ctr_none;
}
@Override
public boolean dispatchtouchevent (Motionevent event) {
int evx = (int) event.getx ();
int evy = (int) event.gety ();
int opertype = Oper_default;
Opertype = Getoperationtype (event);
Switch (opertype) {
Case Oper_translate:
Translate (EVX, Evy);
Break
Case Oper_scale:
Scale (event);
Break
Case Oper_rotate:
Rotate (event);
Break
}
Lastpoint.x = EVX;
Lastpoint.y = Evy;
Lastoper = Opertype;
Invalidate ()//Redraw
return true;
}
/**
* Mobile
* @param EVX
* @param Evy
* @author Zhang_jin1
*/
private void translate (int evx, int evy) {
Prepivot.x + = Evx-lastpoint.x;
Prepivot.y + = Evy-lastpoint.y;
DeltaX = Prepivot.x-lastpivot.x;
DeltaY = Prepivot.y-lastpivot.y;
Lastpivot.x = Prepivot.x;
Lastpivot.y = Prepivot.y;
SetMatrix (oper_translate); Setting the Matrix
}
/**
* Zoom
* 0---1---2
* | |
* 7 8 3
* | |
* 6---5---4
* @param EVX
* @param Evy
*/
private void scale (Motionevent event) {
int pointindex = current_ctr*2;
float px = Dstps[pointindex];
float py = dstps[pointindex+1];
float Evx = Event.getx ();
float Evy = event.gety ();
float Oppositex = 0;
float Oppositey = 0;
if (current_ctr<4 && current_ctr >= 0) {
Oppositex = dstps[pointindex+8];
Oppositey = dstps[pointindex+9];
}else if (current_ctr >= 4) {
Oppositex = dstps[pointindex-8];
Oppositey = dstps[pointindex-7];
}
float Temp1 = getdistanceoftwopoints (Px,py,oppositex,oppositey);
float Temp2 = getdistanceoftwopoints (Evx,evy,oppositex,oppositey);
This.scalevalue = TEMP2/TEMP1;
Symmetricpoint.x = (int) Oppositex;
SYMMETRICPOINT.Y = (int) Oppositey;
LOG.I ("img", "Scalevalue is" +scalevalue);
SetMatrix (Oper_scale);
}
/**
* Rotate Picture
* 0---1---2
* | |
* 7 8 3
* | |
* 6---5---4
* @param EVX
* @param Evy
*/
private void Rotate (Motionevent event) {
if (event.getpointercount () = = 2) {
Predegree = Computedegree ((int) event.getx (0), (int) event.gety (0)), new Point ((int) event.getx (1), (int) Event.gety (1)));
}else{
Predegree = Computedegree ((int) event.getx (), (int) event.gety ()), New Point ((int) dstps[16], (int) dstps[17]) ;
}
SetMatrix (oper_rotate);
Lastdegree = Predegree;
}
/**
* Calculate the angle between two points and the vertical direction
* @param p1
* @param p2
* @return
*/
Public float Computedegree (Point p1, point p2) {
float tran_x = p1.x-p2.x;
float tran_y = p1.y-p2.y;
float degree = 0.0f;
float angle = (float) (Math.asin (tran_x/math.sqrt (tran_x*tran_x + tran_y* tran_y)) *180/math.pi);
if (! Float.isnan (angle)) {
if (tran_x >= 0 && tran_y <= 0) {//First quadrant
degree = angle;
}else if (tran_x <= 0 && tran_y <= 0) {//second quadrant
degree = angle;
}else if (tran_x <= 0 && tran_y >= 0) {//Third quadrant
degree = -180-angle;
}else if (tran_x >= 0 && tran_y >= 0) {//Fourth quadrant
degree = 180-angle;
}
}
return degree;
}
/**
* Calculate the distance between two points
* @param p1
* @param p2
* @return
*/
Private float getdistanceoftwopoints (Point p1, point p2) {
return (float) (MATH.SQRT (p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-P2.Y)));
}
Private float getdistanceoftwopoints (float x1,float y1,float x2,float y2) {
return (float) (MATH.SQRT (x1-x2) * (X1-X2) + (y1-y2) * (y1-y2)));
}
@Override
public void OnDraw (Canvas Canvas) {
Drawbackground (canvas);//Draw the background to test the map of the rectangle
Canvas.drawbitmap (mainbmp, Matrix, paint);//Draw main picture
Drawframe (canvas);//Draw border so that the mapping of test points
Drawcontrolpoints (canvas);//Drawing Control point picture
}
private void Drawbackground (Canvas Canvas) {
Canvas.drawrect (Dstrect, paintrect);
}
private void Drawframe (Canvas Canvas) {
Canvas.drawline (Dstps[0], dstps[1], dstps[4], dstps[5], paintframe);
Canvas.drawline (Dstps[4], dstps[5], dstps[8], dstps[9], paintframe);
Canvas.drawline (Dstps[8], dstps[9], dstps[12], dstps[13], paintframe);
Canvas.drawline (Dstps[0], dstps[1], dstps[12], dstps[13], paintframe);
Canvas.drawpoint (dstps[16], dstps[17], paintframe);
}
private void Drawcontrolpoints (Canvas Canvas) {
for (int i = 0; i < dstps.length; i+=2) {
Canvas.drawbitmap (Controlbmp, DSTPS[I]-CONTROLBMPWIDTH/2, DSTPS[I+1]-CONTROLBMPHEIGHT/2, paint);
}
}
}
Demo Effect: