【问题标题】:Android multi touch - IllegalArgumentException in TouchMove eventAndroid 多点触控 - TouchMove 事件中的 IllegalArgumentException
【发布时间】:2016-05-31 18:15:49
【问题描述】:

我正在尝试获取指针列表,它们是否向下以及它们在屏幕上的像素位置,以便我可以将我的桌面游戏移植到 android。为此,我编写了这个 onTouch 处理程序。

private boolean onTouch(View v, MotionEvent e)
{
    final int action = e.getActionMasked();

    switch (action)
    {
        case MotionEvent.ACTION_DOWN:
            surfaceView.queueEvent(() -> postTouchEvent(FINGER_0, true, e.getX(), e.getY()));
            break;

        case MotionEvent.ACTION_UP:
            surfaceView.queueEvent(() -> postTouchEvent(FINGER_0, false, e.getX(), e.getY()));
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_POINTER_UP:
        {
            final int index = e.getActionIndex();
            final int finger = index + 1;

            if (finger < FINGER_1 || finger > FINGER_9)
                break;

            final boolean isDown = action == MotionEvent.ACTION_POINTER_DOWN;
            surfaceView.queueEvent(() -> postTouchEvent(finger, isDown, e.getX(), e.getY()));
        }
        break;

        case MotionEvent.ACTION_MOVE:
            for (int i = 0; i < e.getPointerCount(); i++)
            {
                final int finger = i + 1;

                if (finger < FINGER_0 || finger > FINGER_9)
                    break;

                surfaceView.queueEvent(() ->
                        postTouchEvent(finger, true, e.getX(finger - 1), e.getY(finger - 1)));
            }
            for (int i = e.getPointerCount(); i < FINGER_9; i++)
            {
                final int finger = i + 1;
                surfaceView.queueEvent(() -> postTouchEvent(finger, false, 0, 0));
            }
            break;
    }

    return true;
}

然而,问题在于ACTION_MOVE 事件,我得到了一个IllegalArgumentException 来访问我的索引ID。仅当我一次在屏幕上点击三个或更多手指时才会发生这种情况,但这仍然是一个问题。例外情况如下。

FATAL EXCEPTION: GLThread 61026
Process: com.shc.silenceengine.tests.android, PID: 23077
java.lang.IllegalArgumentException: pointerIndex out of range
    at android.view.MotionEvent.nativeGetAxisValue(Native Method)
    at android.view.MotionEvent.getX(MotionEvent.java:2014)
    at com.shc.silenceengine.backend.android.AndroidInputDevice.lambda$onTouch$14(AndroidInputDevice.java:228)
    at com.shc.silenceengine.backend.android.AndroidInputDevice.access$lambda$6(AndroidInputDevice.java)
    at com.shc.silenceengine.backend.android.AndroidInputDevice$$Lambda$7.run(Unknown Source)
    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1462)
    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)

我不确定为什么会出现错误,因为我执行的 for 循环只到了 e.getPointerCount(),所以索引不可能超出代码。

我不想跟踪指针 ID,我只想要一个原始指针列表,这些事件在我的引擎列表中组成到下一帧。

谁能指出问题出在哪里?

【问题讨论】:

    标签: java android input multi-touch touchscreen


    【解决方案1】:

    您正在从单独的(后来的)线程调用 e.getX() 和 e.getY() - MotionEvent 对象的内部状态可能在 onTouch() 回调和线程执行之间发生了变化。

    您应该只假设 MotionEvent 对象在 onTouch() 方法期间有效,并检索 getX() 和 getY() 的值以传递给线程在方法退出之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多