Skip to content

Instantly share code, notes, and snippets.

@MFlisar
Last active May 27, 2016 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MFlisar/1ff2bc8bff94eabb52683ee53bf649eb to your computer and use it in GitHub Desktop.
Save MFlisar/1ff2bc8bff94eabb52683ee53bf649eb to your computer and use it in GitHub Desktop.
ViewPager for Gesture Image View
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.alexvasilkov.gestures.views.GestureImageView;
public class CustomViewPager extends ViewPager
{
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof GestureImageView)
{
GestureImageView giv = (GestureImageView)v;
float minZoom = giv.getController().getStateController().getEffectiveMinZoom();
float zoom = giv.getController().getState().getZoom();
float eps = 0.001f;
// if zoom factor == min zoom factor => just let the view pager handle the scroll
// L.d(this, L.logValues("minZoom", minZoom, "zoom", zoom, "dx", dx));
if (Math.abs(minZoom - zoom) < eps)
return false;
RectF effectiveMovementArea = new RectF();
giv.getController().getStateController().getEffectiveMovementArea(effectiveMovementArea, giv.getController().getState());
float stateX = giv.getController().getState().getX();
float width = effectiveMovementArea.width();
// float distToLeftEdge = Math.abs(stateX);
// float distToRightEdge = Math.abs(stateX + width);
// L.d(this, L.logValues("effectiveMovementArea", effectiveMovementArea, "width", width, "stateX", stateX, "distToLeftEdge", distToLeftEdge, "distToRightEdge", distToRightEdge));
// if user reached left edge && is swiping left => just let the view pager handle the scroll
if (Math.abs(stateX) < eps && dx > 0)
return false;
// if user reached right edge && is swiping right => just let the view pager handle the scroll
if (Math.abs(stateX + width) < eps && dx < 0)
return false;
return true;
}
else
return super.canScroll(v, checkV, dx, x, y);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// only handle one finger touches! otherwise, the user probably does want to scale/pan
if (event.getPointerCount() != 1)
return false;
return super.onTouchEvent(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment