追踪触摸事件(flinging事件和其他手势事件)的速率。

来源:互联网 发布:solaris linux 区别 编辑:程序博客网 时间:2024/04/28 06:37
用来追踪触摸事件(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 pixels per 


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());


大体的使用是:
用来跟踪触摸速度的类
 
当你需要跟踪的时候使用obtain()方法来来获得VelocityTracker类的一个实例对象
 
使用addMovement(MotionEvent)函数将当前的移动事件传递给VelocityTracker对象
 
使用 computeCurrentVelocity  (int units)函数来计算当前的速度
 
使用 getXVelocity  ()、 getYVelocity  ()函数来获得当前的速度
0 0
原创粉丝点击