velocity [və'lɒsəti] n.速度;迅速;速率 ; Tracker 英['trækə] n. 追蹤者;跟蹤裝置 VelocityTracker顧名思義即速度跟蹤。android
android.view.VelocityTracke主要用跟蹤觸摸屏事件的速率。在android中主要應用於touch even。 VelocityTracker經過跟蹤一連串事件實時計算出當前的速度,好比Gestures中的Fling, Scrolling和其餘gestures手勢事件等。ide
使用過程:函數
示例代碼code
private VelocityTracker mVelocityTracker; linearLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //建立VelocityTracker對象,並將觸摸content界面的滑動事件加入到VelocityTracker當中。 if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: //設置移動速度單位爲:像素/10000ms,即1000毫秒內移動的像素 mVelocityTracker.computeCurrentVelocity(1000); //獲取手指在界面滑動的速度。 int velocity = (int) mVelocityTracker.getXVelocity(); textView.setText(velocity+""); break; case MotionEvent.ACTION_UP: //回收對象 mVelocityTracker.recycle(); mVelocityTracker = null; break; } return true; } });