Android VelocityTracker使用案例詳解
VelocityTracker顧名思義即速度跟蹤,在android中主要應(yīng)用于touch even。VelocityTracker通過跟蹤一連串事件實時計算出當前的速度,這樣的用法在android系統(tǒng)空間中隨處可見,比如Gestures中的Fling, Scrolling等。
VelocityTracker主要用跟蹤觸摸屏事件(flinging事件和其他gestures手勢事件)的速率。用addMovement(MotionEvent)函數(shù)將Motion event加入到VelocityTracker類實例中.你可以使用getXVelocity() 或getXVelocity()獲得橫向和豎向的速率到速率時,但是使用它們之前請先調(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表示速率的基本時間單位。unitis值為1的表示是,一毫秒時間單位內(nèi)運動了多少個像素, unitis值為1000表示一秒(1000毫秒)時間單位內(nèi)運動了多少個像素 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,意思為一秒時間內(nèi)運動了多少個像素 } return super.onTouchEvent(ev); }
到此這篇關(guān)于Android VelocityTracker使用案例詳解的文章就介紹到這了,更多相關(guān)Android VelocityTracker使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)藝術(shù)探索學(xué)習(xí)筆記(七)
這篇文章主要介紹了Android開發(fā)藝術(shù)探索學(xué)習(xí)筆記(七)的相關(guān)資料,需要的朋友可以參考下2016-01-01Android自定義View 實現(xiàn)水波紋動畫引導(dǎo)效果
在android程序開發(fā)中,我們經(jīng)常簡單通過自定義view實現(xiàn)水波紋動畫引導(dǎo)功能,下面通過本文給大家分享實現(xiàn)代碼,需要的朋友參考下2017-01-01Android實現(xiàn)兩個ScrollView互相聯(lián)動的同步滾動效果代碼
這篇文章主要介紹了Android實現(xiàn)兩個ScrollView互相聯(lián)動的同步滾動效果代碼,涉及Android操作ScrollView實現(xiàn)聯(lián)動功能的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10