我正在尝试使用ViewPager实现一个图库.此外,为了实现缩放功能,我使用的是
github的
TouchImageView.我也试过使用
ZoomableImageView.
但是,问题是,如果我放大图像&如果我滚动图像,而不是图像,ViewPager正在滚动& ViewPager的下一个视图正在加载.
如果我缩放图像,如果我滚动,则图像必须移动而不是ViewPager
只有到达缩放图像的末尾,ViewPager的下一个视图才能加载.怎么做?
我不能找到那个.如果我触摸&沿着对角线拖动图像,只有图像被移动.或者ViewPager的拖动被调用.
ps:这不重复.缩放完成.但问题是图像缩放后.
解决方法
是的,我也碰到过与TouchImageView相同的问题.
当我的观点得到关注时,我解决了这个问题,我所做的是禁用了ViewPager.
public class EnableDisableViewPager extends ViewPager { private boolean enabled = true; public EnableDisableViewPager(Context context,AttributeSet attrs) { super(context,attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent arg0) { if(enabled) return super.onInterceptTouchEvent(arg0); return false; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } }
所以在TouchImageView中实现你的监听器来触发一个事件,无论是放大还是拖动.
将侦听器设置为您的Activity中的视图对象.所以当这些事件发生时,只是禁用视图寻呼机.
注意:您还需要一个鼠标向上的事件来启用查看器.
EDITED
这将仅适用于缩放,因此ViewPager可以滑动您应该缩小到原始页面的页面.
public class TouchImageView extends ImageView { ... private TouchEventListener touchEventListener; private void sharedConstructing(Context context) { ... setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v,MotionEvent event) { ... case MotionEvent.ACTION_UP: ... if(touchEventListener != null) { if(saveScale == 1.0) touchEventListener.onZoomToOriginal(); else touchEventListener.onZoom(); } break; ... } ... } }); } ... public TouchEventListener getTouchEventListener() { return touchEventListener; } public void setTouchEventListener(TouchEventListener touchEventListener) { this.touchEventListener = touchEventListener; } public interface TouchEventListener { void onZoom(); void onZoomToOriginal(); } }
更好的解决方案
我们可以通过使用下面给出的方法将ViewPager扩展到新的类来实现.
requestDisallowInterceptTouchEvent(true);
与此同时,我们可以滑动,而不会缩小到原始位置,我们在Gallery和许多其他应用程序中看到.
public class TouchImageView extends ImageView { ... private void stopInterceptEvent() { getParent().requestDisallowInterceptTouchEvent(true); } private void startInterceptEvent() { getParent().requestDisallowInterceptTouchEvent(false); } private void sharedConstructing(Context context) { super.setClickable(true); this.context = context; mScaleDetector = new ScaleGestureDetector(context,new ScaleListener()); matrix.setTranslate(1f,1f); m = new float[9]; setImageMatrix(matrix); setScaleType(ScaleType.MATRIX); setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v,MotionEvent event) { mScaleDetector.onTouchEvent(event); matrix.getValues(m); float x = m[Matrix.MTRANS_X]; float y = m[Matrix.MTRANS_Y]; PointF curr = new PointF(event.getX(),event.getY()); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: last.set(event.getX(),event.getY()); start.set(last); mode = DRAG; stopInterceptEvent(); break; case MotionEvent.ACTION_MOVE: if (mode == DRAG) { float deltaX = curr.x - last.x; float deltaY = curr.y - last.y; float scaleWidth = Math.round(origWidth * saveScale); float scaleHeight = Math.round(origHeight * saveScale); if (scaleWidth < width) { deltaX = 0; if (y + deltaY > 0) deltaY = -y; else if (y + deltaY < -bottom) deltaY = -(y + bottom); } else if (scaleHeight < height) { deltaY = 0; if (x + deltaX > 0) deltaX = -x; else if (x + deltaX < -right) deltaX = -(x + right); } else { if (x + deltaX > 0) deltaX = -x; else if (x + deltaX < -right) deltaX = -(x + right); if (y + deltaY > 0) deltaY = -y; else if (y + deltaY < -bottom) deltaY = -(y + bottom); } if(deltaX == 0) startInterceptEvent(); else stopInterceptEvent(); matrix.postTranslate(deltaX,deltaY); last.set(curr.x,curr.y); } break; case MotionEvent.ACTION_UP: mode = NONE; int xDiff = (int) Math.abs(curr.x - start.x); int yDiff = (int) Math.abs(curr.y - start.y); if (xDiff < CLICK && yDiff < CLICK) performClick(); startInterceptEvent(); break; case MotionEvent.ACTION_POINTER_UP: mode = NONE; break; } setImageMatrix(matrix); invalidate(); return true; // indicate event was handled } }); } }