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


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

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

    用法详解:

    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() + "");

    在附上一个使用实例:

    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;
        }
    }

    转 http://blog.csdn.net/lonelyroamer/article/details/7560598

  • 相关阅读:
    linux 内核定时器 timer_list详解
    linux2.6源码分析之解压内核映像 head.s
    [C#]我自己写的一个对字节中每位进行修改值的函数
    Android Intent调用大全
    proguard 原理
    何为夫妻?何为家?何为幸福?
    生命只是瞬间,而有些人终究是过客?(转)
    bind端口复用
    在android开发中应该如何管理内存或者是在开发过程中应该注意哪些问题来较少OOM?
    W/ActivityManager( 1419): Activity is launching as a new task, so cancelling activity result.
  • 原文地址:https://www.cnblogs.com/622698abc/p/2983615.html
Copyright © 2020-2023  润新知