缩放PhotoView的监听器

时间:2015-07-20 17:59:04

标签: android imageview android-viewpager

我使用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);
                }
            }
        });

3 个答案:

答案 0 :(得分:3)

对我来说,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;
}

答案 1 :(得分:0)

当照片比例= 1时,我没有使用HackyViewPager设置photoview.setAllowParentInterceptOnEdge(true)。

photoView.setOnMatrixChangeListener(new PhotoViewAttacher.OnMatrixChangedListener() {
  @Override
  public void onMatrixChanged(RectF rect) {
    photoView.setAllowParentInterceptOnEdge(photoView.getScale() == 1);
  }
});

答案 2 :(得分:0)

我加入了@Mashmori和@Raymond Chenon anwsers,您得到了:

photoView.setOnMatrixChangeListener {
        val isZoomedOut = isValueApproximate(view.fullScreenImageView.scale, 1f)
        photoView.setAllowParentInterceptOnEdge(isZoomedOut)
    }

private fun isValueApproximate(expectedValue: Float, value: Float): Boolean {
    val errorMargin = 0.01f
    return abs(expectedValue - value) < errorMargin
}