我創建了一個實現GestureDetector.OnGestureListener的類。但是,我不知道如何調用這些方法。是否有類似view.setOnTouchListener()的GestureListener允許您接收事件?先進的謝謝你。這裏是我的類如何接收來自OnGestureListener的事件
公共類GestureHandler實現GestureDetector.OnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private int direction = -1;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
//Left Swipe
if(event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
direction = 270;
Log.d("Swiped: ", direction + "");
return true;
//Right Swipe
} else if(event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
direction = 90;
Log.d("Swiped: ", direction + "");
return true;
}
//Up Swipe
if(event1.getY() - event2.getY() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
direction = 0;
Log.d("Swiped: ", direction + "");
return true;
//Down Swipe
} else if(event2.getY() - event1.getY() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
direction = 180;
Log.d("Swiped: ", direction + "");
return true;
}
direction = -1;
Log.d("Swiped: ", direction + "");
return false;
}
}這裏