TouchImageView设置拖动和缩放界限,防止屏幕脱落

时间:2015-09-27 17:32:51

标签: android imageview touch zoom pan

我有一个裁剪活动和可绘制矩形背后的图像视图,我设置了一个触摸方法来缩放和拖动图像视图,但问题是我可以将其拖离屏幕并放大和缩小屏幕,我想设置拖动和缩放的限制因此它适合我的矩形,这是一个方形宽度和高度是相同的。

我尝试了很多不同类型的代码,但它仍然没有开始工作, 我还需要处理更宽的图像,而不仅仅是方形或垂直图像。

(请帮我解决这个问题,没有任何图书馆,我不允许使用任何图书馆)

这是我的代码:

@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; 
   }

1 个答案:

答案 0 :(得分:0)

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