Android MotionEvent.getActionIndex() 和 MultiTouch

发布于 2024-11-04 06:48:10 字数 2271 浏览 7 评论 0 原文

我试图在事件 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

I am trying to get the pointer id whene the event MotionEvent.ACTION_MOVE happens.

I am doing it by calling event.getActionIndex() but it always returns 0 for the second, the third, the forth and the fifth finger.

i am using Gingerbread 2.3.3 on Galaxy S I9000

here is my code

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

This is the debug results

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

塔塔猫 2024-11-11 06:48:10

我使用 3 种方法来委托鼠标事件的责任。它在我的 2.3 HTC Desire S 上运行良好。它能够捕获多个触摸事件。

public void processMouseMove(int mouseX, int mouseY, int pid)
public void processMouseDown(int mouseX, int mouseY, int pid)
public void processMouseUp(int mouseX, int mouseY, int pid)


public boolean onTouch(View v, MotionEvent event) { 
    int p = event.getActionIndex();
    switch(event.getActionMasked()){
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            processMouseDown((int)event.getX(p), (int)event.getY(p), event.getPointerId(p));                
        break;              
        case MotionEvent.ACTION_POINTER_UP:                 
        case MotionEvent.ACTION_UP:
            processMouseUp((int)event.getX(p), (int)event.getY(p), event.getPointerId(p));
            break;
        case MotionEvent.ACTION_MOVE:
            final int historySize = event.getHistorySize();
            final int pointerCount = event.getPointerCount();
            for (int h = 0; h < historySize; h++) {
                 for (int p1 = 0; p1 < pointerCount; p1++) {
                    processMouseMove((int)event.getHistoricalX(p1, h), (int)event.getHistoricalY(p1, h), event.getPointerId(p1));
                 }
            }
            for (int p1 = 0; p1 < event.getPointerCount(); p1++) {

                processMouseMove((int)event.getX(p1), (int)event.getY(p1), event.getPointerId(p1));
            }
    }
    return true;
}

I am using 3 methods to delegate the responsibility of the mouse events. It works fine on my 2.3 HTC Desire S. it is able to capture multiple touch events.

public void processMouseMove(int mouseX, int mouseY, int pid)
public void processMouseDown(int mouseX, int mouseY, int pid)
public void processMouseUp(int mouseX, int mouseY, int pid)


public boolean onTouch(View v, MotionEvent event) { 
    int p = event.getActionIndex();
    switch(event.getActionMasked()){
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            processMouseDown((int)event.getX(p), (int)event.getY(p), event.getPointerId(p));                
        break;              
        case MotionEvent.ACTION_POINTER_UP:                 
        case MotionEvent.ACTION_UP:
            processMouseUp((int)event.getX(p), (int)event.getY(p), event.getPointerId(p));
            break;
        case MotionEvent.ACTION_MOVE:
            final int historySize = event.getHistorySize();
            final int pointerCount = event.getPointerCount();
            for (int h = 0; h < historySize; h++) {
                 for (int p1 = 0; p1 < pointerCount; p1++) {
                    processMouseMove((int)event.getHistoricalX(p1, h), (int)event.getHistoricalY(p1, h), event.getPointerId(p1));
                 }
            }
            for (int p1 = 0; p1 < event.getPointerCount(); p1++) {

                processMouseMove((int)event.getX(p1), (int)event.getY(p1), event.getPointerId(p1));
            }
    }
    return true;
}
骷髅 2024-11-11 06:48:10

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

/** 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());
}

我把它取下来这篇 ZDNET 文章,所以我不能将其归功于它。我在 Android 3.0 上运行,但示例代码最初是为早期版本的 Android 编写的,因此它也应该适合您。

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

I am using this code to dump events, and it works pretty well.

/** 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());
}

I got it off this ZDNET article, so I can't take credit for it. I am running on Android 3.0, but the example code was originally written for earlier versions of Android, so it should work for you too.

It looks like you need to call getPointerId rather than getActionIndex.

揽月 2024-11-11 06:48:10

使用 MotionEventCompat。

// get pointer index from the event object

int pointerIndex = MotionEventCompat.getActionIndex(event);

// get masked (not specific to a pointer) action

int maskedAction = MotionEventCompat.getActionMasked(event);

Use MotionEventCompat.

// get pointer index from the event object

int pointerIndex = MotionEventCompat.getActionIndex(event);

// get masked (not specific to a pointer) action

int maskedAction = MotionEventCompat.getActionMasked(event);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文