VelocityTracker 测量手速的使用

android中测量手速的地方为数不少,那么怎么获得呢?api中提供了VelocityTracker类来追踪我们在屏幕上的滑动速度。我这里定义了一个自定义view,在自定义view中的onTouchEvent中,获得了一个 VelocityTracker的实例。在测量速度时,官方建议只使用一个实例

 if(velocityTracker==null){
      velocityTracker=VelocityTracker.obtain();
 }

当我们获得了追踪手速的实例后,就可以将触摸事件添加到实例velocityTracker中,系统会为我们计算。但是在添加之后,计算之前,我们需要设置一个时间段,用来告诉系统,我们需要获取的是多长时间段内的速度。

velocityTracker.addMovement(event);
velocityTracker.computeCurrentVelocity(1000);

在设置时间段时,使用的单位为ms毫秒。接下来就可以分别获取x坐标轴上,y坐标轴上的速度了

float xVelocity = velocityTracker.getXVelocity();
float yVelocity = velocityTracker.getYVelocity();

官方建议我们在不使用手速追踪velocityTracker 的时候,重置回收该类,因此有了下面的代码:

public void release(){
    if(velocityTracker!=null){
        velocityTracker.clear();
        velocityTracker.recycle();
        velocityTracker=null;
    }
}

下面放送这个自定义view的全部代码:

public class VeloctiyView extends View{
    VelocityTracker velocityTracker;

    public VeloctiyView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
//        setClickable(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(velocityTracker==null){
            velocityTracker=VelocityTracker.obtain();
        }
        velocityTracker.addMovement(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.e("Vel","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_UP:
                Log.e("Vel","action_up");
                release();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("Vel","ACTION_MOVE");

                velocityTracker.computeCurrentVelocity(1000);
                float xVelocity = velocityTracker.getXVelocity();
                float yVelocity = velocityTracker.getYVelocity();
                Log.e("Vel",String.format("velx--->%f,vely--->%f",xVelocity,yVelocity));
                break;
            case MotionEvent.ACTION_CANCEL:
                release();
                break;
        }
        // 如果这里使用了super.onTouchEvent(event),那么就只会触发action_down。因为事件不会往下面传递,解决办法,返回true,或者调用setClickable(true),
        //实际上二者的效果是一样的。
        return true;
    }

    public void release(){
        if(velocityTracker!=null){
            velocityTracker.clear();
            velocityTracker.recycle();
            velocityTracker=null;
        }
    }
}

在activity中通过代码的形式创建自定义view,并加载到视图中

VeloctiyView veloctiyView=new VeloctiyView(this);
veloctiyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
veloctiyView.setBackgroundColor(Color.BLUE);
setContentView(veloctiyView);

打印效果如下:

05-09 00:07:04.988 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_DOWN
05-09 00:07:05.214 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
05-09 00:07:05.215 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: velx--->1938.670532,vely--->855.740784
05-09 00:07:05.230 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
05-09 00:07:05.230 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: velx--->3354.457764,vely--->2245.788818
05-09 00:07:05.247 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
05-09 00:07:05.247 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: velx--->2818.437500,vely--->2003.496094
05-09 00:07:05.266 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
05-09 00:07:05.266 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: velx--->2408.752930,vely--->1751.607788
05-09 00:07:05.281 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE

我们也可以使用GestureDetector来获得速度,同样是将触摸事件交给GestureDetector类来处理,我们只需要在回调中获得就可以了

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return true;
    }

    GestureDetector gestureDetector=new GestureDetector(getContext(), new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.e("VELO","velx--->"+velocityX+";velY--->"+velocityY);

            return false;
        }
    });

打印效果如下:

05-09 01:22:02.864 11115-11115/com.xiaguangcheng.bindsconnectionpooldemo E/VELO: velx--->9634.49;velY--->-5575.902
05-09 01:22:07.231 11115-11115/com.xiaguangcheng.bindsconnectionpooldemo E/VELO: velx--->-13790.847;velY--->14729.759
05-09 01:22:10.449 11115-11115/com.xiaguangcheng.bindsconnectionpooldemo E/VELO: velx--->359.31744;velY--->-235.8771
05-09 01:22:14.373 11115-11115/com.xiaguangcheng.bindsconnectionpooldemo E/VELO: velx--->-7355.338;velY--->-3538.2659

你可能感兴趣的:(VelocityTracker 测量手速的使用)