Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A viewpager which works with ImageViewTouch (https://github.com/sephiroth74/ImageViewZoom)
package some.awesome.package;
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class ImageViewTouchViewPager extends ViewPager {
private static final String TAG = "ImageViewTouchViewPager";
public static final String VIEW_PAGER_OBJECT_TAG = "image#";
private int previousPosition;
private OnPageSelectedListener onPageSelectedListener;
public ImageViewTouchViewPager(Context context) {
super(context);
init();
}
public ImageViewTouchViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setOnPageSelectedListener(OnPageSelectedListener listener) {
onPageSelectedListener = listener;
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ImageViewTouch) {
return ((ImageViewTouch) v).canScroll(dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
public interface OnPageSelectedListener {
public void onPageSelected(int position);
}
private void init() {
previousPosition = getCurrentItem();
setOnPageChangeListener(new SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (onPageSelectedListener != null) {
onPageSelectedListener.onPageSelected(position);
}
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == SCROLL_STATE_SETTLING && previousPosition != getCurrentItem()) {
try {
ImageViewTouch imageViewTouch = (ImageViewTouch)
findViewWithTag(VIEW_PAGER_OBJECT_TAG + getCurrentItem());
if (imageViewTouch != null) {
imageViewTouch.zoomTo(1f, 300);
}
previousPosition = getCurrentItem();
} catch (ClassCastException ex) {
Log.e(TAG, "This view pager should have only ImageViewTouch as a children.", ex);
}
}
}
});
}
}
@torbenkikkert

This comment has been minimized.

Copy link

@torbenkikkert torbenkikkert commented Oct 1, 2012

Hi

I am trying to use your viewpager together with it.sephiroth.android.library.imagezoom.ImageViewTouch. But on:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pictures);

I get an inflate exception. I am using this XML

<GridView
    android:id="@+id/pictures_gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:listSelector="@null"
    android:numColumns="4" >

</GridView>

<ImageViewTouchViewPager
    android:id="@+id/pictures_viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:visibility="gone" />

It works fine if I use the normal android.support.v4.view.ViewPager ? Any suggestions ?

@loeschg

This comment has been minimized.

Copy link

@loeschg loeschg commented Feb 10, 2014

Fantastic. This just saved me a good deal of time. Thanks for posting!

@nativ18

This comment has been minimized.

Copy link

@nativ18 nativ18 commented Apr 2, 2014

@tk421dk You should add your package name to the view name in the xml file.
e.g - com.app.ImageViewTouchViewPager

@ulx

This comment has been minimized.

Copy link

@ulx ulx commented Nov 17, 2014

To zoom need to fix the method
@override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ImageViewTouch) {
return ((ImageViewTouch) v).canScroll(dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
to
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ImageViewTouch) {
if (((ImageViewTouch)v).getScale() == ((ImageViewTouch)v).getMinScale()) {
return super.canScroll(v, checkV, dx, x, y);
}
return ((ImageViewTouch) v).canScroll(dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}

@dimsuz

This comment has been minimized.

Copy link

@dimsuz dimsuz commented Sep 3, 2015

@ulx thanks, your solution is simpler and it hits the problem right into solving it :)
Though the right fix would be in the library itself, it seems to incorrectly calculate bitmap offsets or something like this in canScroll()

@g3ntleman

This comment has been minimized.

Copy link

@g3ntleman g3ntleman commented Mar 8, 2016

Please add ulx's code to the example or (better) to the canScroll() code in ImageViewTouch. It works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment