【问题标题】:Android Multi-touch: Pointers with index > 0 not triggering event.Action_MoveAndroid多点触控:索引> 0的指针不触发event.Action_Move
【发布时间】:2015-02-09 19:14:18
【问题描述】:

在我的多点触控程序中,我遇到了运行时错误,其中指针在屏幕上滑动的多点触控逻辑 (MotionEvent.ACTION_MOVE) 仅由初始指针输入。这意味着我的 case ACTION_MOVE 的 switch 语句中的代码只能由触摸屏幕的第一根手指访问,即指针索引 = 0。以下是我的代码的相关部分:

public boolean onTouchEvent(MotionEvent event) {
        // get pointer index from the event object
        int pointerIndex = event.getActionIndex();
        // get pointer ID
        int pointerId = event.getPointerId(pointerIndex);
        // get masked (not specific to a pointer) action
        int maskedAction = event.getActionMasked();

        switch (maskedAction) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN: {
            // We have a new pointer. Lets add it to the list of pointers
            PointF f = new PointF();
            f.x = event.getX(pointerIndex);
            f.y = event.getY(pointerIndex);
            mActivePointers.put(pointerId, f);
            Log.d("Multitouch", "x coord = " + Float.toString(f.x));
            Log.d("Multitouch", "y coord = " + Float.toString(f.y));
            break;
        }
        case MotionEvent.ACTION_MOVE: { // a pointer was moved
            Log.e("Multitouch", "Pointer"+ Integer.toString(pointerId) +" is moving");
            for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                PointF f = mActivePointers.get(event.getPointerId(i));
                if (f != null) {
                    f.x = event.getX(i);
                    f.y = event.getY(i);
                }
            }
            break;
        }
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL: {
            mActivePointers.remove(pointerId);
            break;
        }
        }
        invalidate();
        return true;
    }

不用说,我希望 Action_Move 中的代码由任何指针触发,而不仅仅是初始指针。是什么阻止超出索引零的指针触发移动事件?

【问题讨论】:

    标签: android multi-touch


    【解决方案1】:

    如文档中所述,MotionEvent.getActionIndex() 仅返回 ACTION_POINTER_UP 和 ACTION_POINTER_DOWN (http://developer.android.com/reference/android/view/MotionEvent.html#getActionIndex%28%29) 的索引。但是,您在日志输出中使用由所述函数返回的指针索引生成的指针 ID。因此,日志不正确,因为函数没有返回您期望的结果。

    【讨论】:

    • 对不起,我不太明白你想说什么。即使 getActionIndex() 仅在 ACTION_POINTER_UP 和 ACTION_POINTER_DOWN 内有效,其他指针不应该仍然能够进入 ACTION_MOVE 代码吗?
    • 是的,'ACTION_MOVE' 应该适用于所有指针,我是说它正在工作,你的调试日志只是输出它不是。在我的项目中,它使用循环遍历所有指针,就像你做的那样。
    • 它肯定行不通。我根据指针所在的坐标播放不同的声音,现在只有第一个指针(索引 0)在移动到新坐标时动态改变播放的声音。其余的继续播放他们在触发 Action_POINTER_Down 时触摸的坐标的声音。
    • 我真的不知道出了什么问题,因为它对我来说很好用。恐怕您将不得不提出一个新问题并再次发布您的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 2011-09-06
    相关资源
    最近更新 更多