zoukankan      html  css  js  c++  java
  • 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.   
    11. public class VelocityTrackerTest extends Activity {  
    12.     private TextView mInfo;  
    13.   
    14.     private VelocityTracker mVelocityTracker;  
    15.     private int mMaxVelocity;  
    16.   
    17.     private int mPointerId;  
    18.   
    19.     @Override  
    20.     protected void onCreate(Bundle savedInstanceState) {  
    21.         super.onCreate(savedInstanceState);  
    22.   
    23.         mInfo = new TextView(this);  
    24.         mInfo.setLines(4);  
    25.         mInfo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
    26.         mInfo.setTextColor(Color.WHITE);  
    27.   
    28.         setContentView(mInfo);  
    29.   
    30.         mMaxVelocity = ViewConfiguration.get(this).getMaximumFlingVelocity();  
    31.     }  
    32.   
    33.     @Override  
    34.     public boolean onTouchEvent(MotionEvent event) {  
    35.         final int action = event.getAction();  
    36.         acquireVelocityTracker(event);  
    37.         final VelocityTracker verTracker = mVelocityTracker;  
    38.         switch (action) {  
    39.             case MotionEvent.ACTION_DOWN:  
    40.                 //求第一个触点的id, 此时可能有多个触点,但至少一个  
    41.                 mPointerId = event.getPointerId(0);  
    42.                 break;  
    43.   
    44.             case MotionEvent.ACTION_MOVE:  
    45.                 //求伪瞬时速度  
    46.                 verTracker.computeCurrentVelocity(1000, mMaxVelocity);  
    47.                 final float velocityX = verTracker.getXVelocity(mPointerId);  
    48.                 final float velocityY = verTracker.getYVelocity(mPointerId);  
    49.                 recodeInfo(velocityX, velocityY);  
    50.                 break;  
    51.   
    52.             case MotionEvent.ACTION_UP:  
    53.                 releaseVelocityTracker();  
    54.                 break;  
    55.   
    56.             case MotionEvent.ACTION_CANCEL:  
    57.                 releaseVelocityTracker();  
    58.                 break;  
    59.   
    60.             default:  
    61.                 break;  
    62.         }  
    63.         return super.onTouchEvent(event);  
    64.     }  
    65.   
    66.     /**  
    67.      *  
    68.      * @param event 向VelocityTracker添加MotionEvent  
    69.      *  
    70.      * @see android.view.VelocityTracker#obtain()  
    71.      * @see android.view.VelocityTracker#addMovement(MotionEvent)  
    72.      */  
    73.     private void acquireVelocityTracker(final MotionEvent event) {  
    74.         if(null == mVelocityTracker) {  
    75.             mVelocityTracker = VelocityTracker.obtain();  
    76.         }  
    77.         mVelocityTracker.addMovement(event);  
    78.     }  
    79.   
    80.     /**  
    81.      * 释放VelocityTracker  
    82.      *  
    83.      * @see android.view.VelocityTracker#clear()  
    84.      * @see android.view.VelocityTracker#recycle()  
    85.      */  
    86.     private void releaseVelocityTracker() {  
    87.         if(null != mVelocityTracker) {  
    88.             mVelocityTracker.clear();  
    89.             mVelocityTracker.recycle();  
    90.             mVelocityTracker = null;  
    91.         }  
    92.     }  
    93.   
    94.     private static final String sFormatStr = "velocityX=%f velocityY=%f";  
    95.   
    96.     /**  
    97.      * 记录当前速度  
    98.      *  
    99.      * @param velocityX x轴速度  
    100.      * @param velocityY y轴速度  
    101.      */  
    102.     private void recodeInfo(final float velocityX, final float velocityY) {  
    103.         final String info = String.format(sFormatStr, velocityX, velocityY);  
    104.         mInfo.setText(info);  
    105.     }  
    106. }  

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

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

  • 相关阅读:
    UITextView in iOS7 doesn't scroll
    interlliJ idea 不识别文件类型的解决方式
    __super
    自用广告过滤规则,整合xwhyc大大的,非常小才79K
    Eclipse设置打印线
    SQL Server批量替换全部表中内容sql语句-清楚挂马
    删除同样元素(线性表)
    学习node js 之微信公众帐号接口开发 准备工作之三
    Bitmap基本概念及在Android4.4系统上使用BitmapFactory的注意事项
    多个rs485设备怎样跟上位机通讯?
  • 原文地址:https://www.cnblogs.com/lianghe01/p/4402923.html
Copyright © 2011-2022 走看看