黑马程序员技术交流社区
标题:
Android 多点手势识别
[打印本页]
作者:
y1787257661
时间:
2015-1-6 10:54
标题:
Android 多点手势识别
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;
mIndex = idx;
}
// 行为
public int getAction() {
int action = mEvent.getAction() & MotionEvent.ACTION_MASK; //等效于 mEvent.getActionMasked();
switch (action) {
case MotionEvent.ACTION_POINTER_DOWN:
action = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_POINTER_UP:
action = MotionEvent.ACTION_UP;
break;
}
return action;
}
// 返回X的绝对坐标
public float getX() {
return mEvent.getX(mIndex) + mEvent.getRawX() - mEvent.getX();
}
// 返回Y的绝对坐标
public float getY() {
return mEvent.getY(mIndex) + mEvent.getRawY() - mEvent.getY();
}
// 事件发生的时间
public long getEventTime() {
return mEvent.getEventTime();
}
// 事件序号
public int getIndex() {
return mIndex;
}
// 事件ID
public int getId() {
return mEvent.getPointerId(mIndex);
}
// 释放事件对象,使系统能够继续使用
public void recycle() {
if (mEvent != null) {
mEvent.recycle();
mEvent = null;
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2