Question

I have had pinch-to-zoom working pretty well for a few application versions now using Mike Ortiz's TouchImageView and a custom TouchViewPager so that it can work inside a view pager. This was working pretty well - the only method that TouchViewPager overrides is onInterceptTouchEvent:

public boolean onInterceptTouchEvent(MotionEvent ev) {
    TouchImageView view = getTouchView();
    if( null != view ){
        if( !view.isAtLimit() && view.getCurrentScale() > 1){
            return false;
        } else {
            Log.v(TAG, "View limit = " + view.isAtLimit());
            Log.v(TAG, "View scale = " + view.getCurrentScale());
        }
    }

    return super.onInterceptTouchEvent(ev);
}

Recently, we created a new view that extends FrameLayout and manages downloading the bitmap from the internet. While the bitmap download is in progress, this layout shows a ProgressBar, and then adds the TouchImageView once it's ready. Visually, this seems to work - we see a ProgressBar and then the TouchImageView loads - but the touches get all messed up and generally just don't work consistently.

I've tried delegating onTouchEvent from the progress image view to the touch image view - no luck. I've also tried overriding onInterceptTouchEvent in the progress image view to always return false, and that didn't work either.

Can anyone help with some ideas on where to continue debugging this problem? I've been working on this for about a week now, with little success.

This is the behavior I am seeing:

  1. Activity loads, image is visible inside the ViewPager -> FrameLayout -> TouchImageView.

  2. ViewPager works to swipe and change images, but pinch zoom does not work on first or second images. Eventually you get to a position that pinch zoom will work on. I can't figure out what's different.

  3. If you do try to pinch zoom before swiping, then the ViewPager swipes won't work, and it will take several swipes to get the ViewPager to page.

  4. Once you are on a "working position", swipe-to-page works fine, as well as pinch zoom and panning around a zoomed image. After this all positions work.

Was it helpful?

Solution

I was able to get this working, strangely, by implementing a reset method inside the TouchImageView class:

public void resetZoom() {
    Matrix imageMatrix = getImageMatrix();
    RectF drawableRect = new RectF(0, 0, bmWidth, bmHeight);
    RectF viewRect = new RectF(0, 0, getWidth(), getHeight());

    imageMatrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);

    matrix = imageMatrix;
    setImageMatrix(matrix);

    fitAndCenterView();

    invalidate();
}

This method resets the zoom on the image when called - and when we set up the image inside the framelayout calling this gets everything set up. Oddly, this is not required when the image is not inside a FrameLayout.

I'd of course appreciate any code cleanup and whatnot that we can do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top