安卓VelocityTracker使用小例子

本文介绍了安卓VelocityTracker的使用方法,包括如何获取实例、与MotionEvent关联、计算速度及回收资源等关键步骤,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安卓VelocityTracker使用小例子

定义:

VelocityTracker是位于android.view.VelocityTracker的一个工具类,用于监听滑动的速度,顾名思义Velocity(速度)Tracker(追踪者)

常用api

    /**
     * Retrieve a new VelocityTracker object to watch the velocity of a
     * motion.  Be sure to call {@link #recycle} when done.  You should
     * generally only maintain an active object while tracking a movement,
     * so that the VelocityTracker can be re-used elsewhere.
     *
     * @return Returns a new VelocityTracker.
     */
    static public VelocityTracker obtain() {
        VelocityTracker instance = sPool.acquire();
        return (instance != null) ? instance : new VelocityTracker(null);
    }

获取一个实例 VelocityTracker v= VelocityTracker.obtain();


已经获得初始化的VelocityTracker实例,那么就要和事件中的android.view.MotionEvent做关联,调用如下函数

    /**
     * Add a user's movement to the tracker.  You should call this for the
     * initial {@link MotionEvent#ACTION_DOWN}, the following
     * {@link MotionEvent#ACTION_MOVE} events that you receive, and the
     * final {@link MotionEvent#ACTION_UP}.  You can, however, call this
     * for whichever events you desire.
     * 
     * @param event The MotionEvent you received and would like to track.
     */
    public void addMovement(MotionEvent event) {
        if (event == null) {
            throw new IllegalArgumentException("event must not be null");
        }
        nativeAddMovement(mPtr, event);
    }

关联完毕后,就应该获取了,这个时候就可以先设置使用
public void computeCurrentVelocity(int units) ;
int 参数指的就是监测时间

    /**
     * Equivalent to invoking {@link #computeCurrentVelocity(int, float)} with a maximum
     * velocity of Float.MAX_VALUE.
     * 
     * @see #computeCurrentVelocity(int, float) 
     */
    public void computeCurrentVelocity(int units) {
        nativeComputeCurrentVelocity(mPtr, units, Float.MAX_VALUE);
    }

所有的准备工作都可以了,然后就能调用
getXVelocity(),getYVelocity()来获取速度了
函数原型,其他同理

    /**
     * Retrieve the last computed X velocity.  You must first call
     * {@link #computeCurrentVelocity(int)} before calling this function.
     * 
     * @return The previously computed X velocity.
     */
    public float getXVelocity() {
        return nativeGetXVelocity(mPtr, ACTIVE_POINTER_ID);
    }

最后一步就是记得回收,调用recycle()

     * Return a VelocityTracker object back to be re-used by others.  You must
     * not touch the object after calling this function.
     */
    public void recycle() {
        if (mStrategy == null) {
            clear();
            sPool.release(this);
        }
    }

简单示例:

VelocityTracker mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(/*要监听的MotionEvent*/);
mVelocityTracker.computeCurrentVelocity(1000);//设置时间
mVelocityTracker.getXVelocity());//获取x轴1000毫秒单位速度
mVelocityTracker.recycle();
//使用完毕回收资源,比如一个onTouch事件中开始是obtain获取实例
//,那么,在onTouch事件结束的时候就需要回收(recycle())
mVelocityTracker = null;

注意事项:
有时候如果没有通过obtain()获取就使用的话,会导致错误
必须先调用computeCurrentVelocity(毫秒时间),才能用get系列方法来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值