VelocityTracker 使用

来源:互联网 发布:网络用语rj是什么意思 编辑:程序博客网 时间:2024/04/27 21:13

VelocityTracker类:


这个类的话是用来得到手势在屏幕上滑动的速度,也许我们用的比较少,但是还是在这里写下怎样使用VelocityTracker这个类,

        第一步的话,当然是得到该类的一个实例 mVelocityTracker = VelocityTracker.obtain();

        第二步,需要告诉mVelocityTracker 这个对象你要对那个MotionEvent 进行监控(也就是说得到在那个MotionEvent
上的速度) 对于MotionEvent 的解释文档是这么说的 {对象,用于报告运动(鼠标,笔,手指,轨迹球事件这个类可能持有绝对或相对运动}使用

 mVelocityTracker.addMovement(ev); 方法来制定一个MotionEvent。

       第三步, 这一步就相当简单了直接用 mVelocityTracker.getXVelocity(); 方法获取在水平方向上滑动的速度 用 mVelocityTracker.getYVelocity(); 方法就可以获取垂直方向上滑动的速度了。

 

最后的话我附上一个完整的例子让大家看的更明白点吧!

         

         VelocityTracker mVelocityTracker = VelocityTracker.obtain();
        
        mVelocityTracker.addMovement(ev);

        if (ev.getAction() == MotionEvent.ACTION_UP) {
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int velocityX = (int) mVelocityTracker.getXVelocity();
            final int velocityY = (int) mVelocityTracker.getYVelocity();

            if (mListener != null) {
             int Direction = FLING_NONE;
             if (velocityX > SNAP_VELOCITY) {
              Direction = FLING_LEFT;
             } else if (velocityX < -SNAP_VELOCITY) {
              Direction = FLING_RIGHT;
             } else if (velocityY > SNAP_VELOCITY) {
              Direction = FLING_DOWN;
             } else if (velocityY < -SNAP_VELOCITY) {
              Direction = FLING_UP;
             }
                       }



       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.

原创粉丝点击