VelocityTracker监控速度

 用来追踪触摸事件(flinging事件和其余手势事件)的速率。用obtain()函数来得到类的实例,用addMovement(MotionEvent)函数将motion event加入到VelocityTracker类实例中,当你使用到速率时,使用computeCurrentVelocity(int)初始化速率的单位,并得到当前的事件的速率,而后使用getXVelocity() 或getXVelocity()得到横向和竖向的速率。 ide

 

VelocityTracker.computeCurrentVelocity(int units, float maxVelocity) 函数

        计算那些已经发生触摸事件点的当前速率。这个函数只有在你须要获得速率消息的状况下才调用,由于使用它须要消耗很大的性能。经过getXVelocity()和getYVelocity()得到横向和竖向的速率。 性能

参数:
  units:  你使用的速率单位.1的意思是,以一毫秒运动了多少个像素的速率, 1000表示 一秒时间内运动了多少个像素。
spa

       maxVelocity: 这个方法能计算出事件的最大速率。他的值和上面的units的值具备同样的单位,这个值必须是正数。 事件

private VelocityTracker mVelocityTracker;//生命变量

//在onTouchEvent(MotionEvent ev)中


if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();//得到VelocityTracker类实例
}
mVelocityTracker.addMovement(ev);//将事件加入到VelocityTracker类实例中


//判断当ev事件是MotionEvent.ACTION_UP时:计算速率
final VelocityTracker velocityTracker = mVelocityTracker;
// 1000 provides pixels per second
velocityTracker.computeCurrentVelocity(1, (float)0.01); //设置maxVelocity值为0.1时,速率大于0.01时,显示的速率都是0.01,速率小于0.01时,显示正常
Log.i("test","velocityTraker"+velocityTracker.getXVelocity());

velocityTracker.computeCurrentVelocity(1000); //设置units的值为1000,意思为一秒时间内运动了多少个像素
ci

Log.i("test","velocityTraker"+velocityTracker.getXVelocity()); get

相关文章
相关标签/搜索