標籤:
原文地址:http://android.xsoftlab.net/training/gestures/movement.html
這節課將會學習如何在觸摸事件中記錄手指移動的軌跡。
當手指觸摸的位置、壓力或者尺寸發生變化時,ACTION_MOVE事件就會被觸發。與Detecting Common Gestures中描述的一樣,所有的事件都被記錄在一個MotionEvent對象中。
因為基於手指的觸摸並不是很精確的互動方式,所以檢測觸摸事件的行為需要更多的軌跡點。為了協助APP區分基於軌跡的手勢(比如滑動等移動的手勢)與非軌跡手勢(比如單點等不移動的手勢),Android提出了一個名為”touch slop”的概念。Touch slop指的是使用者按下的以像素為單位的距離。
這裡有若干項不同的追蹤手勢軌跡的方法,具體使用哪個方法取決於應用程式的需求:
- 指標的起始位置與結束位置。
- 指標位移的方向,由X,Y的座標判斷。
- 記錄,你可以通過getHistorySize()獲得手勢的曆史尺寸。然後可以通過getHistorical(Value)方法獲得這些曆史事件的位置,尺寸,事件以及壓力。當渲染手指的軌跡時,比如在螢幕上用手指畫線條等,記錄這時就會派上用場。
- 指標在螢幕上滑動的速度。
軌跡的速度
在記錄手勢的特性或者在檢查何種手勢事件發生時,除了要依靠手指移動的距離、方向這兩個要素之外。還需要另外一個非常重要的因素就是速度。為了使速度計算更加容易,Android為此提供了VelocityTracker類以及VelocityTrackerCompat類。VelocityTracker用於輔助記錄觸摸事件的速度。這對於判斷哪個速度是手勢的標準部分,比如飛速滑動。
下面的例子用於示範在VelocityTracker API中方法的目的:
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 // Best practice to use VelocityTrackerCompat where possible. Log.d("", "X velocity: " + VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId)); Log.d("", "Y velocity: " + VelocityTrackerCompat.getYVelocity(mVelocityTracker, 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; }
Note: 注意,應當在ACTION_MOVE事件內部計算速度,不要在ACTION_UP內部計算,因為在ACTION_UP內部計算所得到的X與Y的速度值都是0.
Android官方開發文檔Training系列課程中文版:手勢處理之記錄手指移動的軌跡