Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use in a ViewPager #30

Open
petar-bogdanov opened this issue Jun 27, 2012 · 12 comments
Open

Unable to use in a ViewPager #30

petar-bogdanov opened this issue Jun 27, 2012 · 12 comments

Comments

@petar-bogdanov
Copy link

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
Copy link

yevon commented Aug 26, 2012

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
Copy link
Author

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

@yevon
Copy link

yevon commented Aug 27, 2012

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
Copy link
Author

Thank you, that's very helpful.

@soyangel
Copy link

soyangel commented Sep 5, 2012

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
Copy link

yevon commented Sep 9, 2012

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

@kirich1409
Copy link

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
Copy link

brk3 commented Sep 23, 2012

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
Copy link

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

brk3 added a commit to brk3/glimmr that referenced this issue Jan 8, 2014
Work around common limitation with GestureImageView when using with
ViewPager.

Requires some modifications to GestureImageView outlined at
jasonpolites/gesture-imageview#30
@longdw
Copy link

longdw commented Mar 16, 2014

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

@jonasasx
Copy link

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
Copy link

ipip2005 commented Aug 7, 2014

@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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants