Android 手指滑动速度识别

/ 0评 / 1

速度追踪--VelocityTracker

方法摘要

1、 返回一个Velocity对象实例,必须确保调用recycle()当不再使用的时候,这样Velocity对象可以在其他地方使用。

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.

public static VelocityTracker obtain()

2、 回收Velocity对象给其他地方使用,你不能继续使用这个对象在调用此函数以后

Return a VelocityTracker object back to be re-used by others. You must not touch the object after calling this function.

public void recycle()

3、 重置Velocity对象

Reset the velocity tracker back to its initial state.

public void clear()

4、 将用户的手势加入追踪,你应该在MotionEvent.ACTION_DOWN中初始化,

Add a user’s movement to the tracker. You should call this for the initial MotionEvent.ACTION_DOWN, the following MotionEvent.ACTION_MOVE events that you receive, and the final MotionEvent.ACTION_UP. You can, however, call this for whichever events you desire.

public void addMovement(MotionEvent ev)

5、通过已经收集的点计算当前速度,只能在你想知道速度的时候调用,你能通过getXVelocity() 和 getYVelocity()获取具体的速度。

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 getXVelocity() and getYVelocity().

public void computeCurrentVelocity(int units)

6、返回X方向上的速度,必须先调用public void computeCurrentVelocity(int units)

Retrieve the last computed X velocity. You must first call computeCurrentVelocity(int) before calling this function.

public float getXVelocity()

7、返回Y方向上的速度,必须先调用public void computeCurrentVelocity(int units)

Retrieve the last computed Y velocity. You must first callcomputeCurrentVelocity(int) before calling this function.

public float getYVelocity()

使用过程

VelocityTracker主要用来追踪手指在滑动过程中的速度,包括水平速度和竖直方向的速度,它的使用过程很简单,首先在View的onTouchEvent方法中追踪当前单击事件的速度

VelocityTracker mVelocityTracker = VelocityTracker.obtain(); 
mVelocityTracker.addMovement(event);

接着如果我们想知道当前的滑动速度我们可以采用如下方式来获得当前的速度。

//mMaxVelocity 最大滚动速度 viewConfiguration.getScaledMaximumFlingVelocity()
mVelocityTracker.computeCurrentVelocity(1000, this.mMaxVelocity);
float xVelocity = mVelocityTracker.getXVelocity();
float yVelocity = mVelocityTracker.getYVelocity();

这一步中有两点需要注意,第一,获取速度前必须先计算速度,即使用mVelocityTracker.computeCurrentVelocity(1000); 计算手指在1000毫秒内运动的速度,第二点,速度可以为负数,当手指从右往左滑动时,水平方向速度为负值,速度计算公式为:速度 = (终点位置-起点位置) / 时间段,

最后当不需要使用的时候,需要调用recycle方法来重置并回收。

mVelocityTracker.recycle();

使用Demo

//定义对象
private VelocityTracker mVelocityTracker;

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (this.mVelocityTracker == null) {
        this.mVelocityTracker = VelocityTracker.obtain();
    }

    this.mVelocityTracker.addMovement(ev);

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if (this.mVelocityTracker != null) {
                this.mVelocityTracker.recycle();
                this.mVelocityTracker = null;
            }
            break;

    }
    return true;
}

其他方法

ViewConfiguration viewConfiguration = ViewConfiguration.get(getContext());
//表示滑动的时候,手的移动要大于这个距离才开始移动控件
scaledTouchSlop = viewConfiguration.getScaledTouchSlop();
//获得允许执行一个fling手势动作的最大速度值
scaledMaximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity();
//获得允许执行一个fling手势动作的最小速度值
scaledMinimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity();

View中两个回调函数:

/**
 * This is called when the view is attached to a window. At this point it
 * has a Surface and will start drawing. Note that this function is
 * guaranteed to be called before onDraw(android.graphics.Canvas), however
 * it may be called any time before the first onDraw -- including before or
 * after onMeasure(int, int).
 */
@Override
protected void onAttachedToWindow() {
    // TODO Auto-generated method stub
    super.onAttachedToWindow();
}

/**
 * This is called when the view is detached from a window. At this point it
 * no longer has a surface for drawing.
 */
@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注