首页 > 解决方案 > 如何覆盖 boolean PhotoViewAttacher onFling(MotionEvent, MotionEvent, float, float) 的限制?

问题描述

PhotoViewAttacher' 会boolean onFling(MotionEvent, MotionEvent, float, float)阻止调用 myOnSingleFlingListeneronFling方法,除非当前比例是最小值。

OnSingleFlingListenercom.github.chrisbanes.photoview. 但是,由于该onFling方法仅对包可见,因此首先调用PhotoViewAttacher' 的方法。防止我的方法在. 我需要我的,除非。(当我的图像宽度与窗口宽度匹配时。)我该如何解决这个问题?我需要自己制作整个包的副本并修改为公开吗?onFlingPhotoViewAttacher.onFlingonFlingscale > DEFAULT_MIN_SCALEscale > getMediumScale()scale == getMediumScale()PhotoViewOnSingleFlingListener.onFling

我的代码:

public class BasicViewActivity extends AppCompatActivity implements DownloadCallback, OnSingleFlingListener {
@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        . . . 
        return true;
    }

OnSingleFlingListener:

package com.github.chrisbanes.photoview;
public interface OnSingleFlingListener {
    boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
}

PhotoViewAttacher:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (mSingleFlingListener != null) {
        if (getScale() > DEFAULT_MIN_SCALE) {
            return false;
        }
        . . . 
        return mSingleFlingListener.onFling(e1, e2, velocityX, velocityY);
    }
    return false;
}

我想要我的onFling被叫时getScale() <= getMediumScale()。相反,我的onFling方法仅在getScale() == DEFAULT_MIN_SCALE.

标签: javaandroidandroid-photoview

解决方案


我决定解决它。

  1. 我克隆了 PhotoView 包。
  2. 我将它的源代码复制到我自己的包中。
  3. 我更改了上面提到的一行(并添加了一个分数以防止浮点错误):

    if(getScale() > getMediumScale() + .01f) {
        return false;
    }
    

推荐阅读