Android MotionEvent.getActionIndex() 和多点触控

标签 android events multi-touch motion

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

最佳答案

我正在使用 3 种方法来委托(delegate)鼠标事件的责任。它在我的 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;
}

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

相关文章:

ios - 如何让这些方法适用于多点触控?

android - 等待android中的多点触控输入

android - 无法连接到 ADB - 使用 CMD 连接 ADB,无线应用程序调试测试

events - GWT eventbus 处理多个模块

c# - 什么是轻量级事件?

windows - 如何注册为 NT 内核事件的实时 ETW 使用者?

android - 多点触控 : Get Action depending on PointerId

android - fragment 通信 - fragment 到 fragment

android - 从 VS2013 C++ 代码创建 APK 文件

android - Inmobi 与 Android APP 集成不显示广告