我正在使用Chris Bane的
PhotoView在ViewPager中显示ImageViews.我想在用户放大图像时禁用ViewPager的滚动.我已经找到了这个
Hacky ViewPager,可以通过调用toggleLock()来启用/禁用滚动.
public class HackyViewPager extends ViewPager { private boolean isLocked; public HackyViewPager(Context context) { super(context); isLocked = false; } public HackyViewPager(Context context,AttributeSet attrs) { super(context,attrs); isLocked = false; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!isLocked) { try { return super.onInterceptTouchEvent(ev); } catch (IllegalArgumentException e) { e.printStackTrace(); return false; } } return false; } @Override public boolean onTouchEvent(MotionEvent event) { return !isLocked && super.onTouchEvent(event); } public void toggleLock() { isLocked = !isLocked; } public void setLocked(boolean isLocked) { this.isLocked = isLocked; } public boolean isLocked() { return isLocked; } }
所以我的问题是,我不确定我必须在哪个位置调用toogleLock().我想我必须实现一个监听器,但我不确定哪一个是最好的.在此先感谢帮助我!
编辑:我自己想出来了!
photoview.setOnMatrixChangeListener(new PhotoViewAttacher.OnMatrixChangedListener() { @Override public void onMatrixChanged(RectF rectF) { if (photoview.getScale() > 1) { sPager.setLocked(true); } else { sPager.setLocked(false); } } });
解决方法
对我来说,photoview.getScale()== 1总是和我一起使用缩放.
setOnScaleChangeListener中的scaleFactor只是某个时间的刻度,与前一个值无关.我们需要计算累积scaleFactor.
private float cumulScaleFactor = 1; photoViewAttacher.setOnScaleChangeListener(new PhotoViewAttacher.OnScaleChangeListener() { @Override public void onScaleChange(final float scaleFactor,final float focusX,final float focusY) { cumulScaleFactor = cumulScaleFactor * scaleFactor; sPager.setLocked(isValueApproximate(1.0f,cumulScaleFactor)); } }); private boolean isValueApproximate(final float expectedValue,final float value) { final float errorMargin = 0.01f; return Math.abs(expectedValue - value) < errorMargin; }