6
我試圖實現一個可拖動的按鈕,它也應該是可以扔掉的。Drag and Drop + Fling Detector not working
不幸的是,系統在拖動開始後停止發送MotionEvents。因此GestureDetector.OnGestureListener.onFling()方法永遠不會被調用。
有沒有辦法在阻塞系統消耗這些事件之前攔截這些事件?
我也試圖創建自己的FlingDetector,但它不是在不同的設備和屏幕密度可靠:
public class FlingDetector {
private final int MIN_FLING_SPEED = 3;
private OnFlingListener mOnFlingListener;
private float mCurrentX = 0;
private float mCurrentY = 0;
private long mLastMovementTime = 0;
private double mCurrentVelocity = 0;
private final float mDensity;
public FlingDetector(OnFlingListener onFlingListener, Context context) {
mOnFlingListener = onFlingListener;
mDensity = context.getResources().getDisplayMetrics().density;
}
public void onMovementStart(float x, float y) {
mCurrentX = x;
mCurrentY = y;
mLastMovementTime = System.currentTimeMillis();
mCurrentVelocity = 0;
}
public void onMovementEnd(float x, float y) {
long currentTime = System.currentTimeMillis();
float distanceX = Math.abs(mCurrentX - x)/mDensity;
float distanceY = Math.abs(mCurrentY - y)/mDensity;
float distance = (float) Math.sqrt(Math.pow(distanceX, 2) +
Math.pow(distanceY, 2));
mCurrentVelocity = (distance/(currentTime - mLastMovementTime));
if(mCurrentVelocity > MIN_FLING_SPEED) {
mOnFlingListener.onFling((int) (mCurrentVelocity + 0.5));
} else {
Log.d("test", "Distance: " + distance);
Log.d("test", "Time Delta: " + (currentTime - mLastMovementTime));
Log.d("test", "Speed: " + mCurrentVelocity);
}
}
public interface OnFlingListener {
void onFling(int speed);
}
}
我在庫中實現了類似的東西。請參閱'GestureDetectorCompat' [here](https://github.com/ericbhatti/floaties/blob/master/FloatiesLibrary/floaties/src/main/java/com/bezyapps/floatieslibrary/Floaty.java)。希望能幫助到你。 –
@timoschloesser嘗試訪問位置,而拖動....,如果位置變化非常快。它被絆倒。嘗試使用getAction()和ACTION_DRAG_LOCATION。我不知道你如何測量速度。 –