Android VelocityTracker使用案例詳解
VelocityTracker顧名思義即速度跟蹤,在android中主要應(yīng)用于touch even。VelocityTracker通過(guò)跟蹤一連串事件實(shí)時(shí)計(jì)算出當(dāng)前的速度,這樣的用法在android系統(tǒng)空間中隨處可見(jiàn),比如Gestures中的Fling, Scrolling等。
VelocityTracker主要用跟蹤觸摸屏事件(flinging事件和其他gestures手勢(shì)事件)的速率。用addMovement(MotionEvent)函數(shù)將Motion event加入到VelocityTracker類(lèi)實(shí)例中.你可以使用getXVelocity() 或getXVelocity()獲得橫向和豎向的速率到速率時(shí),但是使用它們之前請(qǐng)先調(diào)用computeCurrentVelocity(int)來(lái)初始化速率的單位 。
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開(kāi)發(fā)藝術(shù)探索學(xué)習(xí)筆記(七)
這篇文章主要介紹了Android開(kāi)發(fā)藝術(shù)探索學(xué)習(xí)筆記(七)的相關(guān)資料,需要的朋友可以參考下2016-01-01Android自定義View 實(shí)現(xiàn)水波紋動(dòng)畫(huà)引導(dǎo)效果
在android程序開(kāi)發(fā)中,我們經(jīng)常簡(jiǎn)單通過(guò)自定義view實(shí)現(xiàn)水波紋動(dòng)畫(huà)引導(dǎo)功能,下面通過(guò)本文給大家分享實(shí)現(xiàn)代碼,需要的朋友參考下2017-01-01Android實(shí)現(xiàn)兩個(gè)ScrollView互相聯(lián)動(dòng)的同步滾動(dòng)效果代碼
這篇文章主要介紹了Android實(shí)現(xiàn)兩個(gè)ScrollView互相聯(lián)動(dòng)的同步滾動(dòng)效果代碼,涉及Android操作ScrollView實(shí)現(xiàn)聯(lián)動(dòng)功能的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10基于Android實(shí)現(xiàn)一個(gè)常用的布局吸頂效果
這篇文章給大家介紹一個(gè)布局吸頂效果,一般出現(xiàn)在內(nèi)容較長(zhǎng)頁(yè)面還嵌套著分類(lèi)頁(yè)面的情況,比如電商的詳情頁(yè)嵌套分類(lèi),在頁(yè)面滑動(dòng)到tab的時(shí)候我們希望tab還能保留在頁(yè)面頂部而不被頂上去,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-09-09android開(kāi)發(fā)PathEffect問(wèn)題處理
本文主要整理了關(guān)于android中PathEffect的問(wèn)題匯總以及處理方式,以及給大家做了關(guān)于PathEffect類(lèi)的詳細(xì)解釋。2017-11-11