您的位置:首页 > 其它

滑轮控件研究四、VelocityTracker的简单研究

2014-07-23 19:36 357 查看
下面是Google对该类的描述:

[java]
view plaincopy

/**
* Helper for tracking the velocity of touch events, for implementing
* flinging and other such gestures. Use {@link #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 {@link #addMovement(MotionEvent)},
* and when you want to determine the velocity call
* {@link #computeCurrentVelocity(int)} and then {@link #getXVelocity()}
* and {@link #getXVelocity()}.
*/

简单翻译下:
帮助你追踪一个touch事件(flinging事件和其他手势事件)的速率。当你要跟踪一个touch事件的时候,使用obtain()方法得到这个类的实例,然后 用addMovement(MotionEvent)函数将你接受到的motion event加入到VelocityTracker类实例中。当你使用到速率时,使用computeCurrentVelocity(int)初始化速率的单位,并获得当前的事件的速率,然后使用getXVelocity() 或getXVelocity()获得横向和竖向的速率。

从上面的介绍中,我们就可以很简单的明白,如何去使用VelocityTracker类去追踪一个移动事件的速率。

用法详解:

[java]
view plaincopy

1、 //首先获得VelocityTracker的实例
/**
* obtain()的方法介绍
* Retrieve a new VelocityTracker object to watch the velocity of a motion.
* Be sure to call recycle() when done. You should generally only maintain
* an active object while tracking a movement, so that the VelocityTracker
* can be re-used elsewhere.
* 翻译:
* 得到一个速率追踪者对象去检测一个事件的速率。确认在完成的时候调用recycle()方法。
* 一般情况下,你只要维持一个活动的速率追踪者对象去追踪一个事件,那么,这个速率追踪者
* 可以在别的地方重复使用。
*/
VelocityTracker mVelocityTracker = null;
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}

2、 //假设有一个事件event,将事件加入到VelocityTracker类实例中
/**
* addMovement (MotionEvent event)方法介绍
* Add a user's movement to the tracker. You should call this for the initial
* ACTION_DOWN, the following ACTION_MOVE events that you receive,
* and the final ACTION_UP. You can, however, call this for whichever events
* you desire.
* 翻译:向速率追踪者中加入一个用户的移动事件,你应该最先在ACTION_DOWN调用这个方法,
* 然后在你接受的ACTION_MOVE,最后是ACTION_UP。你可以为任何一个你愿意的事件调用该方法
*/
mVelocityTracker.addMovement(event);

3、//判断当事件MotionEvent.ACTION_UP的时候,调用下面的方法
/**
* public void 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 {@link #getXVelocity()} and {@link #getYVelocity()}.
*
* @param units
* The units you would like the velocity in. A value of 1
* provides pixels per millisecond, 1000 provides pixels per
* second, etc.
* @param 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.
* 翻译:基于你所收集到的点计算当前的速率。 当你确定要获得速率信息的时候,在调用该方法,
* 因为使用它需要消耗很大的性能。然后,你可以通过getXVelocity()和getYVelocity()获得横向和竖向的速率。
*
* 参数:units 你想要指定的得到的速度单位,如果值为1,代表1毫秒运动了多少像素。如果值为1000,代表
* 1秒内运动了多少像素。
*
* 参数:maxVelocity 该方法所能得到的最大速度,这个速度必须和你指定的units使用同样的单位,而且
* 必须是整数。(也就是,你指定一个速度的最大值,如果计算超过这个最大值,就使用这个最大值,否则,使用计算的的结果)
*
* public void computeCurrentVelocity (int units)方法介绍
* 这个方法与上面的方法没什么差别,就是在maxVelocity上,他会自动使用Float.MAX_VALUE常量
*/
mVelocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);

4、//然后调用getXVelocity ()、getXVelocity (int id)、getYVelocity ()、getYVelocity (int id)得到速率
/**
* 调用这几个方法之前,必须确定你之前调用了computeCurrentVelocity方法。
* 参数 id 代表返回指定触点的速率
*/
Log.i("test", mVelocityTracker.getXVelocity() + "");
Log.i("test", mVelocityTracker.getYVelocity() + "");

在附上一个使用实例:

[java]
view plaincopy

public class GestureTestActivity extends Activity {
private GestureDetector gestureDetector;
private VelocityTracker mVelocityTracker = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this, new GestureListener());
gestureDetector.setIsLongpressEnabled(false);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.i("test", "ACTION_DOWN");
break;

case MotionEvent.ACTION_MOVE:// 移动的时候
Log.i("test", "ACTION_MOVE");
break;

case MotionEvent.ACTION_UP:
mVelocityTracker.computeCurrentVelocity(1000);
Log.i("test", "ACTION_UP");
Log.i("11111", mVelocityTracker.getXVelocity(0) + "");
Log.i("11111", mVelocityTracker.getYVelocity(0) + "");
break;
}
return gestureDetector.onTouchEvent(event);
}

// 继承于SimpleOnGestureListener,实现所有事件监听方法
private class GestureListener extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i("test", "onFling -----------------------");
Log.i("2222", velocityX + "");
Log.i("2222", velocityY + "");
return super.onFling(e1, e2, velocityX, velocityY);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
//释放
mVelocityTracker.recycle();
mVelocityTracker=null;
}
}

VelocityTracker的用法已经介绍完了,在我们的实际应用中,VelocityTracker类可能使用的不是很多,只需要了解上面的即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: