多点手势识别的兑现

多点手势识别的实现
    google提供的API中,有个类,大家都很熟悉,GestureDetector。使用它,我们可以识别用户通常会用的手势。但是,这个类不支持多点触摸(可能google认为没有人会在几个手指都在屏幕上的时候,使用手势吧~),不过,最近和朋友们一起做的一个App,的确用到了多点手势(主要是onScroll和onFling两个手势),所以,我就把这个类拓展了一下,来实现让多个控件各自跟着一跟手指实现拖动和滑动的效果。
   顺便说一下,大家应该都知道,在Android3.0以后,Android的触摸事件的分配机制和以前的版本是有区别的。从3.0开始,用户在不同控件上操作产生的touch消息不会相互干扰,touch消息会被分派到不同控件上的touchListener中处理。而
在以前的版本中,所有的touch消息,都会被分排到第一个碰到屏幕的手指所操作的控件的touchListener中处理,也就是说,会出现这样一个矛盾的现象:
   在界面上有A,B,C三个控件,然后,当你先用食指按住A,跟着又用中指和无名指(嘛,别的手指也行,不用在意中指还是无名指)按住B,C。当中指和无名指移动的时候,B和C都无法接收到这个ACTION_MOVE消息,而接收到消息的却是A。而在3.0以上版本中,并不存在这个问题。
    使用以下的这个类,可以实现从2.2到3.2平台上手势识别的兼容。
    开始贴代码:
    
package com.finger.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;

public class MultiTouchGestureDetector {

@SuppressWarnings("unused")
private static final String MYTAG = "Ray";
public static final String CLASS_NAME = "MultiTouchGestureDetector";

/**
 * 事件信息类 <br/>
 * 用来记录一个手势
 */
private class EventInfo {
private MultiMotionEvent mCurrentDownEvent; //当前的down事件
private MultiMotionEvent mPreviousUpEvent; //上一次up事件
private boolean mStillDown; //当前手指是否还在屏幕上
private boolean mInLongPress; //当前事件是否属于长按手势
private boolean mAlwaysInTapRegion; //是否当前手指仅在小范围内移动,当手指仅在小范围内移动时,视为手指未曾移动过,不会触发onScroll手势
private boolean mAlwaysInBiggerTapRegion; //是否当前手指在较大范围内移动,仅当此值为true时,双击手势才能成立
private boolean mIsDoubleTapping; //当前手势,是否为双击手势
private float mLastMotionY; //最后一次事件的X坐标
private float mLastMotionX; //最后一次事件的Y坐标

private EventInfo(MotionEvent e) {
this(new MultiMotionEvent(e));
}

private EventInfo(MultiMotionEvent me) {
mCurrentDownEvent = me;
mStillDown = true;
mInLongPress = false;
mAlwaysInTapRegion = true;
mAlwaysInBiggerTapRegion = true;
mIsDoubleTapping = false;
}

//释放MotionEven对象,使系统能够继续使用它们
public void recycle() {
if (mCurrentDownEvent != null) {
mCurrentDownEvent.recycle();
mCurrentDownEvent = null;
}

if (mPreviousUpEvent != null) {
mPreviousUpEvent.recycle();
mPreviousUpEvent = null;
}
}

@Override
public void finalize() {
this.recycle();
}
}

/**
 * 多点事件类 <br/>
 * 将一个多点事件拆分为多个单点事件,并方便获得事件的绝对坐标
 * <br/> 绝对坐标用以在界面中找到触点所在的控件
 * @author ray-ni
 */
public class MultiMotionEvent {
private MotionEvent mEvent;
private int mIndex;

private MultiMotionEvent(MotionEvent e) {
mEvent = e;
mIndex = (e.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;  //等效于 mEvent.getActionIndex();
}

private MultiMotionEvent(MotionEvent e, int idx) {
mEvent = e;