了解Android中的MultiTouch / PointerIndex / PointerId?

时间:2013-07-23 07:45:17

标签: java android multi-touch

我一直在第71行上得到一个指针超出范围的错误。我是新手使用MultiTouch并查看了许多处理指针ID和指针索引的示例和教程,但我仍然有点困惑。我也在这里查看了一些关于pointerIndex超出范围Threads的postsing,我认为这会消除一些混乱,但没有运气。我正在尝试进行拖动和缩放缩放此处列出的代码是我尝试做的一个示例。

这是我的onTouchEvent代码:

@Override
public boolean onTouch(View v, MotionEvent e) {
    int action = e.getAction() & MotionEvent.ACTION_MASK;
    final int INVALID_POINTER_ID = -1;
    int DRAG = 0;
    int ZOOM = 1;
    int NONE = 2;
    float X;
    float Y;
    int gesture = NONE;
    int dragPointerId = INVALID_POINTER_ID;
    int zoomPointerId = INVALID_POINTER_ID;
    int dragPointerIndex;
    int zoomPointerIndex;

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        gesture = DRAG;
        dragPointerId = e.getPointerId(0);
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        gesture = ZOOM;
        zoomPointerId = e.getPointerId(1);
        break;
    case MotionEvent.ACTION_UP:
        gesture = NONE;
        dragPointerId = INVALID_POINTER_ID;

        break;
    case MotionEvent.ACTION_POINTER_UP:
        gesture = NONE;
        zoomPointerId = INVALID_POINTER_ID;
        break;

    case MotionEvent.ACTION_MOVE:
        if (gesture == ZOOM) {
            zoomPointerIndex = e.findPointerIndex(zoomPointerId);
            X = e.getX(zoomPointerIndex);
            Y = e.getY(zoomPointerIndex);
            Coords2.setText(X + "," + Y);
        } else if (gesture == DRAG) {
            dragPointerIndex = e.findPointerIndex(dragPointerId);
            X = e.getX(dragPointerIndex);/////////////////////// Line 71
            Y = e.getY(dragPointerIndex);
            Coords1.setText(X + "," + Y);
        }
        break;
    }

    return true;
}

这是logcat错误:

    07-23 03:16:21.915: E/AndroidRuntime(23096): FATAL EXCEPTION: main
07-23 03:16:21.915: E/AndroidRuntime(23096): java.lang.IllegalArgumentException: pointerIndex out of range
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.MotionEvent.nativeGetAxisValue(Native Method)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.MotionEvent.getX(MotionEvent.java:1974)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at com.example.touchex.MainActivity.onTouch(MainActivity.java:71)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.View.dispatchTouchEvent(View.java:5565)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2084)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1542)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.app.Activity.dispatchTouchEvent(Activity.java:2483)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2031)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.View.dispatchPointerEvent(View.java:5750)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2910)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2486)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:853)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2495)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.os.Looper.loop(Looper.java:137)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at android.app.ActivityThread.main(ActivityThread.java:4586)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at java.lang.reflect.Method.invokeNative(Native Method)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at java.lang.reflect.Method.invoke(Method.java:511)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-23 03:16:21.915: E/AndroidRuntime(23096):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案