View基础——VelocityTracker

来源:互联网 发布:淘宝外卖客服电话多少 编辑:程序博客网 时间:2024/06/11 04:32

一、获取系统识别最小滑动距离

ViewConfiguration.get((getContext())).getScaledTouchSlop();

二、滑动速度VelocityTracker

获取MotionEvent的滑动速度

VelocityTracker mVelocityTracker = VelocityTracker.obtain();mVelocityTracker.addMovement(event);// 时间间隔mVelocityTracker.computeCurrentVelocity(1000);int x = (int) mVelocityTracker.getXVelocity();int y = (int) mVelocityTracker.getYVelocity();Log.i("info1", "x-->>" + x + "\ny-->>" + y);

速度 = (终点位置 - 起点位置) / 时间间隔

所以x,y的正负则表示滑动的方向。

不需要的时候在释放资源

// 重置mVelocityTracker.clear();// 释放内存mVelocityTracker.recycle();