A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© y1787257661 中级黑马   /  2015-1-6 10:54  /  1431 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.finger.utils;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.content.Context;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.view.MotionEvent;
  8. import android.view.VelocityTracker;
  9. import android.view.ViewConfiguration;
  10. public class MultiTouchGestureDetector {
  11.     @SuppressWarnings("unused")
  12.     private static final String MYTAG = "Ray";
  13.     public static final String CLASS_NAME = "MultiTouchGestureDetector";
  14.     /**
  15.      * 事件信息类 <br/>
  16.      * 用来记录一个手势
  17.      */
  18.     private class EventInfo {
  19.         private MultiMotionEvent mCurrentDownEvent;    //当前的down事件
  20.         private MultiMotionEvent mPreviousUpEvent;    //上一次up事件
  21.         private boolean mStillDown;                    //当前手指是否还在屏幕上
  22.         private boolean mInLongPress;                //当前事件是否属于长按手势
  23.         private boolean mAlwaysInTapRegion;            //是否当前手指仅在小范围内移动,当手指仅在小范围内移动时,视为手指未曾移动过,不会触发onScroll手势
  24.         private boolean mAlwaysInBiggerTapRegion;    //是否当前手指在较大范围内移动,仅当此值为true时,双击手势才能成立
  25.         private boolean mIsDoubleTapping;            //当前手势,是否为双击手势
  26.         private float mLastMotionY;                    //最后一次事件的X坐标
  27.         private float mLastMotionX;                    //最后一次事件的Y坐标
  28.         private EventInfo(MotionEvent e) {
  29.             this(new MultiMotionEvent(e));
  30.         }
  31.         private EventInfo(MultiMotionEvent me) {
  32.             mCurrentDownEvent = me;
  33.             mStillDown = true;
  34.             mInLongPress = false;
  35.             mAlwaysInTapRegion = true;
  36.             mAlwaysInBiggerTapRegion = true;
  37.             mIsDoubleTapping = false;
  38.         }
  39.         //释放MotionEven对象,使系统能够继续使用它们
  40.         public void recycle() {
  41.             if (mCurrentDownEvent != null) {
  42.                 mCurrentDownEvent.recycle();
  43.                 mCurrentDownEvent = null;
  44.             }
  45.             if (mPreviousUpEvent != null) {
  46.                 mPreviousUpEvent.recycle();
  47.                 mPreviousUpEvent = null;
  48.             }
  49.         }
  50.         @Override
  51.         public void finalize() {
  52.             this.recycle();
  53.         }
  54.     }
  55.     /**
  56.      * 多点事件类 <br/>
  57.      * 将一个多点事件拆分为多个单点事件,并方便获得事件的绝对坐标
  58.      * <br/> 绝对坐标用以在界面中找到触点所在的控件
  59.      * @author ray-ni
  60.      */
  61.     public class MultiMotionEvent {
  62.         private MotionEvent mEvent;
  63.         private int mIndex;
  64.         private MultiMotionEvent(MotionEvent e) {
  65.             mEvent = e;
  66.             mIndex = (e.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;  //等效于 mEvent.getActionIndex();
  67.         }
  68.         private MultiMotionEvent(MotionEvent e, int idx) {
  69.             mEvent = e;
  70.             mIndex = idx;
  71.         }
  72.         // 行为
  73.         public int getAction() {
  74.             int action = mEvent.getAction() & MotionEvent.ACTION_MASK;    //等效于 mEvent.getActionMasked();
  75.             switch (action) {
  76.             case MotionEvent.ACTION_POINTER_DOWN:
  77.                 action = MotionEvent.ACTION_DOWN;
  78.                 break;
  79.             case MotionEvent.ACTION_POINTER_UP:
  80.                 action = MotionEvent.ACTION_UP;
  81.                 break;
  82.             }
  83.             return action;
  84.         }
  85.         // 返回X的绝对坐标
  86.         public float getX() {
  87.             return mEvent.getX(mIndex) + mEvent.getRawX() - mEvent.getX();
  88.         }
  89.         // 返回Y的绝对坐标
  90.         public float getY() {
  91.             return mEvent.getY(mIndex) + mEvent.getRawY() - mEvent.getY();
  92.         }
  93.         // 事件发生的时间
  94.         public long getEventTime() {
  95.             return mEvent.getEventTime();
  96.         }
  97.         // 事件序号
  98.         public int getIndex() {
  99.             return mIndex;
  100.         }
  101.      
  102.         // 事件ID
  103.         public int getId() {
  104.             return mEvent.getPointerId(mIndex);
  105.         }
  106.         
  107.         // 释放事件对象,使系统能够继续使用
  108.         public void recycle() {
  109.             if (mEvent != null) {
  110.                 mEvent.recycle();
  111.                 mEvent = null;
  112.             }
  113.         }
  114.     }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马