VelocityTracker

时间:2023-03-09 05:27:38
VelocityTracker

VelocityTracker顾名思义即速度跟踪,在android中主要应用于touch event, VelocityTracker通过跟踪一连串事件实时计算出

当前的速度,这样的用法在android系统空间中随处可见,比如Gestures中的Fling, Scrolling等,下面简单介绍一下用法。

  1. //获取一个VelocityTracker对象, 用完后记得回收
  2. //回收后代表你不需要使用了,系统将此对象在此分配到其他请求者
  3. static public VelocityTracker obtain();
  4. public void recycle();
  5. //计算当前速度, 其中units是单位表示, 1代表px/毫秒, 1000代表px/秒, ..
  6. //maxVelocity此次计算速度你想要的最大值
  7. public void computeCurrentVelocity(int units, float maxVelocity);
  8. //经过一次computeCurrentVelocity后你就可以用一下几个方法获取此次计算的值
  9. //id是touch event触摸点的ID, 来为多点触控标识,有这个标识在计算时可以忽略
  10. //其他触点干扰,当然干扰肯定是有的
  11. public float getXVelocity();
  12. public float getYVelocity();
  13. public float getXVelocity(int id);
  14. public float getYVelocity(int id);

下面是我写的一个简单Demo:

  1. package com.bxwu.demo.component.activity;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.MotionEvent;
  6. import android.view.VelocityTracker;
  7. import android.view.ViewConfiguration;
  8. import android.view.ViewGroup.LayoutParams;
  9. import android.widget.TextView;
  10. public class VelocityTrackerTest extends Activity {
  11. private TextView mInfo;
  12. private VelocityTracker mVelocityTracker;
  13. private int mMaxVelocity;
  14. private int mPointerId;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. mInfo = new TextView(this);
  19. mInfo.setLines(4);
  20. mInfo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  21. mInfo.setTextColor(Color.WHITE);
  22. setContentView(mInfo);
  23. mMaxVelocity = ViewConfiguration.get(this).getMaximumFlingVelocity();
  24. }
  25. @Override
  26. public boolean onTouchEvent(MotionEvent event) {
  27. final int action = event.getAction();
  28. acquireVelocityTracker(event);
  29. final VelocityTracker verTracker = mVelocityTracker;
  30. switch (action) {
  31. case MotionEvent.ACTION_DOWN:
  32. //求第一个触点的id, 此时可能有多个触点,但至少一个
  33. mPointerId = event.getPointerId(0);
  34. break;
  35. case MotionEvent.ACTION_MOVE:
  36. //求伪瞬时速度
  37. verTracker.computeCurrentVelocity(1000, mMaxVelocity);
  38. final float velocityX = verTracker.getXVelocity(mPointerId);
  39. final float velocityY = verTracker.getYVelocity(mPointerId);
  40. recodeInfo(velocityX, velocityY);
  41. break;
  42. case MotionEvent.ACTION_UP:
  43. releaseVelocityTracker();
  44. break;
  45. case MotionEvent.ACTION_CANCEL:
  46. releaseVelocityTracker();
  47. break;
  48. default:
  49. break;
  50. }
  51. return super.onTouchEvent(event);
  52. }
  53. /**
  54. *
  55. * @param event 向VelocityTracker添加MotionEvent
  56. *
  57. * @see android.view.VelocityTracker#obtain()
  58. * @see android.view.VelocityTracker#addMovement(MotionEvent)
  59. */
  60. private void acquireVelocityTracker(final MotionEvent event) {
  61. if(null == mVelocityTracker) {
  62. mVelocityTracker = VelocityTracker.obtain();
  63. }
  64. mVelocityTracker.addMovement(event);
  65. }
  66. /**
  67. * 释放VelocityTracker
  68. *
  69. * @see android.view.VelocityTracker#clear()
  70. * @see android.view.VelocityTracker#recycle()
  71. */
  72. private void releaseVelocityTracker() {
  73. if(null != mVelocityTracker) {
  74. mVelocityTracker.clear();
  75. mVelocityTracker.recycle();
  76. mVelocityTracker = null;
  77. }
  78. }
  79. private static final String sFormatStr = "velocityX=%f\nvelocityY=%f";
  80. /**
  81. * 记录当前速度
  82. *
  83. * @param velocityX x轴速度
  84. * @param velocityY y轴速度
  85. */
  86. private void recodeInfo(final float velocityX, final float velocityY) {
  87. final String info = String.format(sFormatStr, velocityX, velocityY);
  88. mInfo.setText(info);
  89. }
  90. }

代码很简单,我们可以求出move过程中的伪瞬时速度, 这样在做很多控件的时候都是可以用到的,比如系统Launcher的分页,

ScrollView滑动等, 可根据此时的速度来计算ACTION_UP后的减速运动等。实现一些非常棒的效果。