zoukankan      html  css  js  c++  java
  • [学习总结]2、android中的VelocityTracker(获得速率用的类)

    参考资料:http://blog.jrj.com.cn/4586793646,5298605a.html 感谢这位兄弟!

    android.view.VelocityTracker主要用跟踪触摸屏事件(flinging事件和其他gestures手势事件)的速率,为up之后做一些效果用的。

    1,用obtain()函数来获得类的实例。

    2,常用的一些方法:

      2.1,使用addMovement(MotionEvent event)函数将当前的移动事件传递给VelocityTracker对象,参数是事件对象。

      2.2,使用 computeCurrentVelocity  (int units)函数来计算当前的速度  参数units:  你使用的速率单位1的意思是,以一毫秒运动了多少个像素的速率, 1000表示 一秒

        时间内运动了多少个像素。 单位是毫秒。

      2.3,computeCurrentVelocity(int units, float maxVelocity) 参数1同上,参数maxVelocity: 这个方法能计算出事件的最大速率。如果计算出来的速率大于maxVelocity

        则得到的速率就是maxVelocity,小于maxVelocity则为真正计算出来的速率。

        例如:CurrentVelocity =CurrentVelocity>maxVelocity?maxVelocity:CurrentVelocity;

      2.4,使用 getXVelocity  ()、 getYVelocity  ()函数来获得当前横向和纵向的速度

    3,例子demo:

      

     1 private VelocityTracker mVelocityTracker;//生命变量 
     2         
     3         //在onTouchEvent(MotionEvent ev)中 
     4 
     5 
     6         if (mVelocityTracker == null) { 
     7                 mVelocityTracker = VelocityTracker.obtain();//获得VelocityTracker类实例 
     8         } 
     9         mVelocityTracker.addMovement(ev);//将事件加入到VelocityTracker类实例中 
    10 
    11 
    12         //判断当ev事件是MotionEvent.ACTION_UP时:计算速率 
    13         final VelocityTracker velocityTracker = mVelocityTracker; 
    14         // 1000 provides pixels per second 
    15         velocityTracker.computeCurrentVelocity(1, (float)0.01); //设置maxVelocity值为0.1时,速率大于0.01时,显示的速率都是0.01,速率小于0.01时,显示正常 
    16         Log.i("test","velocityTraker"+velocityTracker.getXVelocity()); 
    17                                 
    18         velocityTracker.computeCurrentVelocity(1000); //设置units的值为1000,意思为一秒时间内运动了多少个像素 
    19         Log.i("test","velocityTraker"+velocityTracker.getXVelocity());
  • 相关阅读:
    临时表空间占用大量空间(新建)
    学习总结
    sql:表关联方式
    11gR2 Clusterware 和 Grid Home
    sql分析常用查询
    通过 SSH 实现 TCP / IP 隧道(端口转发):使用 OpenSSH 可能的 8 种场景
    Fabric部署环境初始化(Centos 7)
    Fabric 学习路线
    代币智能合约(go)
    springboot切面编程范例
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/3718280.html
Copyright © 2011-2022 走看看