我正在使用以下代码实现手势检测的表面视图。
surfaceview.setOnTouchListener(new OnSwipeTouchListener(this ) {
});
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
//Toast.makeText(getApplicationContext(),"On Down press", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
return result;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
Log.d("g","g");
Toast.makeText(getApplicationContext(),"On long press", Toast.LENGTH_SHORT).show();
Log.d("h","h");
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1,
float arg2, float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(),"On Show press", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTap(MotionEvent arg0) {
return true;
}
}
}
我想在用户触摸屏幕后执行操作并在那里保留一段时间。我认为这样的行动应该进入onLongPress
方法。然而,当用户在第一次敲击之后,即在第二次敲击时长按时,启动方法内的吐司。这是这个方法应该如何工作还是我犯了一些错误?