處理多點觸控手勢

試用 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 事件結束的事件。一次只能啟用一個手勢。「向下」、「移動」、「向上」和「取消」的動作會套用至整個手勢。例如,包含 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.多點觸控繪圖模式。

其他資源

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