簡體   English   中英

如何在Android touchlistener中同時檢測到觸摸屏幕上的次要手指?

[英]how to detect the secondary finger on touching the screen not at same time in android touchlistener?

如何檢測屏幕上的其他手指? 例如,我用一根手指觸摸屏幕,一段時間后我將第一根手指放在屏幕上,然后用另一根手指觸摸屏幕,同時保持第一根手指的原樣? 如何在Touch Listener中檢測秒針觸摸?

從第二個手指開始發送MotionEvent.ACTION_POINTER_DOWN and MotionEvent.ACTION_POINTER_UP 對於第一個手指MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP使用MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP

getPointerCount()方法MotionEvent允許您確定在裝置上的指針的數目。 所有事件和指針的位置都包含在您在onTouch()方法中收到的onTouch()實例中。

要跟蹤來自多個指針的觸摸事件,您必須使用MotionEvent.getActionIndex()MotionEvent.getActionMasked()方法來標識指針的索引和此指針發生的觸摸事件。

    int action = MotionEventCompat.getActionMasked(event);
// Get the index of the pointer associated with the action.
int index = MotionEventCompat.getActionIndex(event);
int xPos = -1;
int yPos = -1;

Log.d(DEBUG_TAG,"The action is " + actionToString(action));

if (event.getPointerCount() > 1) {
    Log.d(DEBUG_TAG,"Multitouch event"); 
    // The coordinates of the current screen contact, relative to 
    // the responding View or Activity.  
    xPos = (int)MotionEventCompat.getX(event, index);
    yPos = (int)MotionEventCompat.getY(event, index);

} else {
    // Single touch event
    Log.d(DEBUG_TAG,"Single touch event"); 
    xPos = (int)MotionEventCompat.getX(event, index);
    yPos = (int)MotionEventCompat.getY(event, index);
}
...

// Given an action int, returns a string description
public static String actionToString(int action) {
    switch (action) {

        case MotionEvent.ACTION_DOWN: return "Down";
        case MotionEvent.ACTION_MOVE: return "Move";
        case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down";
        case MotionEvent.ACTION_UP: return "Up";
        case MotionEvent.ACTION_POINTER_UP: return "Pointer Up";
        case MotionEvent.ACTION_OUTSIDE: return "Outside";
        case MotionEvent.ACTION_CANCEL: return "Cancel";
    }
    return "";
}

有關詳細信息,請訪問Google 處理多點觸控手勢

如果您需要處理多點觸控事件,請檢查PointerCount。

    public boolean onTouchEvent(MotionEvent event) {    
        if (event.getPointerCount() > 1) {
            Log.d(DEBUG_TAG,"Multitouch event"); 
            // The coordinates of the current screen contact, relative to 
            // the responding View or Activity.  
            xPos = (int)MotionEventCompat.getX(event, index);
            yPos = (int)MotionEventCompat.getY(event, index);

        } else {
            // Single touch event
            Log.d(DEBUG_TAG,"Single touch event"); 
            xPos = (int)MotionEventCompat.getX(event, index);
            yPos = (int)MotionEventCompat.getY(event, index);
        }
...

    }

您可以在此處找到更多信息: https//developer.android.com/training/gestures/multi.html

暫無
暫無

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

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