SoFunction
Updated on 2025-04-10

Android image operation (zoom movement) instance code

/**
* Operation of viewing pictures
 * @author hongj
 */
public class GalleryViewTouch extends Activity  {
    private ImageView iv;
     private Bitmap bitmap=null;
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();
        DisplayMetrics dm;
float minScaleR;// Minimum scaling ratio
static final float MAX_SCALE = 4f;// Maximum scaling ratio
static final int NONE = 0;// Initial state
static final int DRAG = 1;// Drag
static final int ZOOM = 2;// Zoom
        int mode = NONE;
        PointF prev = new PointF();
        PointF mid = new PointF();
        float dist = 1f;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(.view_show);
        dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);// Get resolution
        bitmap=(getResources(), .bottom_layout_background);
        iv=(ImageView)findViewById(.view_img);
        (bitmap);
        (new ImageTouch(bitmap,,,iv));

    }

    class ImageTouch implements OnTouchListener {
        private ImageView iv;
         private Bitmap bitmap=null;
            Matrix matrix = new Matrix();
            Matrix savedMatrix = new Matrix();
            int dmwidth,dmheight;
float minScaleR;// Minimum scaling ratio
static final float MAX_SCALE = 4f;// Maximum scaling ratio
static final int NONE = 0;// Initial state
static final int DRAG = 1;// Drag
static final int ZOOM = 2;// Zoom
            int mode = NONE;
            PointF prev = new PointF();
            PointF mid = new PointF();
            float dist = 1f;
            ImageTouch(Bitmap bitmap,int width,int height,ImageView iv){
                = bitmap;
                =width;
                =height;
                =iv;
                initImage();
            }
            public void initImage(){
                if (dmwidth < ()|| dmheight < ()) {

                    minZoom();
                    center();
                    (matrix);
                } else {
                    ();
                }
            }
        @Override

        public boolean onTouch(View v, MotionEvent event) {
             switch (() & MotionEvent.ACTION_MASK) {
// Click the main button
                case MotionEvent.ACTION_DOWN:
                    ("", "ACTION_DOWN");
                    (matrix);
                    ((), ());
                    mode = DRAG;
break; // Click the secondary button
                case MotionEvent.ACTION_POINTER_DOWN:
                    ("", "ACTION_POINTER_DOWN");
dist = spacing(event); // If the distance between two consecutive points is greater than 10, it is determined to be multi-point mode
                    if (spacing(event) > 10f) {
                        (matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {
                        (savedMatrix);
                        (() - , ()
                                - );

                    } else if (mode == ZOOM) {
                        float newDist = spacing(event);
                        if (newDist > 10f) {
                            (savedMatrix);
                            float tScale = newDist / dist;
                            (tScale, tScale, , );
                        }
                    }
                    break;
                }

                (matrix);
                if(mode!=NONE)
               CheckView();

                return true;
        }
         private void CheckView() {
                float p[] = new float[9];
                (p);
                if (mode == ZOOM) {
                    if (p[0] < minScaleR) {
                        (minScaleR, minScaleR);
                    }
                    if (p[0] > MAX_SCALE) {
                        (savedMatrix);
                    }
                }
                center();
            }

         public void minZoom() {
                ("test", ()+"");
                ("test", ()+"");
                minScaleR = (
                        (float) dmwidth / (float) (),
                        (float) dmheight / (float) ());
                if (minScaleR < 1.0) {
                    (minScaleR, minScaleR);
                }
            }
          protected void center(boolean horizontal, boolean vertical) {
                Matrix m = new Matrix();
                (matrix);
                RectF rect = new RectF(0, 0, (), ());
                (rect);
                float height = ();
                float width = ();
                ("",+"************"+ );
                float deltaX = 0, deltaY = 0;
if (vertical) { // If the picture is smaller than the screen size, it will be displayed in the center. Larger than the screen, if left empty above, move upward, if left empty below, move downward