2016-05-31 58 views
1

我試圖獲取指針列表,無論它們是否已關閉,以及它們在屏幕上的像素位置,因此我可以將我的桌面遊戲移植到android。爲此,我寫了這個onTouch處理函數。然而Android多點觸摸 - TouchMove事件中的IllegalArgumentException

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,我只想要一個指針的原始列表,這些事件組成我的引擎列表中的下一幀。

任何人指出我的問題在哪裏?

回答

1

要調用從一個單獨的(後)線程e.getX()e.getY() - 這很可能是MotionEvent對象的內部狀態有onTouch()回調和執行的線程之間變化。

只應承擔MotionEvent對象是onTouch()方法的過程中有效和檢索getX()getY()值傳遞給線程之前的方法退出。

+0

正是!感謝您指出了這一點。 –