簡體   English   中英

ACTION_UP和ACTION_POINTER_UP事件的坐標

[英]Co-ordinates of the ACTION_UP and ACTION_POINTER_UP event

我試圖檢查每當按下屏幕,移動指針或釋放觸摸時觸摸屏幕的內容。

問題在於,釋放觸摸時,getPointerCount()會將剛離開屏幕的指針包括在其計數中。 因此,當我嘗試收集所有當前的X和Y值時,它包括一個離開屏幕的x / y對。

我目前正在做類似的事情:

int count = e.getPointerCount();
for(i = 0; i < count; i++) {
    x = e.getX(i); 
    y = e.getY(i); 
    /* do stuff with them */ 
}

有沒有一種方法可以獲取離開屏幕的指針的X和Y坐標,或者刪除離開屏幕的指針並僅獲取觸摸屏幕的坐標?

謝謝

您是否嘗試過在應用程序中使用onTouchEvent?

import android.view.MotionEvent;

...

private int mActivePointerId;

protected void onCreate(Bundle savedInstanceState){
    ...
}

public boolean onTouchEvent(MotionEvent event){

    // Get the pointer ID
    mActivePointerId = event.getPointerId(0);

    // ... Many touch events later...

    // Use the pointer ID to find the index of the active pointer 
    // and fetch its position

    int pointerIndex = event.findPointerIndex(mActivePointerId);

    // Get the pointer's current position
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);

    return true;

}

我認為您想要的方法是getActionIndex() 從文檔看來,當getActionMasked()返回ACTION_POINTER_UP ,此方法將返回相應的指針索引,然后您可以在循環中忽略它(或對其進行其他操作)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM