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

时间:2015-09-21 08:59:04

标签: android zoom

我复制了这种代码以使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);
        }

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

在片刻,我可以无限放大或缩小。

亲切的问候!

3 个答案:

答案 0 :(得分:2)

您必须创建一个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

答案 1 :(得分:0)

您应该为总比例设置变量,因为您的比例只是当前比例和之前比例之间的差异。如果你完全超出范围,则规范你的本地规模。

    // 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
        }
    }
}

答案 2 :(得分:0)

 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或您想要的任何值