public class Mainactivity extends Activity {private ImageView operimage;private PointF point = new PointF ();//Record the position of the finger Priva Te PointF midpoint = new PointF ();//Record the position of the midpoint between the fingers private matrix matrix = new Matrix ();//record drag and zoom before finger pressed matrixprivate Matrix NE Wmatrix = new Matrix ()///record finger being dragged and scaled matrixprivate int type = 0;//action type private float pointerdistance;//two finger distance private static final int DRAG = 1;//drag private static final int scale = 2;//zoom @overrideprotected void onCreate (Bundle savedinstance State) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); operimage= (ImageView) Findviewbyid (R.id.before); Operimage.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean OnTouch (View V, motionevent event) {int action = Event.getaction () &motionevent.action_mask;switch (action) {case MotionEvent.ACTION_DOWN:float StartX = Event.getx (); Float starty = Event.gety ();//Record the position of the finger point.set (StartX, starty);// Get the position of the current picture Matrix.set (Operimage.getimagematrix ()); type = drag;//drag break;case motionevent.action_pointer_1_down://record picture does not scale before the size Matrix.set (Operimage.getimagematrix ()); Record the distance of two fingers pointerdistance = getdistance (event);//Record the midpoint of the distance of two fingers midpoint= getmidpoint (event); type = scale;//Zoom break;case MotionEvent.ACTION_MOVE:switch (type) {case drag://drag float newx = Event.getx (); Float newy = Event.gety (); Newmatrix.set ( matrix); Float dx = newx-point.x;float dy = newy-point.y;newmatrix.posttranslate (dx, dy); break;case scale://Zoom Newmatrix . Set (matrix); float nowdistance = Getdistance (event), and//calculates the scaling ratio of float scale = nowdistance/based on the distance between the two fingers at this point divided by the distance between the first two fingers pointerdistance;//Scaling Newmatrix.postscale (scale, scales, midpoint.x, midpoint.y) according to the center point; break;default:break;} Break;case MotionEvent.ACTION_UP:type = 0;break;case MotionEvent.ACTION_POINTER_1_UP:type = 0;break;} Operimage.setimagematrix (Newmatrix); return true;});} /* Calculates the midpoint of two fingers */protected PointF getmidpoint (motionevent event) {float x = (Event.getx (1) + event.getx (0))/2;float y = (E Vent.gety (1) + event.gety (0))/2;return new PointF (x, y);} /* * Calculate the distance between two points */protected float getdistance (motionevent event) {float x = event.getx (0)-event.getx (1); Float y = event.get Y (0)-event.gety (1); return floatmath.sqrt (x*x + y*y);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}