Detailed Explanation of Android VelocityTracker Use Case

  • 2021-12-13 17:19:03
  • OfStack

As its name implies, VelocityTracker is speed tracking, which is mainly used in touch and even in android. VelocityTracker calculates the current speed in real time by tracking a series of events, which can be seen everywhere in android system space, such as Fling and Scrolling in Gestures.

VelocityTracker is primarily used to track the rate of touch screen events (flinging events and other gestures gesture events). Use the addMovement (MotionEvent) function to add Motion event to the VelocityTracker class instance. You can use getXVelocity () or getXVelocity () to get horizontal and vertical rate to rate time, but call computeCurrentVelocity (int) to initialize the rate unit before using them.

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的表示是,1毫秒时间单位内运动了多少个像素, unitis值为1000表示1秒(1000毫秒)时间单位内运动了多少个像素 floatVelocity表示速率的最大值
void computeCurrentVelocity(int units) Equivalent to invoking computeCurrentVelocity(int, float)with a maximum velocity of Float.MAX_VALUE. 1般使用此函数即可
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)

Sample code:


@Override
	    public boolean onTouchEvent(MotionEvent ev) {
 
	        if (null == mVelocityTracker) {
	            mVelocityTracker = VelocityTracker.obtain();
	        }
	        mVelocityTracker.addMovement(ev);
 
	        switch (ev.getAction()) {
	            case MotionEvent.ACTION_UP:
	                //  Width hidden on the left 
	                int scrollX = getScrollX();
	                Loger.e(ObjEarth.TAG, "V=" + mVelocityTracker.getXVelocity());
	                if (Math.abs(mVelocityTracker.getXVelocity()) > 4000f) {
	                    if (mVelocityTracker.getXVelocity() < 0f) {
	                        // Forward logic code 
	                    } else {
	                        // Reverse logic code 
	                    }
	                }
	                return true;
	            case MotionEvent.ACTION_MOVE:
	                mVelocityTracker.computeCurrentVelocity(1000); // Settings units The value of is 1000 It means 1 How many pixels have been moved in seconds 
	        }
	        return super.onTouchEvent(ev);
	    }

Related articles: