2012-03-12 63 views
2

我正在尋找一種在我的應用程序中實現多點觸控拖放的方法。 我有很多意見,我希望第一個用戶在拖動視圖的同時拖動另一個視圖。 (我的視圖正在實現這個監聽器,onDragStart記錄了被觸摸的點,onDragContinuing改變視圖的位置,onDrop檢查位置是否有效)我的單觸式拖放效果很好,這裏是我的代碼的一部分:在Android中使用多點觸控實現拖放

public boolean onTouch(View v, MotionEvent event) { 

    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     return onDragStart(event.getX(), event.getY()); 
    } 

    else if (event.getAction() == MotionEvent.ACTION_MOVE){ 
     return onDragContinuing(event.getRawX(), event.getRawY());      
    } 

    else if (event.getAction() == MotionEvent.ACTION_UP){ 
     return onDrop(event.getRawX(), event.getRawY()); 
    } 

    else{ 
     return false; 
    } 

} 

我試圖實現這樣的多點觸控,但它不工作:

private static final int INVALID_POINTER_ID = -1; 
// The ‘active pointer’ is the one currently moving our object. 
private int mActivePointerId = INVALID_POINTER_ID; 

public boolean onTouch(View v, MotionEvent event) { 

    int action = event.getAction(); 
    int actionCode = action & MotionEvent.ACTION_MASK; 

    switch(actionCode){ 

     case MotionEvent.ACTION_DOWN :{ 
      Toast.makeText(getContext(), "down", Toast.LENGTH_SHORT).show(); 
      // Save the ID of this pointer 
      mActivePointerId = event.getPointerId(0); 
      return onDragStart(event.getX(), event.getY()); 
     } 

     case MotionEvent.ACTION_POINTER_DOWN :{ 
      Toast.makeText(getContext(), "pointer down", Toast.LENGTH_SHORT).show(); 
      // Save the ID of this pointer 
      // Extract the index of the pointer that left the touch sensor 
      final int pointerIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; 
      mActivePointerId = event.getPointerId(pointerIndex); 
      return onDragStart(event.getX(mActivePointerId), event.getY(mActivePointerId)); 
     } 

     case MotionEvent.ACTION_MOVE :{ 
      Toast.makeText(getContext(), "move", Toast.LENGTH_SHORT).show(); 
      final int pointerIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; 
      final int mCurrentPointerId = event.getPointerId(pointerIndex); 
      if (mActivePointerId == mCurrentPointerId){ 
       return onDragContinuing(getRawX(event, mActivePointerId), getRawY(event, mActivePointerId)); 
      } 
      else return false; 
     } 

     case MotionEvent.ACTION_UP :{ 
      Toast.makeText(getContext(), "up", Toast.LENGTH_SHORT).show(); 
      final int pointerIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; 
      final int mCurrentPointerId = event.getPointerId(pointerIndex); 

      if (mActivePointerId == mCurrentPointerId){ 
       return onDrop(event.getRawX(), event.getRawY()); 
      } 
      else return false; 

     } 

     case MotionEvent.ACTION_POINTER_UP :{ 
      Toast.makeText(getContext(), "pointer up", Toast.LENGTH_SHORT).show(); 
      final int pointerIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; 
      final int mCurrentPointerId = event.getPointerId(pointerIndex); 

      if (mActivePointerId == mCurrentPointerId){ 
       return onDrop(getRawX(event, pointerIndex), getRawY(event, pointerIndex)); 
      } 
      else return false; 

     } 

     default :{ 
      return false; 
     } 

    } 

} 

/** 
* MotionEvent has no getRawX(int) method; simulate it pending future API approval. 
*/ 
private static float getRawX(MotionEvent event, int pointerIndex) { 
    float offset = event.getX() - event.getRawX(); 
    return event.getX(pointerIndex) + offset; 
} 

/** 
* MotionEvent has no getRawY(int) method; simulate it pending future API approval. 
*/ 
private static float getRawY(MotionEvent event, int pointerIndex) { 
    float offset = event.getY() - event.getRawY(); 
    return event.getY(pointerIndex) + offset; 
} 

有什麼不對?你可以幫我嗎 ?謝謝 !

回答

2

我找到了解決方案和解釋。

Android不支持多視圖中的多點觸控,多點觸控僅適用於一個視圖。因此,爲了實現多視圖多點觸控,我們必須在主要活動中捕捉觸摸事件併爲子視圖生成新事件。

感謝Pascal Welsch和他的博客(http://www.passsy.de/multitouch-for-all-views/),並修改了我找到的代碼中的錯誤(http://code.google.com/p/friendbattle/issues/detail?id=19),我已經解決了我的問題。

希望這會對其他人有所幫助:-)