VelocityTracker
速度追踪,包括水平和竖直方向的速度,在onTounchEvent方法中调用
VelocityTracker velocityTracker =VelocityTracker.obtain();
velocityTracker.addMovement(event)
获取速度:速度=(终点位置-起点位置)/时间段
velocityTracker.computeCurrentVelocity(1000);
float x=velocityTracker.getXVelocity();
float y=velocityTracker.getXVelocity();
结束时候记得是释放资源
velocityTracker.clear();
velocityTracker.recycle();
举例
VelocityTracker velocityTracker;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
velocityTracker = VelocityTracker.obtain();
velocityTracker.addMovement(event);
break;
case MotionEvent.ACTION_MOVE:
velocityTracker.computeCurrentVelocity(1000);
float x = velocityTracker.getXVelocity();
float y = velocityTracker.getXVelocity();
//todo
break;
case MotionEvent.ACTION_UP:
velocityTracker.clear();
velocityTracker.recycle();
break;
}
return super.onTouchEvent(event);
}