0

I have an image view, after zoom(pinch zoom) i have to get the ends of the image. i.e. when i drag the image left or right i have to fire an event when reached to the last point(after which it cannot be dragged). I have used the zoom like this.

 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_UP:
  case MotionEvent.ACTION_POINTER_UP:
     mode = NONE;
     Log.d(TAG, "mode=NONE");
     break;
  case MotionEvent.ACTION_MOVE:
     if (mode == DRAG) {
        // ...
        matrix.set(savedMatrix);
        matrix.postTranslate(event.getX() - start.x,
              event.getY() - start.y);
     }
     else if (mode == ZOOM) {
        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);
        }
     }
     break;
  }

  view.setImageMatrix(matrix);
  return true; // indicate event was handled
  }
zaiff
  • 999
  • 2
  • 13
  • 29

1 Answers1

0

When you originally load your image into the ImageView record a point of reference, either top left corner or the center (x, y). Every time you drag or zoom you image update the changes in regards to that point of reference; i.e. if you drag the image a pixels on the x axis and b pixels on the y axis your new point of reference will be at (x + a, y + b).

Check this link for more details. And this as well.

Community
  • 1
  • 1
slybloty
  • 6,346
  • 6
  • 49
  • 70