我试图在 MotionEvent.ACTION_MOVE 事件发生时获取指针 ID。

我是通过调用 event.getActionIndex() 来实现的,但它总是为第二根、第三根、第四根和第五根手指返回 0。

我在 Galaxy S I9000 上使用 Gingerbread 2.3.3

这是我的代码

switch (event.getActionMasked()) {
case MotionEvent.ACTION_MOVE: {
    Log.d("D","  getActionIndex()="+event.getActionIndex());
    };break;
}

这是调试结果
05-02 19:20:08.628: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=1
05-02 19:20:08.781: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=1
05-02 19:20:08.820: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=1
05-02 19:20:08.914: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=1
05-02 19:20:09.070: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=2
05-02 19:20:09.187: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=2
05-02 19:20:09.324: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=2
05-02 19:20:09.460: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=2
05-02 19:20:09.523: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=2
05-02 19:20:09.542: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=2
05-02 19:20:09.679: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=3
05-02 19:20:09.703: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=3
05-02 19:20:09.847: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=3
05-02 19:20:10.117: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=3
05-02 19:20:10.261: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=4
05-02 19:20:10.281: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=4
05-02 19:20:10.304: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=4
05-02 19:20:10.371: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=4
05-02 19:20:10.410: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=4
05-02 19:20:10.433: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=4
05-02 19:20:10.519: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=4
05-02 19:20:10.558: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=4
05-02 19:20:10.613: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=3
05-02 19:20:10.640: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=2
05-02 19:20:10.656: DEBUG/D(4534): getActionIndex()=0  getPointerCount()=1

最佳答案

我正在使用此代码转储事件,并且效果很好。

/** Show an event in the LogCat view, for debugging */
private void dumpEvent(MotionEvent event) {
   String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
      "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
   StringBuilder sb = new StringBuilder();
   int action = event.getAction();
   int actionCode = action & MotionEvent.ACTION_MASK;
   sb.append("event ACTION_" ).append(names[actionCode]);
   if (actionCode == MotionEvent.ACTION_POINTER_DOWN
         || actionCode == MotionEvent.ACTION_POINTER_UP) {
      sb.append("(pid " ).append(
      action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
      sb.append(")" );
   }
   sb.append("[" );
   for (int i = 0; i < event.getPointerCount(); i++) {
      sb.append("#" ).append(i);
      sb.append("(pid " ).append(event.getPointerId(i));
      sb.append(")=" ).append((int) event.getX(i));
      sb.append("," ).append((int) event.getY(i));
      if (i + 1 < event.getPointerCount())
         sb.append(";" );
   }
   sb.append("]" );
   Log.d(TAG, sb.toString());
}

我把它从 this ZDNET article 弄下来了,所以我不能相信它。我在 Android 3.0 上运行,但示例代码最初是为早期版本的 Android 编写的,因此它也适用于您。

看起来您需要调用 getPointerId 而不是 getActionIndex。

关于Android MotionEvent.getActionIndex() 和 MultiTouch,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5860879/

10-11 22:48