当前位置:网站首页>Velocitytracker use

Velocitytracker use

2022-06-24 03:02:00 Study hard

1 brief introduction

VelocityTracker Speed tracking for touch events , It can obtain the speed value of the current touch event , According to the speed value , We can judge whether the current gesture is fling etc. . Usually used to implement fling Sliding effect when , It makes the control look silky and smooth .

2 Introduction of usage

Let's start with VelocityTracker Class is provided externally API Interface

static VelocityTracker obtain() // obtain VelocityTracker object 

void addMovement(MotionEvent event) // Add touch events that need to track speed , It can be ACTION_DOWN, ACTION_MOVE, ACTION_UP Any one of 

/**
 *  Used to calculate the speed of added Events ,
 * units Is the unit of speed , Usually set to 1000, It means the speed of calculation , Unit is pixel / second 
 * maxVelocity Is the maximum speed ,
 */
void computeCurrentVelocity(int units, float maxVelocity) 

void computeCurrentVelocity(int units) 

/*
 * obtain id The speed of the corresponding finger in the horizontal direction , Call... Before using this method computeCurrentVelocity()
 */
float getXVelocity(int id)

float getXVelocity()

/*
 * obtain id The speed of the corresponding finger in the vertical direction , Call... Before using this method computeCurrentVelocity()
 */
float getYVelocity(int id)

float getYVelocity()

void clear() // Reset velocityTracker, Go back to the original state 

void recycle() // Recycle and reuse 

Usage is as follows , With onTouchEvent For example

Private VelocityTracker mVelocityTracker;

public void onTouchEvent(MotionEvent ev) {
    ...
    if (mVelocityTracker == null) {
       mVelocityTracker = VelocityTracker.obtain();
    }

    switch (ev.getAction()) {
      ...
      case MotionEvent.ACTION_UP:
         mVelocityTracker.addMovement(ev);
         mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
         float velocityX = mVelocityTracker.getX();
         float velocityY = mVelocityTracker.getY();
         ...
    }
    ...
}

3 summary

VelocityTracker Class is through jni The way , stay native The velocity calculation of the layer . When using the application layer , When it is really necessary to calculate the speed , Call again addMovement() and computeCurrentVelocity() Method , After all, these are time-consuming methods . Refer to the example above .

原网站

版权声明
本文为[Study hard]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/10/20211020184615660U.html

随机推荐