TouchImageView 设置拖动和缩放边界限制,防止离开屏幕

TouchImageView set drag and zoom bounds limits , prevent from going off screen

我在可绘制矩形后面有一个裁剪 activity 和一个图像视图,我设置了一种触摸方法来缩放和拖动图像视图,但问题是我可以将它拖出屏幕并放大和缩小屏幕,我想为拖动和缩放设置限制,以便它适合我的矩形,它是一个宽度和高度相同的正方形。

我已经尝试了许多不同类型的代码,但仍然无法正常工作, 我还需要处理更宽的图像,而不仅仅是方形或垂直图像。

(请在没有任何库的情况下帮助我解决这个问题,我不允许使用任何库)

这是我的代码:

@Override
public boolean onTouch(View v, MotionEvent event) {

     ImageView view = (ImageView) v;

      // Dump touch event to log
      dumpEvent(event);

      // Handle touch events here...
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
         savedMatrix.set(matrix);
         start.set(event.getX(), event.getY());
         Log.d(TAG, "mode=DRAG");
         mode = DRAG;
         break;

      case MotionEvent.ACTION_POINTER_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_POINTER_UP:
         mode = NONE;
         Log.d(TAG, "mode=NONE");
         break;
          case MotionEvent.ACTION_UP:

          case MotionEvent.ACTION_MOVE:
         if (mode == DRAG) {

            matrix.set(savedMatrix);

            matrix.postTranslate(event.getX()  - start.x /(1.0f) ,
                 event.getY() - start.y / (1.0f) );


         }
         else if (mode == ZOOM) {
            float[] f = new float[9];

            float newDist = spacing(event);
            Log.d(TAG, "newDist=" + newDist);

            if (newDist > 10f) {
               matrix.set(savedMatrix);
               float scale = newDist / oldDist;
               matrix.postScale(scale, scale, mid.x, mid.y);
            }

             matrix.getValues(f);
             scaleX = f[Matrix.MSCALE_X];
             scaleY = f[Matrix.MSCALE_Y];


             if(scaleX <= 0.9f) {
                matrix.postScale((0.9f) / scaleX, (0.9f) /scaleY, mid.x, mid.y);
            }
            else if(scaleX >= 2.5f) {
                   matrix.postScale((2.5f)/scaleX, (2.5f)/scaleY, mid.x, mid.y);             
       } 


      }

         break;


      }

      view.setImageMatrix(matrix);
      return true; 
   }
public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int sH = displaymetrics.heightPixels;
        int sW = displaymetrics.widthPixels;
        float dx, dy, newX, newY;

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                dx = event.getRawX() - v.getX();
                dy = event.getRawY() - v.getY();
                break;

            case MotionEvent.ACTION_MOVE:
                newX = event.getRawX() - dx;
                newY = event.getRawY() - dy;

                if ((newX <= 0 || newX >= sW-v.getWidth()) || (newY <= 0 || newY >= sH-v.getHeight()))
                    break;

                v.setX(newX);
                v.setY(newY);
                break;

            case MotionEvent.ACTION_UP:
                break;

            case MotionEvent.ACTION_CANCEL:
                break;

            default:
                break;
        }

        return true;
    }