velocityTracker 速度跟踪器 手势事件

手势事件:滑动速度跟踪类VelocityTracker

1.android.view.VelocityTracker 主要是用跟踪触摸屏事件(flinging事件和其他gesture手势事件)的速率。

2.VelocityTracker顾名思义即速度跟踪,在android中主要用于touchEvent,VelocityTracker通过跟踪一连串事件实时计算出当前的速度,这样的用法在android系统空间中随处可见,比如说Gestures的Fling,Scroling等

3.经常用到的一些方法

   1)addMovement(MotionEvent)将MotionEvent加入到VelocityTracker类实例中

   2)getXVelocity()或者getYVelocity()获取横向和竖向的速度,

   3)computerCurrentVelocity(int)来初始化速率的单位

   但是在调用2)之前必须要调用3)

4.主要方法

 

   Public Methods

 

Public Methods
void addMovement(MotionEvent event)
Add a user's movement to the tracker.
void clear()
Reset the velocity tracker back to its initial state.
void computeCurrentVelocity(int units, float maxVelocity)
Compute the current velocity based on the points that have been collected.
int unitis表示速率的基本时间单位。unitis值为1的表示是,一毫秒时间单位内运动了多少个像素, unitis值为1000表示一秒(1000毫秒)时间单位内运动了多少个像素
floatVelocity表示速率的最大值
void computeCurrentVelocity(int units)
Equivalent to invoking computeCurrentVelocity(int, float) with a maximum velocity of Float.MAX_VALUE.
abstract T getNextPoolable()
float getXVelocity()
Retrieve the last computed X velocity.
float getXVelocity(int id)
Retrieve the last computed X velocity.
float getYVelocity(int id)
Retrieve the last computed Y velocity.
float getYVelocity()
Retrieve the last computed Y velocity.
abstract boolean isPooled()
static VelocityTracker obtain()
Retrieve a new VelocityTracker object to watch the velocity of a motion.
void recycle()
Return a VelocityTracker object back to be re-used by others.
abstract void setNextPoolable(T element)
abstract void setPooled(boolean isPooled)

 

 

 代码说明:

 1 //获取一个VelocityTracker对象, 用完后记得回收
 2 //回收后代表你不需要使用了,系统将此对象在此分配到其他请求者
 3 static public VelocityTracker obtain();
 4 public void recycle(); 
 5 //计算当前速度, 其中units是单位表示, 1代表px/毫秒, 1000代表px/秒, ..
 6 //maxVelocity此次计算速度你想要的最大值
 7 public void computeCurrentVelocity(int units, float maxVelocity);
 8 //经过一次computeCurrentVelocity后你就可以用一下几个方法获取此次计算的值
 9 //id是touch event触摸点的ID, 来为多点触控标识,有这个标识在计算时可以忽略
10 //其他触点干扰,当然干扰肯定是有的
11 public float getXVelocity();
12 public float getYVelocity();
13 public float getXVelocity(int id);
14 public float getYVelocity(int id);

 

 DEMO:

  1 package com.bxwu.demo.component.activity;
  2 import android.app.Activity;
  3 import android.graphics.Color;
  4 import android.os.Bundle;
  5 import android.view.MotionEvent;
  6 import android.view.VelocityTracker;
  7 import android.view.ViewConfiguration;
  8 import android.view.ViewGroup.LayoutParams;
  9 import android.widget.TextView;
 10 
 11 public class VelocityTrackerTest extends Activity {
 12     private TextView mInfo;
 13 
 14     private VelocityTracker mVelocityTracker;
 15     private int mMaxVelocity;
 16 
 17     private int mPointerId;
 18 
 19     @Override
 20     protected void onCreate(Bundle savedInstanceState) {
 21         super.onCreate(savedInstanceState);
 22 
 23         mInfo = new TextView(this);
 24         mInfo.setLines(4);
 25         mInfo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
 26         mInfo.setTextColor(Color.WHITE);
 27 
 28         setContentView(mInfo);
 29 
 30         mMaxVelocity = ViewConfiguration.get(this).getMaximumFlingVelocity();
 31     }
 32 
 33     @Override
 34     public boolean onTouchEvent(MotionEvent event) {
 35         final int action = event.getAction();
 36         acquireVelocityTracker(event);
 37         final VelocityTracker verTracker = mVelocityTracker;
 38         switch (action) {
 39             case MotionEvent.ACTION_DOWN:
 40                 //求第一个触点的id, 此时可能有多个触点,但至少一个
 41                 mPointerId = event.getPointerId(0);
 42                 break;
 43 
 44             case MotionEvent.ACTION_MOVE:
 45                 //求伪瞬时速度
 46                 verTracker.computeCurrentVelocity(1000, mMaxVelocity);
 47                 final float velocityX = verTracker.getXVelocity(mPointerId);
 48                 final float velocityY = verTracker.getYVelocity(mPointerId);
 49                 recodeInfo(velocityX, velocityY);
 50                 break;
 51 
 52             case MotionEvent.ACTION_UP:
 53                 releaseVelocityTracker();
 54                 break;
 55 
 56             case MotionEvent.ACTION_CANCEL:
 57                 releaseVelocityTracker();
 58                 break;
 59 
 60             default:
 61                 break;
 62         }
 63         return super.onTouchEvent(event);
 64     }
 65 
 66     /**
 67      *
 68      * @param event 向VelocityTracker添加MotionEvent
 69      *
 70      * @see android.view.VelocityTracker#obtain()
 71      * @see android.view.VelocityTracker#addMovement(MotionEvent)
 72      */
 73     private void acquireVelocityTracker(final MotionEvent event) {
 74         if(null == mVelocityTracker) {
 75             mVelocityTracker = VelocityTracker.obtain();
 76         }
 77         mVelocityTracker.addMovement(event);
 78     }
 79 
 80     /**
 81      * 释放VelocityTracker
 82      *
 83      * @see android.view.VelocityTracker#clear()
 84      * @see android.view.VelocityTracker#recycle()
 85      */
 86     private void releaseVelocityTracker() {
 87         if(null != mVelocityTracker) {
 88             mVelocityTracker.clear();
 89             mVelocityTracker.recycle();
 90             mVelocityTracker = null;
 91         }
 92     }
 93 
 94     private static final String sFormatStr = "velocityX=%f\nvelocityY=%f";
 95 
 96     /**
 97      * 记录当前速度
 98      *
 99      * @param velocityX x轴速度
100      * @param velocityY y轴速度
101      */
102     private void recodeInfo(final float velocityX, final float velocityY) {
103         final String info = String.format(sFormatStr, velocityX, velocityY);
104         mInfo.setText(info);
105     }
106 }
View Code

 

我们可以求出move过程中的伪瞬时速度, 这样在做很多控件的时候都是可以用到的,比如系统Launcher的分页,

ScrollView滑动等, 可根据此时的速度来计算ACTION_UP后的减速运动等。实现一些非常棒的效果。

实例参考:http://blog.csdn.net/bingxianwu/article/details/7446799

posted @ 2013-07-05 11:39  a_gray_lark  阅读(454)  评论(0)    收藏  举报