SoFunction
Updated on 2025-03-02

Specific implementation method of multi-touch picture zoom in Android


package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;

public class MutilTouchDemoActivity extends Activity implements OnTouchListener, OnClickListener {
   private static final String TAG = "Touch" ;

   // These matrices will be used to move and zoom image
   Matrix matrix = new Matrix();
   Matrix savedMatrix = new Matrix();
   PointF start = new PointF();
   PointF mid = new PointF();
   float  oldDist;
   private ImageView view;
   private Button zoomIn, zoomOut;

   //button zoom
   private float scaleWidth = 1;
        private float scaleHeight = 1;
        private Bitmap bmp, zoomedBMP;
        private int zoom_level = 0;
private static final double ZOOM_IN_SCALE = 1.25;//Amplification coefficient
private static final double ZOOM_OUT_SCALE = 0.8;//Shrink coefficient

   // We can be in one of these 3 states
   static final int NONE = 0;
   static final int DRAG = 1;
   static final int ZOOM = 2;
   int mode = NONE;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      (savedInstanceState);
      setContentView();
//Zoom in button
      zoomIn = (Button) findViewById(.zoom_in);
//Zoom out button
      zoomOut = (Button) findViewById(.zoom_out);
      (this);
      (this);
      view = (ImageView) findViewById();
      (this);
//Get the image in drawable, zoom in, zoom out, and multi-touch object.
       bmp = ((), );

              }

   public boolean onTouch(View v, MotionEvent event) {
      // Handle touch events here...
      ImageView view = (ImageView) v;

      // Handle touch events here...
      switch (() & MotionEvent.ACTION_MASK) {
//Set drag mode
        case MotionEvent.ACTION_DOWN:
            (matrix);
            ((), ());
            (TAG, "mode=DRAG" );
            mode = DRAG;
            break;

         case MotionEvent.ACTION_UP:
         case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            (TAG, "mode=NONE" );
            break;
//Set multi-touch mode
         case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);
            (TAG, "oldDist=" + oldDist);
            if (oldDist > 10f) {
               (matrix);
               midPoint(mid, event);
               mode = ZOOM;
               (TAG, "mode=ZOOM" );
            }
            break;
//If it is DRAG mode, click to move the picture
         case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) {
               (savedMatrix);
// Set displacement
               (() - ,
              () - );
            }
//If it is ZOOM mode, multi-touch zoom
               else if (mode == ZOOM) {
               float newDist = spacing(event);
               (TAG, "newDist=" + newDist);
               if (newDist > 10f) {
                  (savedMatrix);
                  float scale = newDist / oldDist;
//Set the zoom ratio and the midpoint position of the picture
                  (scale, scale, , );
               }
            }
            break;
      }

      // Perform the transformation
      (matrix);

      return true; // indicate event was handled
   }
//Calculate the moving distance
   private float spacing(MotionEvent event) {
      float x = (0) - (1);
      float y = (0) - (1);
      return (x * x + y * y);
   }
// Calculate the midpoint position
   private void midPoint(PointF point, MotionEvent event) {
      float x = (0) + (1);
      float y = (0) + (1);
      (x / 2, y / 2);
   }

//Zoom in and out button click event
@Override
public void onClick(View v) {
        if(v == zoomIn){
                enlarge();
        }else if (v == zoomOut) {
                small();
        }

}

//Button click to reduce function
private void small() {

        int bmpWidth = ();
        int bmpHeight = ();

        scaleWidth = (float) (scaleWidth * ZOOM_OUT_SCALE);
        scaleHeight = (float) (scaleHeight * ZOOM_OUT_SCALE);

        Matrix matrix = new Matrix();
        (scaleWidth, scaleHeight);
        zoomedBMP = (bmp, 0, 0, bmpWidth, bmpHeight, matrix,
                        true);
        (zoomedBMP);
}

//Button click magnification function
private void enlarge() {
        try {
                int bmpWidth = ();
                int bmpHeight = ();

                scaleWidth = (float) (scaleWidth * ZOOM_IN_SCALE);
                scaleHeight = (float) (scaleHeight * ZOOM_IN_SCALE);

                Matrix matrix = new Matrix();
                (scaleWidth, scaleHeight);
                zoomedBMP = (bmp, 0, 0, bmpWidth, bmpHeight, matrix,
                                true);
                (zoomedBMP);
        } catch (Exception e) {

        }

}
}