How to keep an image inside the screen limits while using pinch zoom and drag gestures?

后端 未结 2 1273
死守一世寂寞
死守一世寂寞 2021-02-09 21:53

I\'m building an app that uses the pinch zoom and drag. The problem is that for now I can drag the picture out of it bounds. I wanted to know how can I use drag and make sure th

2条回答
  •  耶瑟儿~
    2021-02-09 22:43

    Why not grab the dimensions of the screen and check the MotionEvent coordinates are within these before updating your matrix?

    Something like..

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenHight = displaymetrics.heightPixels;
    int screenWidth = displaymetrics.widthPixels;
    
       ...
       case MotionEvent.ACTION_MOVE:
    
          if (mode == DRAG) {
    
             int newX = event.getX() - start.x;
             int newY = event.getY() - start.y;
             if ( (newX <= 0 || newX >= screenWidth) ||
                  (newY <= 0 || newY >= screenHeight) )
                 break;
    
             matrix.set(savedMatrix);
             matrix.postTranslate(newX, newY);
          }
       ...
    

提交回复
热议问题