在 Android 中设置 TouchEvent 的最大和最小缩放

Set Max and Min zoom on TouchEvent in Android

我复制了这种代码使 ImageView 可滚动。

example.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            view.setScaleType(ImageView.ScaleType.MATRIX);
            float scale;

            dumpEvent(event);
            //Handle touch events here...

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN: //first finger down only
                    savedMatrix.set(matrix);
                    start.set(event.getX(), event.getY());
                    Log.d(TAG, "mode=DRAG");
                    mode = DRAG;
                    break;
                case MotionEvent.ACTION_UP://first finger lifted

                case MotionEvent.ACTION_POINTER_UP: //second finger lifted
                    mode = NONE;
                    Log.d(TAG, "mode=NONE");
                    break;
                case MotionEvent.ACTION_POINTER_DOWN: //first and second finger down
                    oldDist = spacing(event);
                    Log.d(TAG, "oldDist=" + oldDist);
                    if (oldDist > 10f) {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                        Log.d(TAG, "mode=ZOOM");
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {
                        matrix.set(savedMatrix);
                        matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); //create the transformation in the matrix of points
                    } else if (mode == ZOOM) {
                        //pinch zooming
                        float newDist = spacing(event);
                        Log.d(TAG, "newDist=" + newDist);
                        if (newDist > 10f) {
                            matrix.set(savedMatrix);
                            scale = newDist / oldDist; //setting the scaling of the matrix...
                            //if scale > 1 means zoom in
                            // if scale < 1 means zoom out
                            matrix.postScale(scale, scale, mid.x, mid.y);
                        }
                    }
                    break;
            }
            view.setImageMatrix(matrix); //display the transformation on screen

            return true; //indicate event was handled
        }

private float spacing(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);
        }

        /*
         * --------------------------------------------------------------------------
         * Method: midPoint Parameters: PointF object, MotionEvent Returns: void
         * Description: calculates the midpoint between the two fingers
         * ------------------------------------------------------------
         */

        private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
        }

我必须在哪里放置变量来设置最大和最小缩放。

此刻我可以无限放大或缩小。

亲切的问候!

你应该有总比例的变量,因为你的比例只是当前比例和以前比例的不同。如果总计超出范围,请标准化您的局部比例。

    // normalize scale
    if (scale * scaleAll > MAX_ZOOM) {
        scale = MAX_ZOOM / scaleAll;
    }
    if (scale * scaleAll < MIN_ZOOM) {
        scale = MIN_ZOOM / scaleAll;
    }

所有代码为:

private static final float MIN_ZOOM = 0.1f;
private static final float MAX_ZOOM = 4f;
private float scaleAll = 1;

boolean someMethod() {
    // Some code
    example.setOnTouchListener(new View.OnTouchListener()        {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            view.setScaleType(ImageView.ScaleType.MATRIX);
            float scale;

            dumpEvent(event);
            //Handle touch events here...

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN: //first finger down only
                    savedMatrix.set(matrix);
                    start.set(event.getX(), event.getY());
                    Log.d(TAG, "mode=DRAG");
                    mode = DRAG;
                    break;
                case MotionEvent.ACTION_UP://first finger lifted

                case MotionEvent.ACTION_POINTER_UP: //second finger lifted
                    mode = NONE;
                    Log.d(TAG, "mode=NONE");
                    break;
                case MotionEvent.ACTION_POINTER_DOWN: //first and second finger down
                    oldDist = spacing(event);
                    Log.d(TAG, "oldDist=" + oldDist);
                    if (oldDist > 10f) {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                        Log.d(TAG, "mode=ZOOM");
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {
                        matrix.set(savedMatrix);
                        matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); //create the transformation in the matrix of points
                    } else if (mode == ZOOM) {
                        //pinch zooming
                        float newDist = spacing(event);
                        Log.d(TAG, "newDist=" + newDist);
                        if (newDist > 10f) {
                            scale = newDist / oldDist; //setting the scaling of the matrix...

                            // normalize scale
                            if (scale * scaleAll > MAX_ZOOM) {
                                scale = MAX_ZOOM / scaleAll;
                            }
                            if (scale * scaleAll < MIN_ZOOM) {
                                scale = MIN_ZOOM / scaleAll;
                            }
                            scaleAll*=scale;
                            matrix.set(savedMatrix);
                            //if scale > 1 means zoom in
                            // if scale < 1 means zoom out
                            matrix.postScale(scale, scale, mid.x, mid.y);
                        }
                    }
                    break;
            }
            view.setImageMatrix(matrix); //display the transformation on screen

            return true; //indicate event was handled
        }
    }
}
 case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) { 
                        matrix.set(savedMatrix); 
                        matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); //create the transformation in the matrix of points
                    } else if (mode == ZOOM) { 
                        //pinch zooming 
                        float newDist = spacing(event);
                        Log.d(TAG, "newDist=" + newDist);
                        if (newDist > 10f) {
                            matrix.set(savedMatrix); 
                            scale = newDist / oldDist; //setting the scaling of the matrix...
                            //if scale > 1 means zoom in 
                            // if scale < 1 means zoom out

/* 在这里你可以添加这样的东西*/

                      if(scale >= 1 && scale < maxZoom || scale <1 && scale >= minZoom){

                                matrix.postScale(scale, scale, mid.x, mid.y);
                              }
                        } 
                    } 

maxZoom = 5 或者你想要的任何值

minZoom = 0.25 或者你想要的任何值

您必须创建一个 MIN_ZOOM、MAX_ZOOM,然后按如下方式缩放它:

private static final float MIN_ZOOM = 1.0f; 
private static final float MAX_ZOOM = WHAT_YOU_WANT; 
float scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));

有关详细信息,请查看此 answer