VelocityTracker类

来源:互联网 发布:淘宝淘金币怎么没有了 编辑:程序博客网 时间:2024/05/16 01:31

VelocityTracker类:


       Helper for tracking the velocity of touch events, for implementing flinging and other such gestures. Use obtain to retrieve a new instance of the class when you are  going to begin tracking, put the motion events you receive into it with addMovement(MotionEvent), and when you want to determine the velocity call computeCurrentVelocity(int) and then getXVelocity() and getXVelocity().

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


VelocityTracker.computeCurrentVelocity(int units, float maxVelocity)

        Compute the current velocity based on the points that have been collected. Only call this when you actually want to retrieve velocity information, as it is relatively expensive. You can then retrieve the velocity with getXVelocity() and getYVelocity().

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


Parameters:
        units The units you would like the velocity in. A value of 1 provides pixels per millisecond, 1000 provides pixelsper second, etc.
        maxVelocity The maximum velocity that can be computed by this method. This value must be declared in the same unit as the units parameter. This value must be positive.
参数:
  units:  你使用的速率单位.1的意思是,以一毫秒运动了多少个像素的速率, 1000表示 一秒时间内运动了多少个像素。

       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,意思为一秒时间内运动了多少个像素

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


   那位英语好的朋友,帮忙分析一下,下面这句怎么翻译, The units you would like the velocity in.

原创粉丝点击