我正在尝试识别图像视图上的向下滑动手势。我试过GestureDetector
了,但现在不推荐使用了。我正在使用的示例代码如下。但是,它不会进入 onFling() 方法。
GestureDetector gestureDetector;
gestureDetector = new GestureDetector(new GestureListener());
view.findViewById(R.id.imageView).setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
System.out.println("DONE-------");
updateSwipeAction();
return true;
}
return false;
}
});
class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 10;
private static final int SWIPE_THRESHOLD_VELOCITY = 10;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
//From Right to Left
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
//From Left to Right
return true;
}
if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
//From Bottom to Top
return true;
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
//From Top to Bottom
return true;
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
//always return true since all gestures always begin with onDown and<br>
//if this returns false, the framework won't try to pick up onFling for example.
return true;
}
}