Android基础进阶之----手势跟踪

这个课程描述了如何在触摸事件中跟踪移动 

 

无论当前触摸接触点的位置,压力,或者大小的变化,onTouchEvenet()方法被一个ACTION_MOVE事件触发。正如在Detecting Common Gestures中描述,所有的这些事件都被记录在onTouchEvent()方法的MotionEvent参数中 

 

因为基于手指的触摸不总是最精确的交互形式,检测触摸时间经常是基于移动而不是简单的触摸。为了帮助应用程序区分在基于移动的手势(例如swip)和没有移动的手势(例如一个单独的轻敲),Android包含”触摸溢出“的概念。触摸溢出在手势被翻译为一个基于移动的手势之前,引用了用户的触摸移动的像素距离。更多这个话题的讨论,查阅Managing Touch Evenet in a ViewGroup 

 

这里有多种不同的方式来在一个手势中跟踪移动,基于你的应用程序的需要。例如 

  • 一个点的开始和结束的位置(例如,移动一个屏幕对象,从点A到点B)。 

  • 点移动的方向,通过x和y坐标来被决定 

  • 历史。通过调用MotionEvent方法getHistorySize()方法,你能找到这个手势的历史的大小。你然后通过这个运动事件的getHistorical<Value>方法,能获得每个历史事件的位置,大小,时间,和压力。当描绘一个用户的手指的轨迹的时候,历史是非常有用的,例如触摸绘制。查看MotionEvent参考详情。 

  • 这个点它在屏幕上移动的速率 

 

跟踪速度 

————————————————————————————————————————————————————————————————— 

你能拥有一个基于移动的手势,它仅仅基于点移动的距离和/或者方向。但是速率经常是跟踪一个手势的特征,或者甚至是决定一个手势是否发生的决定因素。为了使速率计算简单,Android在支持库中提供了VelocityTracker类和VelocityTrackerCompat类。VelocityTracker帮助你跟踪这个触摸事件的速率。这对于手势是非常有用的,在速率是这个手势的准则的时候,例如一个fling。 

 

下面是一个简单示例,它说明了在VelocityTracker API中方法的目的 

public class MainActivity extends Activity {
    private static final String DEBUG_TAG = "Velocity";
        ...
    private VelocityTracker mVelocityTracker = null;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);

        switch(action) {
            case MotionEvent.ACTION_DOWN:
                if(mVelocityTracker == null) {
                    // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    // Reset the velocity tracker back to its initial state.
                    mVelocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
                // When you want to determine the velocity, call 
                // computeCurrentVelocity(). Then call getXVelocity() 
                // and getYVelocity() to retrieve the velocity for each pointer ID. 
                mVelocityTracker.computeCurrentVelocity(1000);
                // Log velocity of pixels per second
                // Best practice to use VelocityTrackerCompat where possible.
                Log.d("", "X velocity: " + 
                        VelocityTrackerCompat.getXVelocity(mVelocityTracker, 
                        pointerId));
                Log.d("", "Y velocity: " + 
                        VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                        pointerId));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // Return a VelocityTracker object back to be re-used by others.
                mVelocityTracker.recycle();
                break;
        }
        return true;
    }
}


ps:翻译 自 http://developer.android.com/training/gestures/movement.html

转载自 http://blog.csdn.net/p106786860/article/details/17347501


public void computeCurrentVelocity (int units)
Added in  API level 1

Equivalent to invoking computeCurrentVelocity(int, float) with a maximum velocity of Float.MAX_VALUE.

public void computeCurrentVelocity (int units, float maxVelocity)
Added in  API level 4

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().


Parameters
units The units you would like the velocity in. A value of 1 provides pixels per millisecond, 1000 provides pixels per second, etc.   (表示速率的基本时间单位。unitis值为1的表示是,一毫秒时间单位内运动了多少个像素, unitis值为1000表示一秒(1000毫秒)时间单位内运动了多少个像素
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.


ps: VelocityTracker中这些事件的velocity 的速度的计算都是通过,native的方法的实现。


参考:


http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1117/574.html




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值