我正在开发一款游戏,我想在用户单次触摸屏幕时执行某些操作,而在用户长按屏幕时执行一些其他操作。为此,我创建了一个Gesture Detector类,并向其中添加事件。
表面视图类
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public MySurfaceView(Context context, AttributeSet attrs) {
gestureDetector = new GestureDetector(context, new GestureListener(this));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG,"Inside Touch Event");
float lasttouched_X, lasttouched_Y;
Card localcard;
int index=-1;
Log.d(TAG,"Inside OnTouch event");
gestureDetector.onTouchEvent(event);
return true;
}
}
手势检测器类
class GestureListener extends GestureDetector.SimpleOnGestureListener{
private static final String TAG = GestureListener.class.getSimpleName(); // To get name of class in Logging
MySurfaceView mySurfaceView;
public GestureListener(MySurfaceView paramMySurfaceView)
{
mySurfaceView=paramMySurfaceView;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d(TAG,"Inside Long Pressed event");
mySurfaceView.addTouchedCardToLongTouched(e);
}
@Override
public boolean onDown(MotionEvent e) {
// don't return false here or else none of the other
// gestures will work
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
mySurfaceView.swapSingleTouchCard(e);
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG,"Inside On Double Tap event");
return false;
}
}
OnLongPress事件仅第一次起作用,此后每当我长按触摸屏时,它就会触发onSingleTapConfirmed事件。我无法理解为什么会发生这种情况。
请让我知道为什么会这样。 预先感谢。