VelocityTracker.computeCurrentVelocity(intunits, float maxVelocity) Compute the current velocity based on the pointsthat have been collected. Only call this when you actually want toretrieve velocity information, as it is relatively expensive. Youcan then retrieve the velocity with getXVelocity() andgetYVelocity().
Parameters: units The units you would like the velocity in. A value of 1provides pixels per millisecond, 1000 provides pixels per second,etc. maxVelocity The maximum velocity that can be computed by thismethod. This value must be declared in the same unit as the unitsparameter. This value must be positive. 参数: units: 你使用的速率单位.1的意思是,以一毫秒运动了多少个像素的速率,1000表示 一秒时间内运动了多少个像素。 maxVelocity:这个方法能计算出事件的最大速率。他的值和上面的units的值具有一样的单位,这个值必须是正数。
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());