/** * 触发长按事件 * @param info * @param idx */ private void dispatchLongPress(EventInfo info, int idx) { mHandler.removeMessages(TAP_SINGLE, idx);//移除单击事件确认 info.mInLongPress = true; mListener.onLongPress(info.mCurrentDownEvent); } /** * 构造器1 * @param context * @param listener */ public MultiTouchGestureDetector(Context context, MultiTouchGestureListener listener) { this(context, listener, null); } /** * 构造器2 * @param context * @param listener * @param handler */ public MultiTouchGestureDetector(Context context, MultiTouchGestureListener listener, Handler handler) { if (handler != null) { mHandler = new GestureHandler(handler); } else { mHandler = new GestureHandler(); } mListener = listener; if (listener instanceof MultiTouchDoubleTapListener) { setOnDoubleTapListener((MultiTouchDoubleTapListener) listener); } init(context); } /** * 初始化识别器 * @param context */ private void init(Context context) { if (mListener == null) { throw new NullPointerException("OnGestureListener must not be null"); } mIsLongpressEnabled = true; int touchSlop, doubleTapSlop; if (context == null) { touchSlop = ViewConfiguration.getTouchSlop(); doubleTapSlop = DOUBLE_TAP_SLAP; mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity(); mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity(); } else {//允许识别器在App中,使用偏好的设定 final ViewConfiguration configuration = ViewConfiguration.get(context); touchSlop = configuration.getScaledTouchSlop(); doubleTapSlop = configuration.getScaledDoubleTapSlop(); mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity(); mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity(); } mTouchSlopSquare = touchSlop * touchSlop / 16; mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop; } /** * 设置双击监听器 * @param onDoubleTapListener */ public void setOnDoubleTapListener(MultiTouchDoubleTapListener onDoubleTapListener) { mDoubleTapListener = onDoubleTapListener; } /** * 设置是否允许长按 * @param isLongpressEnabled */ public void setIsLongpressEnabled(boolean isLongpressEnabled) { mIsLongpressEnabled = isLongpressEnabled; } /** * 判断是否允许长按 * @return */ public boolean isLongpressEnabled() { return mIsLongpressEnabled; } /** * 判断当前事件是否为双击事件 * <br/> 通过遍历sEventForDoubleTap来匹配是否存在能够构成双击事件的单击事件 * @param e * @return */ private EventInfo checkForDoubleTap(MultiMotionEvent e) { if (sEventForDoubleTap.isEmpty()) { // Log.e(MYTAG, CLASS_NAME + ":checkForDoubleTap(), sEventForDoubleTap is empty !"); return null; } for (int i = 0; i < sEventForDoubleTap.size(); i++) { EventInfo info = sEventForDoubleTap.get(i); if (info != null && isConsideredDoubleTap(info, e)) { sEventForDoubleTap.set(i, null);// 这个单击事件已经被消耗了,所以置为null mHandler.removeMessages(TAP_DOUBLE, i);// 移除Handler内的为处理消息 return info; } } return null; } /** * 判断当前按下事件是否能和指定的单击事件构成双击事件 * * @param info * @param secondDown * @return */ private boolean isConsideredDoubleTap(EventInfo info, MultiMotionEvent secondDown) { if (!info.mAlwaysInBiggerTapRegion) { //如多第一次单击事件有过较大距离的移动,则无法构成双击事件 return false; } if (secondDown.getEventTime() - info.mPreviousUpEvent.getEventTime() > DOUBLE_TAP_TIMEOUT) { //如果第一次单击的UP时间和第二次单击的down时间时间间隔大于DOUBLE_TAP_TIMEOUT,也无法构成双击事件 return false; } int deltaX = (int) info.mCurrentDownEvent.getX() - (int) secondDown.getX(); int deltaY = (int) info.mCurrentDownEvent.getY() - (int) secondDown.getY(); return (deltaX * deltaX + deltaY * deltaY < mDoubleTapSlopSquare);//最后判断两次单击事件的距离 } /** * 将事件信息放入双击判断队列,并返回序号 * * @param info * @return */ private int addIntoTheMinIndex(EventInfo info) { for (int i = 0; i < sEventForDoubleTap.size(); i++) { if (sEventForDoubleTap.get(i) == null) { sEventForDoubleTap.set(i, info); return i; } } sEventForDoubleTap.add(info); return sEventForDoubleTap.size() - 1; } /** * 从事件信息队列中移除指定序号的事件 * * @param idx */ private void removeEventFromList(int id) { if (id > sEventInfos.size() || id < 0) { // Log.e(MYTAG, CLASS_NAME + ".removeEventFromList(), id=" + id + ", while sEventInfos.size() =" + sEventInfos.size()); return; } sEventInfos.set(id, null); } /** * 向事件队列中添加新信息 * * @param e */ private void addEventIntoList(EventInfo info) { int id = info.mCurrentDownEvent.getId(); if (id < sEventInfos.size()) { // if (sEventInfos.get(id) != null) // Log.e(MYTAG, CLASS_NAME + ".addEventIntoList, info(" + id + ") has not set to null !"); sEventInfos.set(info.mCurrentDownEvent.getId(), info); } else if (id == sEventInfos.size()) { sEventInfos.add(info); } else { // Log.e(MYTAG, CLASS_NAME + ".addEventIntoList, invalidata id !"); } }
上一页 [1] [2] [3] 下一页