處理多點觸控手勢

嘗試 Compose 方法
Jetpack Compose 是 Android 推薦的 UI 工具包。瞭解如何在 Compose 中使用觸控和輸入功能。

多點觸控手勢是指多次指標 (手指) 輕觸螢幕時。本文件說明如何偵測涉及多個指標的手勢。

追蹤多個指標

多次指標同時輕觸螢幕時,系統會產生下列觸控事件:

  • ACTION_DOWN:在第一個指標輕觸螢幕時傳送。這樣做會啟動該手勢。這個指標的指標資料一律位於 MotionEvent 中的索引 0
  • ACTION_POINTER_DOWN:在第一次離開螢幕後,有額外指標進入螢幕時傳送。您可以使用 getActionIndex() 取得剛剛關閉的指標索引。
  • ACTION_MOVE:在手勢發生變更時傳送,涉及任何指標數量。
  • ACTION_POINTER_UP:在非主要指標上升時傳送。您可以使用 getActionIndex() 取得剛剛向上的指標索引。
  • ACTION_UP:在最後一個指標離開螢幕時傳送。
  • ACTION_CANCEL:表示整個手勢 (包括所有指標) 已取消。

開始與結束手勢

手勢是指一系列事件,先以 ACTION_DOWN 事件開頭,結尾則是 ACTION_UPACTION_CANCEL 事件。一次只能使用一個手勢。「向下」、「MOVE」、「向上」和「取消」等動作會套用至整個手勢。例如,含有 ACTION_MOVE 的事件表示當下所有指標向下的移動。

持續追蹤指標

使用指標的索引和 ID,追蹤 MotionEvent 中的個別指標位置。

  • 索引MotionEvent 會在陣列中儲存指標資訊。指標的索引是其在這個陣列中的位置。大部分的 MotionEvent 方法會採用指標索引做為參數,而非指標 ID。
  • ID:每個指標還具有 ID 對應,在觸控事件間保持不變,以便追蹤整個手勢中的個別指標。

個別指標會以未定義的順序顯示在動作事件中。因此,指標的索引可以從一個事件變更為下一個事件,但只要指標保持有效狀態,指標的指標 ID 一定會保持不變。使用 getPointerId() 方法取得指標 ID,即可在手勢中追蹤所有後續動作事件的指標。接著,如果是連續的動作事件,請使用 findPointerIndex() 方法取得該動作事件中特定指標 ID 的指標索引。例如:

Kotlin

private var mActivePointerId: Int = 0

override fun onTouchEvent(event: MotionEvent): Boolean {
    ...
    // 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.
    val (x: Float, y: Float) = event.findPointerIndex(mActivePointerId).let { pointerIndex ->
        // Get the pointer's current position.
        event.getX(pointerIndex) to event.getY(pointerIndex)
    }
    ...
}

Java

private int mActivePointerId;

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);
    ...
}

如要支援多個觸控指標,您可以在其個別 ACTION_POINTER_DOWNACTION_DOWN 事件時間快取所有有效指標及其 ID。從快取的 ACTION_POINTER_UPACTION_UP 事件中移除指標。您可能會覺得這些快取 ID 有助於正確處理其他動作事件。舉例來說,在處理 ACTION_MOVE 事件時,請找出各個快取有效指標 ID 的索引,然後使用 getX()getY() 函式擷取指標座標,然後將這些座標與快取座標進行比較,以找出移動的指標。

請僅將 getActionIndex() 函式與 ACTION_POINTER_UPACTION_POINTER_DOWN 事件搭配使用。請勿搭配 ACTION_MOVE 事件使用這個函式,因為一律會傳回 0

擷取 MotionEvent 個動作

使用 getActionMasked() 方法或相容性版本 MotionEventCompat.getActionMasked() 擷取 MotionEvent 的動作。與先前的 getAction() 方法不同,getActionMasked() 專為搭配多個指標而設計。此方法會在沒有指標索引的情況下傳回動作。如果動作含有有效指標索引,請使用 getActionIndex() 傳回與動作相關聯的指標索引,如以下程式碼片段所示:

Kotlin

val (xPos: Int, yPos: Int) = MotionEventCompat.getActionMasked(event).let { action ->
    Log.d(DEBUG_TAG, "The action is ${actionToString(action)}")
    // Get the index of the pointer associated with the action.
    MotionEventCompat.getActionIndex(event).let { index ->
        // The coordinates of the current screen contact, relative to
        // the responding View or Activity.
        MotionEventCompat.getX(event, index).toInt() to MotionEventCompat.getY(event, index).toInt()
    }
}

if (event.pointerCount > 1) {
    Log.d(DEBUG_TAG, "Multitouch event")

} else {
    // Single touch event.
    Log.d(DEBUG_TAG, "Single touch event")
}

...

// Given an action int, returns a string description.
fun actionToString(action: Int): String {
    return when (action) {
        MotionEvent.ACTION_DOWN -> "Down"
        MotionEvent.ACTION_MOVE -> "Move"
        MotionEvent.ACTION_POINTER_DOWN -> "Pointer Down"
        MotionEvent.ACTION_UP -> "Up"
        MotionEvent.ACTION_POINTER_UP -> "Pointer Up"
        MotionEvent.ACTION_OUTSIDE -> "Outside"
        MotionEvent.ACTION_CANCEL -> "Cancel"
        else -> ""
    }
}

Java

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 "";
}
圖 1.多點觸控繪圖模式。

其他資源

如要進一步瞭解輸入事件,請參閱下列參考資料: