我正在使用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总是,我使用缩放。
scaleFactor中的setOnScaleChangeListener仅是在特定时间的标度,与先前的值无关。我们需要计算累积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;
}

关于android - PhotoView的缩放监听器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31523247/

10-09 07:23