Android VelocityTracker使用案例詳解
VelocityTracker顧名思義即速度跟蹤,在android中主要應(yīng)用于touch even。VelocityTracker通過跟蹤一連串事件實(shí)時(shí)計(jì)算出當(dāng)前的速度,這樣的用法在android系統(tǒng)空間中隨處可見,比如Gestures中的Fling, Scrolling等。
VelocityTracker主要用跟蹤觸摸屏事件(flinging事件和其他gestures手勢事件)的速率。用addMovement(MotionEvent)函數(shù)將Motion event加入到VelocityTracker類實(shí)例中.你可以使用getXVelocity() 或getXVelocity()獲得橫向和豎向的速率到速率時(shí),但是使用它們之前請先調(diào)用computeCurrentVelocity(int)來初始化速率的單位 。
| Public Methods | |
|---|---|
| void | addMovement( MotionEventevent) Add a user's movement to the tracker. |
| void | clear() Reset the velocity tracker back to its initial state. |
| void | computeCurrentVelocity(int units, float maxVelocity) Compute the current velocity based on the points that have been collected. intunitis表示速率的基本時(shí)間單位。unitis值為1的表示是,一毫秒時(shí)間單位內(nèi)運(yùn)動(dòng)了多少個(gè)像素, unitis值為1000表示一秒(1000毫秒)時(shí)間單位內(nèi)運(yùn)動(dòng)了多少個(gè)像素 floatVelocity表示速率的最大值 |
| void | computeCurrentVelocity(int units) Equivalent to invoking computeCurrentVelocity(int, float)with a maximum velocity of Float.MAX_VALUE. 一般使用此函數(shù)即可 |
| abstract T | getNextPoolable() |
| float | getXVelocity() Retrieve the last computed X velocity. |
| float | getXVelocity(int id) Retrieve the last computed X velocity. |
| float | getYVelocity(int id) Retrieve the last computed Y velocity. |
| float | getYVelocity() Retrieve the last computed Y velocity. |
| abstract boolean | isPooled() |
| static VelocityTracker | obtain() Retrieve a new VelocityTracker object to watch the velocity of a motion. |
| void | recycle() Return a VelocityTracker object back to be re-used by others. |
| abstract void | setNextPoolable(T element) |
| abstract void | setPooled(boolean isPooled) |
示例代碼:
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (null == mVelocityTracker) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(ev);
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
// 隱藏在左邊的寬度
int scrollX = getScrollX();
Loger.e(ObjEarth.TAG, "V=" + mVelocityTracker.getXVelocity());
if (Math.abs(mVelocityTracker.getXVelocity()) > 4000f) {
if (mVelocityTracker.getXVelocity() < 0f) {
//正向邏輯代碼
} else {
//反向邏輯代碼
}
}
return true;
case MotionEvent.ACTION_MOVE:
mVelocityTracker.computeCurrentVelocity(1000); //設(shè)置units的值為1000,意思為一秒時(shí)間內(nèi)運(yùn)動(dòng)了多少個(gè)像素
}
return super.onTouchEvent(ev);
}
到此這篇關(guān)于Android VelocityTracker使用案例詳解的文章就介紹到這了,更多相關(guān)Android VelocityTracker使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Flutter實(shí)現(xiàn)紅包動(dòng)畫效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Flutter實(shí)現(xiàn)紅包的動(dòng)畫效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2023-06-06
DownloadManager實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了DownloadManager實(shí)現(xiàn)文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android Studio 代理配置指南(小結(jié))
這篇文章主要介紹了Android Studio 代理配置指南(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
Android CoordinatorLayout高級(jí)用法之自定義Behavior
這篇文章主要介紹了Android CoordinatorLayout高級(jí)用法之自定義Behavior,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Android開發(fā)教程之ContentProvider數(shù)據(jù)存儲(chǔ)
這篇文章主要介紹了Android開發(fā)教程之ContentProvider數(shù)據(jù)存儲(chǔ)的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android開發(fā)筆記之Intent初級(jí)學(xué)習(xí)教程
這篇文章主要介紹了Android開發(fā)筆記之Intent初級(jí)學(xué)習(xí),較為詳細(xì)的分析了Android Intent項(xiàng)目的建立,功能實(shí)現(xiàn)及Intent使用技巧,需要的朋友可以參考下2016-02-02
Android開發(fā)之TextView控件用法實(shí)例總結(jié)
這篇文章主要介紹了Android開發(fā)之TextView控件用法,結(jié)合實(shí)例形式總結(jié)分析了TextView控件常用的屬性設(shè)置及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-02
Android本地驗(yàn)證碼的簡易實(shí)現(xiàn)方法(防止暴力登錄)
驗(yàn)證馬真是無處不在啊,主要作用是防止惡意暴力破解登錄,這篇文章主要介紹了Android本地驗(yàn)證碼的簡易實(shí)現(xiàn)方法(防止暴力登錄),需要的朋友可以參考下2017-04-04
完美解決Android Studio集成crashlytics后無法編譯的問題
下面小編就為大家?guī)硪黄昝澜鉀QAndroid Studio集成crashlytics后無法編譯的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05

