I have extended the image view touch and posted my code below. This code is much more similar to Android gallery: double tap always zooms out unless you are already zoomed out; if zoomed in, scrolling to next image will be disabled unless you are at edge of image. Hope someone finds this useful. (I extended the existing class and made modifications until it worked, it may not be the cleanest code :) )
public class ExtendedImageViewTouch extends ImageViewTouch {
static final float SCROLL_DELTA_THRESHOLD = 1.0f;
public ExtendedImageViewTouch(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
init();
}
public ExtendedImageViewTouch(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ExtendedImageViewTouch(Context context) {
super(context);
init();
}
private void init() {
OnTouchListener listener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getScale() > 1f) {
getParent().requestDisallowInterceptTouchEvent(true);
} else {
getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
};
setOnTouchListener(listener);
setDisplayType(DisplayType.FIT_TO_SCREEN);
}
@Override
protected float onDoubleTapPost(float scale, float maxZoom) {
if (scale!=1f) {
mDoubleTapDirection = 1;
return 1f;
}
if (mDoubleTapDirection == 1) {
mDoubleTapDirection = -1;
if ( ( scale + ( mScaleFactor * 2 ) ) <= maxZoom ) {
return scale + mScaleFactor;
} else {
mDoubleTapDirection = -1;
return maxZoom;
}
} else {
mDoubleTapDirection = 1;
return 1f;
}
}
@Override
public boolean canScroll( int direction ) {
RectF bitmapRect = getBitmapRect();
updateRect( bitmapRect, mScrollRect );
Rect imageViewRect = new Rect();
getGlobalVisibleRect( imageViewRect );
if( null == bitmapRect ) {
return false;
}
if ( Math.abs( bitmapRect.right - imageViewRect.right ) < SCROLL_DELTA_THRESHOLD ) {
if ( direction < 0 ) {
return false;
}
}
if (Math.abs( bitmapRect.left - mScrollRect.left ) < SCROLL_DELTA_THRESHOLD) {
if ( direction > 0 ) {
return false;
}
}
return true;
}
@Override
public boolean onScroll( MotionEvent e1, MotionEvent e2, float distanceX, float distanceY ) {
if ( getScale() == 1f ) return false;
if (distanceX!=0 && !canScroll((int) -distanceX)) {
getParent().requestDisallowInterceptTouchEvent(false);
return false;
} else {
getParent().requestDisallowInterceptTouchEvent(true);
mUserScaled = true;
scrollBy( -distanceX, -distanceY );
invalidate();
return true;
}
}
}