作用:帮助去跟踪屏幕上的flinging事件,个人认为可能就是手指的触摸移动事件
使用方法:1 调用 VelocityTracker mVelocityTracker = VelocityTracker.obtain(); 实例化对象
2 调用 mVelocityTracker.addMovement(event); ,为对象添加监听的事件
3 调用 mVelocityTracker.computeCurrentVelocity(1, (float)0.01); 计算速度
4 mVelocityTracker.getXVelocity( ); 和 mVelocityTracker.getYVelocity( ); 计算X和Y的速度(可以传递参数来指定移动对象)
5 使用结束调用 mVelocityTracker.clear(); 和 mVelocityTracker.recycle(); 来清理对象
测试代码:
public class VelocityTrackerTestActivity extends Activity {
private TextView mInfo;
private VelocityTracker mVelocityTracker;
private int mMaxVelocity;
private int mPointerId;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.velocity_trackerl );
mInfo = (TextView) findViewById( R.id.text);
// mInfo = new TextView(this);
// mInfo.setLines(4);
// mInfo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// mInfo.setTextColor(Color.WHITE);
// this.addContentView(mInfo , null);
// setContentView(mInfo);
mMaxVelocity = ViewConfiguration.get(this).getMaximumFlingVelocity();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
acquireVelocityTracker(event);
switch (action) {
case MotionEvent.ACTION_DOWN:
//求第一个触点的id, 此时可能有多个触点,但至少一个
mPointerId = event.getPointerId(0);
break;
case MotionEvent.ACTION_MOVE:
//求伪瞬时速度
// mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
mVelocityTracker.computeCurrentVelocity(1, (float)0.01);
final float velocityX = mVelocityTracker.getXVelocity(mPointerId);
final float velocityY = mVelocityTracker.getYVelocity(mPointerId);
recodeInfo(velocityX, velocityY);
break;
case MotionEvent.ACTION_UP:
releaseVelocityTracker();
break;
case MotionEvent.ACTION_CANCEL:
releaseVelocityTracker();
break;
default:
break;
}
return super.onTouchEvent(event);
}
private void acquireVelocityTracker(final MotionEvent event) {
if(null == mVelocityTracker) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
}
/**
* 释放VelocityTracker
*
* @see android.view.VelocityTracker#clear()
* @see android.view.VelocityTracker#recycle()
*/
private void releaseVelocityTracker() {
if(null != mVelocityTracker) {
mVelocityTracker.clear();
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
private static final String sFormatStr = "velocityX=%f\nvelocityY=%f";
/**
* 记录当前速度
*
* @param velocityX x轴速度
* @param velocityY y轴速度
*/
private void recodeInfo(final float velocityX, final float velocityY) {
final String info = String.format(sFormatStr, velocityX, velocityY);
mInfo.setText(info);
}
}
2 xml
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="top"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="富字体样式"/>
</LinearLayout>