溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中VelocityTracker怎么用

發(fā)布時(shí)間:2021-08-30 09:18:27 來源:億速云 閱讀:147 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Android中VelocityTracker怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

   VelocityTracker顧名思義即速度跟蹤,在android中主要應(yīng)用于touch even。VelocityTracker通過跟蹤一連串事件實(shí)時(shí)計(jì)算出當(dāng)前的速度,這樣的用法在android系統(tǒng)空間中隨處可見,比如Gestures中的Fling, Scrolling等。

   VelocityTracker主要用跟蹤觸摸屏事件(flinging事件和其他gestures手勢(shì)事件)的速率。用addMovement(MotionEvent)函數(shù)將Motion event加入到VelocityTracker類實(shí)例中.你可以使用getXVelocity() 或getXVelocity()獲得橫向和豎向的速率到速率時(shí),但是使用它們之前請(qǐng)先調(diào)用computeCurrentVelocity(int)來初始化速率的單位 。

Public Methods
voidaddMovement(      MotionEventevent)             Add a user's movement to the tracker.
voidclear()             Reset the velocity tracker back to its initial state.
voidcomputeCurrentVelocity(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表示速率的最大值
voidcomputeCurrentVelocity(int units)             Equivalent to invoking computeCurrentVelocity(int, float)with a maximum velocity of Float.MAX_VALUE.             一般使用此函數(shù)即可
abstract TgetNextPoolable()
floatgetXVelocity()             Retrieve the last computed X velocity.
floatgetXVelocity(int id)             Retrieve the last computed X velocity.
floatgetYVelocity(int id)             Retrieve the last computed Y velocity.
floatgetYVelocity()             Retrieve the last computed Y velocity.
abstract booleanisPooled()
static        VelocityTrackerobtain()             Retrieve a new VelocityTracker object to watch the velocity of a motion.
voidrecycle()             Return a VelocityTracker object back to be re-used by others.
abstract voidsetNextPoolable(T element)
abstract voidsetPooled(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);
	    }

以上是“Android中VelocityTracker怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI