原文:https://blog.csdn.net/uyy203/article/details/78926439
下面是Google對(duì)該類(lèi)的描述:
/**
* 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()}.
*/
簡(jiǎn)單翻譯下:
幫助你追蹤一個(gè)touch事件(flinging事件和其他手勢(shì)事件)的速率。當(dāng)你要跟蹤一個(gè)touch事件的時(shí)候,使用obtain()方法得到這個(gè)類(lèi)的實(shí)例甜攀,然后 用addMovement(MotionEvent)函數(shù)將你接受到的motion event加入到VelocityTracker類(lèi)實(shí)例中诅蝶。當(dāng)你使用到速率時(shí)偎蘸,使用computeCurrentVelocity(int)初始化速率的單位,并獲得當(dāng)前的事件的速率,然后使用getXVelocity() 或getXVelocity()獲得橫向和豎向的速率。
從上面的介紹中摆昧,我們就可以很簡(jiǎn)單的明白,如何去使用VelocityTracker類(lèi)去追蹤一個(gè)移動(dòng)事件的速率蜒程。
用法詳解:
1绅你、 //首先獲得VelocityTracker的實(shí)例
/**
* 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.
* 翻譯:
* 得到一個(gè)速率追蹤者對(duì)象去檢測(cè)一個(gè)事件的速率。確認(rèn)在完成的時(shí)候調(diào)用recycle()方法昭躺。
* 一般情況下忌锯,你只要維持一個(gè)活動(dòng)的速率追蹤者對(duì)象去追蹤一個(gè)事件,那么窍仰,這個(gè)速率追蹤者
* 可以在別的地方重復(fù)使用汉规。
*/
VelocityTracker mVelocityTracker = null;
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
2礼殊、 //假設(shè)有一個(gè)事件event,將事件加入到VelocityTracker類(lèi)實(shí)例中
/**
* 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.
* 翻譯:向速率追蹤者中加入一個(gè)用戶(hù)的移動(dòng)事件驹吮,你應(yīng)該最先在ACTION_DOWN調(diào)用這個(gè)方法,
* 然后在你接受的ACTION_MOVE晶伦,最后是ACTION_UP碟狞。你可以為任何一個(gè)你愿意的事件調(diào)用該方法
*/
mVelocityTracker.addMovement(event);
3、//判斷當(dāng)事件MotionEvent.ACTION_UP的時(shí)候婚陪,調(diào)用下面的方法
/**
* 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.
* 翻譯:基于你所收集到的點(diǎn)計(jì)算當(dāng)前的速率族沃。 當(dāng)你確定要獲得速率信息的時(shí)候,在調(diào)用該方法,
* 因?yàn)槭褂盟枰暮艽蟮男阅艽嘌汀H缓蟪?眨憧梢酝ㄟ^(guò)getXVelocity()和getYVelocity()獲得橫向和豎向的速率。
*
* 參數(shù):units 你想要指定的得到的速度單位盖溺,如果值為1漓糙,代表1毫秒運(yùn)動(dòng)了多少像素。如果值為1000烘嘱,代表
* 1秒內(nèi)運(yùn)動(dòng)了多少像素昆禽。
*
* 參數(shù):maxVelocity 該方法所能得到的最大速度,這個(gè)速度必須和你指定的units使用同樣的單位蝇庭,而且
* 必須是整數(shù)醉鳖。(也就是,你指定一個(gè)速度的最大值哮内,如果計(jì)算超過(guò)這個(gè)最大值盗棵,就使用這個(gè)最大值,否則北发,使用計(jì)算的的結(jié)果)
*
* public void computeCurrentVelocity (int units)方法介紹
* 這個(gè)方法與上面的方法沒(méi)什么差別漾根,就是在maxVelocity上,他會(huì)自動(dòng)使用Float.MAX_VALUE常量
*/
mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
4鲫竞、//然后調(diào)用getXVelocity ()辐怕、getXVelocity (int id)、getYVelocity ()从绘、getYVelocity (int id)得到速率
/**
* 調(diào)用這幾個(gè)方法之前寄疏,必須確定你之前調(diào)用了computeCurrentVelocity方法。
* 參數(shù) id 代表返回指定觸點(diǎn)的速率
*/
Log.i("test", mVelocityTracker.getXVelocity() + "");
Log.i("test", mVelocityTracker.getYVelocity() + "");
在附上一個(gè)使用實(shí)例:
public class GestureTestActivity extends Activity {
private GestureDetector gestureDetector;
private VelocityTracker mVelocityTracker = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this, new GestureListener());
gestureDetector.setIsLongpressEnabled(false);
}
@Override
public 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:// 移動(dòng)的時(shí)候
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僵井,實(shí)現(xiàn)所有事件監(jiān)聽(tīng)方法
private class GestureListener extends SimpleOnGestureListener {
@Override
public 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);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//釋放
mVelocityTracker.recycle();
mVelocityTracker=null;
}
}