下面是Google對該類的描述:
/** * Helper for tracking the velocity of touch events, for implementing * flinging and other such gestures. Use {@link #obtain} to retrieve a * new instance of the class when you are going to begin tracking, put * the motion events you receive into it with {@link #addMovement(MotionEvent)}, * and when you want to determine the velocity call * {@link #computeCurrentVelocity(int)} and then {@link #getXVelocity()} * and {@link #getXVelocity()}. */
簡單翻譯下:
協助你追蹤一個touch事件(flinging事件和其他手勢事件)的速率。當你要跟蹤一個touch事件的時候,使用obtain()方法得到這個類的執行個體,然後 用addMovement(MotionEvent)函數將你接受到的motion event加入到VelocityTracker類執行個體中。當你使用到速率時,使用computeCurrentVelocity(int)初始化速率的單位,並獲得當前的事件的速率,然後使用getXVelocity() 或getXVelocity()獲得橫向和豎向的速率。
從上面的介紹中,我們就可以很簡單的明白,如何去使用VelocityTracker類去追蹤一個移動事件的速率。
用法詳解:
1、//首先獲得VelocityTracker的執行個體 /** * obtain()的方法介紹 * Retrieve a new VelocityTracker object to watch the velocity of a motion. * Be sure to call recycle() when done. You should generally only maintain * an active object while tracking a movement, so that the VelocityTracker * can be re-used elsewhere. * 翻譯: * 得到一個速率追蹤者對象去檢測一個事件的速率。確認在完成的時候調用recycle()方法。 * 一般情況下,你只要維持一個活動的速率追蹤者對象去追蹤一個事件,那麼,這個速率追蹤者 * 可以在別的地方重複使用。 */ VelocityTracker mVelocityTracker = null; if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } 2、//假設有一個事件event,將事件加入到VelocityTracker類執行個體中 /** * addMovement (MotionEvent event)方法介紹 * Add a user's movement to the tracker. You should call this for the initial * ACTION_DOWN, the following ACTION_MOVE events that you receive, * and the final ACTION_UP. You can, however, call this for whichever events * you desire. * 翻譯:向速率追蹤者中加入一個使用者的移動事件,你應該最先在ACTION_DOWN調用這個方法, * 然後在你接受的ACTION_MOVE,最後是ACTION_UP。你可以為任何一個你願意的事件調用該方法 */ mVelocityTracker.addMovement(event); 3、//判斷當事件MotionEvent.ACTION_UP的時候,調用下面的方法 /** * public void computeCurrentVelocity (int units, float maxVelocity)方法介紹: * Compute the current velocity based on the points that have been * collected. Only call this when you actually want to retrieve velocity * information, as it is relatively expensive. You can then retrieve the * velocity with {@link #getXVelocity()} and {@link #getYVelocity()}. * * @param units * The units you would like the velocity in. A value of 1 * provides pixels per millisecond, 1000 provides pixels per * second, etc. * @param maxVelocity * The maximum velocity that can be computed by this method. This * value must be declared in the same unit as the units * parameter. This value must be positive. * 翻譯:基於你所收集到的點計算當前的速率。 當你確定要獲得速率資訊的時候,在調用該方法, * 因為使用它需要消耗很大的效能。然後,你可以通過getXVelocity()和getYVelocity()獲得橫向和豎向的速率。 * * 參數:units 你想要指定的得到的速度單位,如果值為1,代表1毫秒運動了多少像素。如果值為1000,代表 * 1秒內運動了多少像素。 * * 參數:maxVelocity 該方法所能得到的最大速度,這個速度必須和你指定的units使用同樣的單位,而且 * 必須是整數。(也就是,你指定一個速度的最大值,如果計算超過這個最大值,就使用這個最大值,否則,使用計算的的結果) * * public void computeCurrentVelocity (int units)方法介紹 * 這個方法與上面的方法沒什麼差別,就是在maxVelocity上,他會自動使用Float.MAX_VALUE常量 */ mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); 4、//然後調用getXVelocity ()、getXVelocity (int id)、getYVelocity ()、getYVelocity (int id)得到速率 /** * 調用這幾個方法之前,必須確定你之前調用了computeCurrentVelocity方法。 * 參數 id 代表返回指定觸點的速率 */ Log.i("test", mVelocityTracker.getXVelocity() + "");Log.i("test", mVelocityTracker.getYVelocity() + "");
在附上一個使用執行個體:
public class GestureTestActivity extends Activity {private GestureDetector gestureDetector;private VelocityTracker mVelocityTracker = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);gestureDetector = new GestureDetector(this, new GestureListener());gestureDetector.setIsLongpressEnabled(false);}@Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction();if (mVelocityTracker == null) {mVelocityTracker = VelocityTracker.obtain();}mVelocityTracker.addMovement(event);switch (action) {case MotionEvent.ACTION_DOWN:Log.i("test", "ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:// 移動的時候Log.i("test", "ACTION_MOVE");break;case MotionEvent.ACTION_UP:mVelocityTracker.computeCurrentVelocity(1000);Log.i("test", "ACTION_UP");Log.i("11111", mVelocityTracker.getXVelocity(0) + "");Log.i("11111", mVelocityTracker.getYVelocity(0) + "");break;}return gestureDetector.onTouchEvent(event);}// 繼承於SimpleOnGestureListener,實現所有事件監聽方法private class GestureListener extends SimpleOnGestureListener {@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {Log.i("test", "onFling -----------------------");Log.i("2222", velocityX + "");Log.i("2222", velocityY + "");return super.onFling(e1, e2, velocityX, velocityY);}}@Overrideprotected void onDestroy() {super.onDestroy();//釋放mVelocityTracker.recycle();mVelocityTracker=null;}}
VelocityTracker的用法已經介紹完了,在我們的實際應用中,VelocityTracker類可能使用的不是很多,只需要瞭解上面的即可。