2011-07-22 47 views
0

我有一個SurfaceView,我正在畫一個圓。我知道圓的位置和半徑。Android:檢測區域是否被任何手指按下

我需要知道圓是否被用戶按下。用戶可能正在用一個或兩個手指敲擊屏幕。如果有任何手指在圓圈的區域上,則必須按下它。

當用戶從屏幕上擡起手指,並且在圓上不再有手指時,該圓不得不被按下。

當用戶只有一個手指在屏幕上時,我沒有任何問題,但是當他使用兩個手指時我無法解決問題。

我遇到的問題是,當我收到一個ACTION_UP或ACTION_POINTER_UP操作時,我不知道哪一個指針不再在屏幕上,所以我不必看看它們的座標是否在圓上。

我已經做了幾次嘗試都沒有成功,最後一個是:

protected boolean checkPressed(MotionEvent event) { 

    ColourTouchWorld w = (ColourTouchWorld)gameWorld; 

    int actionMasked = event.getActionMasked(); 

    for (int i = 0; i < event.getPointerCount(); i++) { 
     if (i == 0 && (actionMasked == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_POINTER_UP)) { 
      // the pointer with index 0 is no longer on screen, 
      // so the circle is not pressed by this pointer, even if 
      // it's coordinates are over the area of the circle 

      continue; 
     } 

     if (isPointInCicle(event.getX(i)), event.getY(i))) { 
      return true; 
     } 
    } 

    return false; 
} 

任何想法?謝謝。

回答

1

您需要使用ACTION_POINTER_INDEX_MASK常數。我從來沒有實現過,所以我不知道代碼的外觀。但我認爲你將需要使用這個。

+0

是的,這很好!我假設收到的操作引用索引爲0的指針。我錯了,我需要使用ACTION_POINTER_INDEX_MASK。 – GaRRaPeTa

2

在寫在我是假設該操作接收提到的指針與索引0我錯問題的方法,但我需要使用ACTION_POINTER_INDEX_MASK

正確執行該方法的是:

protected boolean checkPressed(MotionEvent event) { 

ColourTouchWorld w = (ColourTouchWorld)gameWorld; 

int actionMasked = event.getActionMasked(); 
int pointerIndex = ((event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT); 

for (int i = 0; i < event.getPointerCount(); i++) { 
    if (i == pointerIndex && (actionMasked == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_POINTER_UP)) { 
     // the pointer with index 0 is no longer on screen, 
     // so the circle is not pressed by this pointer, even if 
     // it's coordinates are over the area of the circle 

     continue; 
    } 

    if (isPointInCicle(event.getX(i)), event.getY(i))) { 
     return true; 
    } 
} 

return false; 
}