1.简介
VelocityTracker是android提供的用来记录滑动速度的一个类,可以监控手指移动的速度。
2.VelocityTracker常用方法
2.1.
方法:public static VelocityTracker obtain()
作用:获取一个新的VelocityTracker对象。
2.2.
方法:public void recycle()
作用:返回一个VelocityTracker对象以供其他人重用。
2.3.
方法:public void addMovement(MotionEvent event)
作用:向跟踪器添加用户的移动。
2.4.
方法:public float getXVelocity()
作用:检索最后计算的X速度。
2.5.
方法:public float getYVelocity()
作用:检索最后计算的Y速度。
2.6.
方法:public void clear()
作用:将速度跟踪器重置为初始状态。
2.7.
方法:public void computeCurrentVelocity(int units)
作用:计算过去unitsms内的速度。这个速度其实就是 ((当前的位置)-(之前的位置))/时间.如果得到的值为200 就表示这1000ms内,X方向移动了200像素。速度是有正负的,右划就是正的,左划就是负的,上划为负, 下划为正。
2.8.
方法:public void computeCurrentVelocity(int units, float maxVelocity)
作用:计算过去unitsms内的速度。maxVelocity:最大速度,实际上是一个上限,比如我们当前速度300,但是上限为200,那用getXVelocity()得到的值就是200,不可能超过上限(无论正负)。
3.配合onTouch使用
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/activity_motionevent_textview" android:layout_width="200dp" android:layout_height="200dp" android:clickable="true" android:layout_centerInParent="true" android:gravity="center" android:background="#FF0000" android:textColor="#FFFFFF" android:text="Android 自定义View之MotionEvent方法详解"/> </RelativeLayout>
Java代码
public class MotionEventActivity extends AppCompatActivity { private TextView textView; private int lastX; private int lastY; private VelocityTracker velocityTracker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_motionevent); if(velocityTracker == null) {//velocityTracker对象为空 获取velocityTracker对象 velocityTracker = VelocityTracker.obtain(); }else {//velocityTracker对象不为空 将velocityTracker对象重置为初始状态 velocityTracker.clear(); } textView= (TextView) findViewById(R.id.activity_motionevent_textview); textView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { int action=motionEvent.getAction(); int x= (int) motionEvent.getRawX(); int y= (int) motionEvent.getRawY(); velocityTracker.addMovement(motionEvent);//向velocityTracker对象添加action switch(action){ case MotionEvent.ACTION_DOWN://按下 lastX=x; lastY=y; break; case MotionEvent.ACTION_MOVE://移动 velocityTracker.computeCurrentVelocity(1000); Log.d("TAG", "移动X velocity: " + velocityTracker.getXVelocity()); Log.d("TAG", "移动Y velocity: " + velocityTracker.getYVelocity()); break; case MotionEvent.ACTION_UP://抬起 velocityTracker.computeCurrentVelocity(1000); Log.d("TAG", "抬起X velocity: " + velocityTracker.getXVelocity()); Log.d("TAG", "抬起Y velocity: " + velocityTracker.getYVelocity()); break; } return false; } }); } /** * onDestroy方法 * */ @Override protected void onDestroy() { super.onDestroy(); if (velocityTracker != null) { velocityTracker.recycle(); velocityTracker = null; } } }
效果:
附:VelocityTracker官方链接
https://developer.android.google.cn/reference/android/view/VelocityTracker