public class GameActivity extends Activity {
private static final String TAG = "GameActivity";
. . .
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
Log.d(TAG, "- onTouchEvent()");
return super.onTouchEvent(event);
}
. . .
}
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "GameView";
. . .
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
gestureDetector.onTouchEvent(event);
Log.d(TAG, "- onTouchEvent()");
return super.onTouchEvent(event);
}
private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
private static final String TAG = "GestureListener";
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(TAG, "+ onSingleTapConfirmed(event:" + event + ")");
singleTapDetected = true;
Log.d(TAG, "- onSingleTapConfirmed()");
return true;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(TAG, "+ onDoubleTap(event:" + event + ")");
doubleTapDetected = true;
Log.d(TAG, "- onDoubleTap()");
return true;
}
};
private GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener);
. . .
}
我在 AVD 上运行它并单击 SurfaceView (GameView)。
根据 LogCat 日志:
03-11 14:19:51.171: D/GameView(4839): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223172, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.171: D/GameView(4839): - onTouchEvent()
03-11 14:19:51.171: D/GameActivity(4839): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223172, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.171: D/GameActivity(4839): - onTouchEvent()
03-11 14:19:51.299: D/GameActivity(4839): + onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223303, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.299: D/GameActivity(4839): - onTouchEvent()
有几个问题:
为什么
SimpleOnGestureListener
没有被调用?(即使onTouchEvent()
被调用并且大概该行gestureDetector.onTouchEvent(event);
已被执行)为什么 GameActivity
onTouchEvent()
得到ACTION_DOWN和ACTION_UP,但GameViewonTouchEvent()
只得到ACTION_DOWN?
我尝试过的一些事情
我用与上面显示的相同的代码
GestureDetector
在类上实现了它,它按预期工作,调用GameActivity
onSingleTapConfirmed()
在 GameView 中,不是私有成员,而是
SimpleOnGestureListener
创建了一个私有成员class GestureListener extends SimpleOnGestureListener
并使用它来构造GestureDetector
. 我看到了与描述相同的行为,没有调用监听器。