在第 71 行,我不断收到 pointerIndex out of range 错误。我是使用 MultiTouch 的新手,并且看过许多处理指针 id 和指针索引的示例和教程,但我仍然对此感到有些困惑。我还查看了一些关于pointerIndex out of range Threads 的帖子,我认为这些帖子可以消除一些混乱,但没有运气。我正在尝试进行拖动和捏合缩放此处列出的代码是我尝试执行的操作的示例。
这是我的 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)