jasonpolites / gesture-imageview

Implements pinch-zoom, rotate, pan as an ImageView for Android 2.1+
1.15k stars 501 forks source link

Unable to use in a ViewPager #30

Open petar-bogdanov opened 12 years ago

petar-bogdanov commented 12 years ago

My project has a web gallery where you can swipe through photos. It would be nice to have zooming and panning.

I tried setting the ViewPager's ontouchlistener, to the GestureImageView's TouchListener, but no success. Either nothing happens, or the images become black.

Any ideas?

yevon commented 12 years ago

I have a view pager and it works correctly, the drawback is that you should be able to detect if imageView is zoomed for desactive viewPager paging. If not, you cannot move or zoom to left/right.

petar-bogdanov commented 12 years ago

How are you placing it in the ViewPager? With an image, or loaded with an AsyncTask? Do you have a custom OnTouchListener?

yevon commented 12 years ago

I have a custom ViewPager that can disable paging. Then i just use a GestureImageView. I have modified the gesture imageView to have a function called isZoomed(). Then i put a touchListener to theGestureImageView, and if it isZoomed() i do ViewPager.setPagingEnabled(false), else setPagingEnabled(true). That way you can't pass to the next /prior image if it is zoomed.

This code has a litlle problem, if you do a double tap to disable zoom, its not detected, so isZoomed is not false and you would not be able to switch to next image. If you do pinch to disable zoom works perfect. It need to put isZoomed = false somwhere in code, i have to discover where.


public class ViewPagerDisable extends ViewPager {

private boolean enabled;

public ViewPagerDisable(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.enabled = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onTouchEvent(event);
    }

    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
}

}

In gestureImageView.java...

public boolean isZoomed() { boolean result = false; if(gestureImageViewTouchListener != null){ result = gestureImageViewTouchListener.isZoomed(); } return result; }


In GestureImageViewTouchListener.java modify this...

private boolean isZoomed = false;

public boolean isZoomed() {
    return isZoomed;
}

public void setZoomed(boolean isZoomed) {
    this.isZoomed = isZoomed;
}

protected void handleScale(float scale, float x, float y) {

    currentScale = scale;

    if(currentScale > maxScale) {
        currentScale = maxScale;
        isZoomed = true;
    }
    else if (currentScale < minScale) {
        currentScale = minScale;
        isZoomed = false;
    }
    else {
        isZoomed = true;
        next.x = x;
        next.y = y;
    }

    calculateBoundaries();

    image.setScale(currentScale);
    image.setPosition(next.x, next.y);

    if(imageListener != null) {
        imageListener.onScale(currentScale);
        imageListener.onPosition(next.x, next.y);
    }

    image.redraw();
}
petar-bogdanov commented 12 years ago

Thank you, that's very helpful.

soyangel commented 12 years ago

Apart from changes you metioned I have assigned isZoomed here, and seems to work when zooming in/out with double tap:

In GestureImageViewTouchListener:

    zoomAnimation.setZoomAnimationListener(new ZoomAnimationListener() {
        @Override
        public void onZoom(float scale, float x, float y) {
            if(scale <= maxScale && scale >= minScale) {
                handleScale(scale, x, y);
            }
        }

        @Override
        public void onComplete() {
            inZoom = false;
            handleUp();
            isZoomed = zoomAnimation.getZoom() > 1.0;   // CHANGED LINE
        }
    });
yevon commented 12 years ago

I have been 2 week in holidays. I have just tried it, works perfect your fix! Thanks!

kirich1409 commented 12 years ago

I solve problem with setting zoom after pinch. Add string in GestureImageViewTouchListener

public boolean onTouch(View v, MotionEvent event) { if (!inZoom) { if (!tapDetector.onTouchEvent(event)) {... if (event.getAction() == MotionEvent.ACTION_UP) { handleUp(); isZoomed = !(lastScale == startingScale); //NEW } ...

brk3 commented 12 years ago

If it helps anyone I've combined the various changes mentioned above and put them on this branch: https://github.com/brk3/gesture-imageview/compare/topic/issue-30-isZoomed (Don't have time to format a pull request right now, also the code uses tabs which is really annoying - I can't get my editor to match up properly.)

Update People struggling with this lib should check out PhotoView by Chris Banes. It handles ViewPager and fixes a lot of other issues I've been experiencing.

httpdispatch commented 11 years ago

@brk3 thanks, i've created pull request based on your code modifications

longdw commented 10 years ago

Thanks all of you,your response solved my problem!!!

jonasasx commented 10 years ago

I've found out very interesting solution!

    @Override
    public boolean onTouch(View v, MotionEvent event) {
+       if (isZoomed())
+           image.getParent().requestDisallowInterceptTouchEvent(true);
+   private boolean zoomed;
+   public boolean isZoomed() {
+       return zoomed;
+   }
+   public void setZoomed(boolean zoomed) {
+       this.zoomed = zoomed;
+   }

and @kirich1409's:

    if (event.getAction() == MotionEvent.ACTION_UP) {
        handleUp();
+       this.zoomed = !(lastScale == startingScale);
    }
ipip2005 commented 10 years ago

@jonasasx Thank you for your advice and it almost worked out but this line: this.zoomed = !(lastScale == startingScale); sometimes these two variables are not equal when the image was totally zoomed out probably could change to: zoomed = !(Math.abs(lastScale - startingScale) < 0.01);