VelocityTracker是一个什么东西呢,查看VelocityTracker源代码发现有如下注释:
/**
* Helper for tracking the velocity of touch events, for implementing
* flinging and other such gestures.
*
* Use {@link #obtain} to retrieve a new instance of the class when you are going
* to begin tracking. Put the motion events you receive into it with
* {@link #addMovement(android.view.MotionEvent)}. When you want to determine the velocity call
* {@link #computeCurrentVelocity(int)} and then call {@link #getXVelocity(int)}
* and {@link #getYVelocity(int)} to retrieve the velocity for each pointer id.
*
* 追踪触摸事件速率,实现flinging和其他手势的帮助类
*
* 1、当开始追踪的时候,使用obtain来获取VelocityTracker类的实例
* 2、把接收到的MotionEvent放入到addMovement(android.view.MotionEvent)中
* 3、当要确定速度时调用computeCurrentVelocity(int),
* 使用getXVelocity(int)和getYVelocity(int)来检测每个触摸点id的速率
*/
VelocityTracker是一个帮助追踪触摸事件速率的追踪器,可以追踪fliinging和其他触摸手势。
如何使用VelocityTracker呢,注释中提到如下步骤:
1、当开始追踪的时候,使用obtain来获取VelocityTracker类的实例
2、把接受到的MotionEvent放入到addMovement(android.view.MotionEvent)中
3、当要确定速度时调用computeCurrentVelocity(int),
使用getXVelocity(int)和getYVelocity(int)来检测每个触摸点id的速率
既然VelocityTracker是追踪触摸事件的速度追踪器,当然需要与触摸事件结合使用。
第一步:当你想要追踪触摸事件的速度时,使用private VelocityTracker mVelocityTracker = VelocityTracker.obtain();来获取一个实例,
obtain()方法的源代码:
/**
* Retrieve a new VelocityTracker object to watch the velocity of