追蹤觸控和指標移動位置

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

本課程說明如何追蹤觸控事件中的活動。

每當目前的觸控聯絡人位置、壓力或大小有所變更時,就會透過 ACTION_MOVE 事件觸發新的 onTouchEvent()。如「偵測常用手勢」一文所述,所有事件都會記錄在 onTouchEvent()MotionEvent 參數中。

由於手指式觸控不一定是最精確的互動形式,因此偵測觸控事件的頻率,通常會比單純接觸位置來得重要。為協助應用程式區分以動作為基礎的手勢 (例如滑動) 與非移動手勢 (例如單次輕觸),Android 提供觸控輪的概念。觸控偏移是指使用者輕觸時可移動的距離 (以像素為單位),系統會將手勢解譯為以動作為基礎的手勢。如要進一步瞭解這個主題,請參閱「管理 ViewGroup 中的觸控事件」一文。

視應用程式的需求而定,您可以利用手勢追蹤動作。示例如下:

  • 指標的起始和結束位置,例如將螢幕上的物件從 A 點移動到 B 點。
  • 指標的行駛方向,由 X 和 Y 座標決定。
  • 。您可以呼叫 MotionEvent 方法 getHistorySize(),查看手勢記錄的大小。接著,您可以使用動作事件的 getHistorical<Value> 方法,取得每個歷史事件的位置、大小、時間和壓力。算繪使用者的手指追蹤記錄 (例如觸控繪圖) 時,記錄非常實用。詳情請參閱 MotionEvent 參考資料。
  • 指標在觸控螢幕上移動時的速率。

請參閱下列相關資源:

追蹤速度

您可以採用根據指標移動的距離或方向移動型手勢。不過,速率經常是決定手勢特性或決定手勢是否發生的決定因素。為簡化速率計算,Android 提供 VelocityTracker 類別。VelocityTracker 可協助您追蹤觸控事件的速率。這對於速度是手勢條件 (例如快速滑過) 的手勢而言非常實用。

以下範例說明 VelocityTracker API 中方法的用途:

Kotlin

private const val DEBUG_TAG = "Velocity"

class MainActivity : Activity() {
    private var mVelocityTracker: VelocityTracker? = null

    override fun onTouchEvent(event: MotionEvent): Boolean {

        when (event.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                // Reset the velocity tracker back to its initial state.
                mVelocityTracker?.clear()
                // If necessary, retrieve a new VelocityTracker object to watch
                // the velocity of a motion.
                mVelocityTracker = mVelocityTracker ?: VelocityTracker.obtain()
                // Add a user's movement to the tracker.
                mVelocityTracker?.addMovement(event)
            }
            MotionEvent.ACTION_MOVE -> {
                mVelocityTracker?.apply {
                    val pointerId: Int = event.getPointerId(event.actionIndex)
                    addMovement(event)
                    // When you want to determine the velocity, call
                    // computeCurrentVelocity(). Then, call getXVelocity() and
                    // getYVelocity() to retrieve the velocity for each pointer
                    // ID.
                    computeCurrentVelocity(1000)
                    // Log velocity of pixels per second. It's best practice to
                    // use VelocityTrackerCompat where possible.
                    Log.d("", "X velocity: ${getXVelocity(pointerId)}")
                    Log.d("", "Y velocity: ${getYVelocity(pointerId)}")
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                // Return a VelocityTracker object back to be re-used by others.
                mVelocityTracker?.recycle()
                mVelocityTracker = null
            }
        }
        return true
    }
}

Java

public class MainActivity extends Activity {
    private static final String DEBUG_TAG = "Velocity";
        ...
    private VelocityTracker mVelocityTracker = null;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);

        switch(action) {
            case MotionEvent.ACTION_DOWN:
                if(mVelocityTracker == null) {
                    // Retrieve a new VelocityTracker object to watch the
                    // velocity of a motion.
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    // Reset the velocity tracker back to its initial state.
                    mVelocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
                // When you want to determine the velocity, call
                // computeCurrentVelocity(). Then call getXVelocity() and
                // getYVelocity() to retrieve the velocity for each pointer ID.
                mVelocityTracker.computeCurrentVelocity(1000);
                // Log velocity of pixels per second. It's best practice to use
                // VelocityTrackerCompat where possible.
                Log.d("", "X velocity: " + mVelocityTracker.getXVelocity(pointerId));
                Log.d("", "Y velocity: " + mVelocityTracker.getYVelocity(pointerId));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // Return a VelocityTracker object back to be re-used by others.
                mVelocityTracker.recycle();
                break;
        }
        return true;
    }
}

使用指標擷取功能

部分應用程式 (例如遊戲、遠端桌面和虛擬化用戶端) 會因能控制滑鼠遊標而受益。指標擷取功能適用於 Android 8.0 (API 級別 26) 以上版本,可提供這個控制項,方式是將所有滑鼠事件傳送至應用程式中的聚焦檢視畫面。

要求指標擷取

應用程式中的檢視畫面只有在包含該檢視區塊的檢視區塊階層具有焦點時,才能要求指標擷取作業。因此,當檢視區塊出現特定的使用者動作時 (例如在活動的 onClick() 事件或活動的 onWindowFocusChanged() 事件處理常式期間),要求指標擷取。

如要要求指標擷取,請在檢視畫面中呼叫 requestPointerCapture() 方法。以下程式碼範例說明如何在使用者點選檢視區塊時要求指標擷取:

Kotlin

fun onClick(view: View) {
    view.requestPointerCapture()
}

Java

@Override
public void onClick(View view) {
    view.requestPointerCapture();
}

要求擷取指標的要求成功後,Android 會呼叫 onPointerCaptureChange(true)。系統會將滑鼠事件傳送至應用程式內聚焦的檢視畫面,前提是其與要求擷取的檢視畫面位於相同的檢視區塊階層。其他應用程式會停止接收滑鼠事件,直到擷取作業釋出為止,包括 ACTION_OUTSIDE 事件。Android 照常從滑鼠以外的來源傳送指標事件,但不會再顯示滑鼠遊標。

處理擷取的指標事件

一旦檢視畫面成功取得指標擷取,Android 就會傳送滑鼠事件。聚焦檢視可以執行下列其中一項工作來處理事件:

以下程式碼範例說明如何實作 onCapturedPointerEvent(MotionEvent)

Kotlin

override fun onCapturedPointerEvent(motionEvent: MotionEvent): Boolean {
    // Get the coordinates required by your app.
    val verticalOffset: Float = motionEvent.y
    // Use the coordinates to update your view and return true if the event is
    // successfully processed.
    return true
}

Java

@Override
public boolean onCapturedPointerEvent(MotionEvent motionEvent) {
  // Get the coordinates required by your app.
  float verticalOffset = motionEvent.getY();
  // Use the coordinates to update your view and return true if the event is
  // successfully processed.
  return true;
}

以下程式碼範例說明如何註冊 OnCapturedPointerListener

Kotlin

myView.setOnCapturedPointerListener { view, motionEvent ->
    // Get the coordinates required by your app.
    val horizontalOffset: Float = motionEvent.x
    // Use the coordinates to update your view and return true if the event is
    // successfully processed.
    true
}

Java

myView.setOnCapturedPointerListener(new View.OnCapturedPointerListener() {
  @Override
  public boolean onCapturedPointer (View view, MotionEvent motionEvent) {
    // Get the coordinates required by your app.
    float horizontalOffset = motionEvent.getX();
    // Use the coordinates to update your view and return true if the event is
    // successfully processed.
    return true;
  }
});

無論您使用自訂檢視區塊或註冊事件監聽器,檢視區塊都會收到含有指標座標的 MotionEvent,用於指定 X 或 Y 差異等相對動作,類似軌跡球裝置提供的座標。您可以使用 getX()getY() 擷取座標。

放開指標擷取

應用程式中的檢視畫面可呼叫 releasePointerCapture() 來釋出指標擷取內容,如以下程式碼範例所示:

Kotlin

override fun onClick(view: View) {
    view.releasePointerCapture()
}

Java

@Override
public void onClick(View view) {
    view.releasePointerCapture();
}

系統可以在您未明確呼叫 releasePointerCapture() 的情況下,從檢視區塊中移除擷取內容,通常是因為包含要求擷取的檢視區塊的檢視區塊階層會失去焦點。