1

I implemented multitouch zoom and drag for an image in a viewpager. How do I stop the image from passing a certain scale and x-position? I also want to know when the position is at the boundary I set. Here is my implementation of onTouch()

public boolean onTouch(View v, MotionEvent event) {
    // handle touch events here

    ImageView view = (ImageView) v;
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            savedMatrix.
            start.set(event.getX(), event.getY());
            mode = DRAG;
            lastEvent = null;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);
            if (oldDist > 10f) {
                savedMatrix.set(matrix);
                midPoint(mid, event);
                mode = ZOOM;
            }
            lastEvent = new float[4];
            lastEvent[0] = event.getX(0);
            lastEvent[1] = event.getX(1);
            lastEvent[2] = event.getY(0);
            lastEvent[3] = event.getY(1);
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            lastEvent = null;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) {
                matrix.set(savedMatrix);
                float dx = event.getX() - start.x;
                float dy = event.getY() - start.y;
                matrix.postTranslate(dx, dy);
            } else if (mode == ZOOM) {
                float newDist = spacing(event);
                if (newDist > 10f) {
                    matrix.set(savedMatrix);
                    float scale = (newDist / oldDist);
                    matrix.postScale(scale, scale, mid.x, mid.y);
                }
            }
            break;
    }

    view.setImageMatrix(matrix);
    return true;
}

So far I tried to store a total change of scale and movement, but it hasn't worked out so well. I'm hoping to find something that directly has to do with the Matrix methods

ndh
  • 111
  • 2
  • Elaborate on `hasn't worked so well` ;) I've never dealt with this sort of thing in image viewing in Android but storing the net relative displacement from the origin sounds pretty reasonable if you want to get the absolute position at any instance of time. – Aleksander Lidtke Nov 10 '13 at 12:42
  • Some weird occurrences with zooming. Sometimes it zoomed by itself until it disappeared, sometimes it became jumpy. I tried many things that didn't do very much. I don't think describing them will help. – ndh Nov 10 '13 at 12:45
  • I see. Have you seen these two posts? http://stackoverflow.com/questions/17411221/why-imageviews-getx-gety-setx-and-sety-do-not-represent-the-actual-value-in-t http://stackoverflow.com/questions/16557076/how-to-smoothly-move-a-image-view-with-users-finger-on-android-emulator – Aleksander Lidtke Nov 10 '13 at 12:49
  • I actually got it, thanks. I misunderstood what information the matrix holds. I thought it holds the difference, when it actually holds the displacement. I just needed the displacement – ndh Nov 14 '13 at 05:45

0 Answers0