4
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()

有几个问题:

  1. 为什么SimpleOnGestureListener没有被调用?(即使onTouchEvent()被调用并且大概该行gestureDetector.onTouchEvent(event);已被执行)

  2. 为什么 GameActivityonTouchEvent()得到ACTION_DOWNACTION_UP,但GameViewonTouchEvent()只得到ACTION_DOWN

我尝试过的一些事情

  • 我用与上面显示的相同的代码GestureDetector在类上实现了它,它按预期工作,调用GameActivityonSingleTapConfirmed()

  • 在 GameView 中,不是私有成员,而是SimpleOnGestureListener创建了一个私有成员class GestureListener extends SimpleOnGestureListener并使用它来构造GestureDetector. 我看到了与描述相同的行为,没有调用监听器。

4

1 回答 1

8

我将代码调试到android源代码GestureDetector类中。这解释了为什么SimpleOnGestureListener没有被调用:

  • 接收到 ACTION_DOWN 并生成内部 TAP 消息给 GestureDetector 消息处理程序。
  • onSingleTapConfirmed()仅当事件停止时,处理程序才调用侦听器( !mStillDown)
  • 该事件仍处于关闭状态,因为从未收到 ACTION_UP 事件。

所以你看,这两个问题是相关的,当我找出为什么没有发出 ACTION_UP 我会解决这个问题!

编辑

这完成了答案,现在它正在工作。

  • GameView中的调用super.onTouchEvent()返回false,因为superSurfaceView的类是View,所以返回false。这就是为什么 ACTION_UP 永远不会被调用
  • GameActivity 中的调用super.onTouchEvent()返回true,这就是在 GameActivity 上发出 ACTION_UP 的原因。

注意:如果我设置 GameView.setclickable(true) super.onTouchEvent() 返回 true. 这也是一个可以接受的解决方案

现在我的 GameView 代码是:

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
    gestureDetector.onTouchEvent(event);
    Log.d(TAG, "- onTouchEvent()");
    return true;
}

当前的 LogCat 日志是:

03-11 16:32:06.629: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=977.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.629: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.641: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=978.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.641: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.772: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=978.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158772, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.772: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.931: D/GestureListener(5316): + onSingleTapConfirmed(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=977.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.931: D/GestureListener(5316): - onSingleTapConfirmed()
于 2013-03-11T16:38:01.850 回答