Public class MainActivity extends Activity {private ImageView operImage; private PointF point = new PointF (); // record the position of the finger private PointF midPoint = new PointF (); // record the position of the midpoint between fingers private Matrix matrix = new Matrix (); // record the matrixprivate Matrix newMatrix = new Matrix () pressed by the finger before dragging and scaling (); // record the matrixprivate int type that is being dragged and scaled by fingers = 0; // operation type private float pointerDistance; // The distance between the two fingers is private static final int DRAG = 1; // drag private static final int SCALE = 2; // SCALE @ Overrideprotected void onCreate (Bundle savedInstanceState) {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 point of the finger. set (startX, startY); // obtain the position matrix of the current image. set (operImage. getImageMatrix (); type = DRAG; // DRAG break; case MotionEvent. action_pointer_resize down: // records the image size before scaling. set (operImage. getImageMatrix (); // record the distance between two fingers pointerDistance = getDistance (event); // record the midPoint of the distance between the two fingers = getMidPoint (event); type = SCALE; // scale 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: // SCALE newMatrix. set (matrix); float nowDistance = getDistance (event ); // calculate the ratio of float scale = nowDistance/pointerDistance based on the distance between the two fingers at this time divided by the distance between the two fingers at the beginning; // scale newMatrix according to the center point. postScale (scale, scale, midPoint. x, midPoint. y); break; default: break;} break; case MotionEvent. ACTION_UP: type = 0; break; case MotionEvent. action_pointer_reply up: type = 0; break;} operImage. setImageMatrix (newMatrix); return true ;}});}/** calculate the midpoint of two fingers */protected PointF getMidPoint (MotionEvent event) {float x = (event. getX (1) + event. getX (0)/2; float y = (event. 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. getY (0)-event. getY (1); return FloatMath. sqrt (x * x + y * y);} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}