Android手势事件分发过程分析
2014-06-21 18:02
148 查看
Android在监听到屏幕上的手势事件后,会根据视图的层级结构从最外层的ViewGroup一直往下传递,事件可以在中途被某一层级的View截断而不再向下传,也就是消费了该事件。这个过程涉及到的方法主要是1、dispatchTouchEvent,2、onInterceptTouchEvent,3、onTouchEvent这三个方法,这三个方法什么含义呢,下面是具体的解释:
2、View只有onTouchEvent方法,ViewGroup添加了dispatchTouchEvent和onInterceptTouchEvent方法,也就是说容器才需要判断事件是否需要向下传递,view只需要处理事件就可以了,因为view已经是视图层里的做小单位了
3、View也可以设置onTouchListener(),里面有onTouch方法,如果一个View设置了onTouchListener,并且返回true,表明view消费了该事件,则不再执行onTouchEvent中的逻辑。
1、dispatchTouchEvent
dispatch是否分发事件,整个事件的驱动都在这个方法中,他会先调用自己的onInterceptTouchEvent,再递归调用child的dispatchTouchEvent,这样就可以将事件一直传下去,如果我们覆写了这个方法,而且没有调用super.dispatchTouchEvent(),也就是我们将事件调用的递归过程在这里截断了,child就不会收到该事件的传递,所以这里可以对事件的进行第一次拦截,这里返回true,表示该View已经消费了这个事件,事件传递结束;返回false,表示没有消费,事件返回父控件onTouchEven进行处理。@Override public boolean dispatchTouchEvent(MotionEvent ev) { if (mInputEventConsistencyVerifier != null) { mInputEventConsistencyVerifier.onTouchEvent(ev, 1); } boolean handled = false; if (onFilterTouchEventForSecurity(ev)) { final int action = ev.getAction(); final int actionMasked = action & MotionEvent.ACTION_MASK; // Handle an initial down. if (actionMasked == MotionEvent.ACTION_DOWN) { // Throw away all previous state when starting a new touch gesture. // The framework may have dropped the up or cancel event for the previous gesture // due to an app switch, ANR, or some other state change. cancelAndClearTouchTargets(ev); resetTouchState(); } // Check for interception. final boolean intercepted; if (actionMasked == MotionEvent.ACTION_DOWN || mFirstTouchTarget != null) { final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; if (!disallowIntercept) { ////////////////////////////////////////// // 这里调用自身的onInterceptTouchEvent(); // ////////////////////////////////////////// intercepted = onInterceptTouchEvent(ev); ev.setAction(action); // restore action in case it was changed } else { intercepted = false; } } else { // There are no touch targets and this action is not an initial down // so this view group continues to intercept touches. intercepted = true; } // Check for cancelation. final boolean canceled = resetCancelNextUpFlag(this) || actionMasked == MotionEvent.ACTION_CANCEL; // Update list of touch targets for pointer down, if needed. final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; TouchTarget newTouchTarget = null; boolean alreadyDispatchedToNewTouchTarget = false; if (!canceled && !intercepted) { if (actionMasked == MotionEvent.ACTION_DOWN || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { final int actionIndex = ev.getActionIndex(); // always 0 for down final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) : TouchTarget.ALL_POINTER_IDS; // Clean up earlier touch targets for this pointer id in case they // have become out of sync. removePointersFromTouchTargets(idBitsToAssign); final int childrenCount = mChildrenCount; if (newTouchTarget == null && childrenCount != 0) { final float x = ev.getX(actionIndex); final float y = ev.getY(actionIndex); // Find a child that can receive the event. // Scan children from front to back. final ArrayList<View> preorderedList = buildOrderedChildList(); final boolean customOrder = preorderedList == null && isChildrenDrawingOrderEnabled(); final View[] children = mChildren; /////////////////////////////////////////// // 这里开始递归子view的dispatchTouchEvent() // /////////////////////////////////////////// for (int i = childrenCount - 1; i >= 0; i--) { final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; final View child = (preorderedList == null) ? children[childIndex] : preorderedList.get(childIndex); if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) { continue; } newTouchTarget = getTouchTarget(child); if (newTouchTarget != null) { // Child is already receiving touch within its bounds. // Give it the new pointer in addition to the ones it is handling. newTouchTarget.pointerIdBits |= idBitsToAssign; break; } resetCancelNextUpFlag(child); ///////////////////////////////////////////////////////////////////// // dispatchTransformedTouchEvent里面调用了view的dispatchTouchEvent() // ///////////////////////////////////////////////////////////////////// if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { // Child wants to receive touch within its bounds. mLastTouchDownTime = ev.getDownTime(); if (preorderedList != null) { // childIndex points into presorted list, find original index for (int j = 0; j < childrenCount; j++) { if (children[childIndex] == mChildren[j]) { mLastTouchDownIndex = j; break; } } } else { mLastTouchDownIndex = childIndex; } mLastTouchDownX = ev.getX(); mLastTouchDownY = ev.getY(); newTouchTarget = addTouchTarget(child, idBitsToAssign); alreadyDispatchedToNewTouchTarget = true; break; } } if (preorderedList != null) preorderedList.clear(); } if (newTouchTarget == null && mFirstTouchTarget != null) { // Did not find a child to receive the event. // Assign the pointer to the least recently added target. newTouchTarget = mFirstTouchTarget; while (newTouchTarget.next != null) { newTouchTarget = newTouchTarget.next; } newTouchTarget.pointerIdBits |= idBitsToAssign; } } } // Dispatch to touch targets. if (mFirstTouchTarget == null) { // No touch targets so treat this as an ordinary view. handled = dispatchTransformedTouchEvent(ev, canceled, null, TouchTarget.ALL_POINTER_IDS); } else { // Dispatch to touch targets, excluding the new touch target if we already // dispatched to it. Cancel touch targets if necessary. TouchTarget predecessor = null; TouchTarget target = mFirstTouchTarget; while (target != null) { final TouchTarget next = target.next; if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { handled = true; } else { final boolean cancelChild = resetCancelNextUpFlag(target.child) || intercepted; if (dispatchTransformedTouchEvent(ev, cancelChild, target.child, target.pointerIdBits)) { handled = true; } if (cancelChild) { if (predecessor == null) { mFirstTouchTarget = next; } else { predecessor.next = next; } target.recycle(); target = next; continue; } } predecessor = target; target = next; } } // Update list of touch targets for pointer up or cancel, if needed. if (canceled || actionMasked == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { resetTouchState(); } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) { final int actionIndex = ev.getActionIndex(); final int idBitsToRemove = 1 << ev.getPointerId(actionIndex); removePointersFromTouchTargets(idBitsToRemove); } } if (!handled && mInputEventConsistencyVerifier != null) { mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1); } return handled; }
2、onInterceptTouchEvent
intercept意思是拦截、截断,所以该方法的作用就是判断是否要将这个事件断下来。返回true,事件截断,进入onTouchEvent消费事件;返回false,不进行拦截,事件继续向下传递,进入到子view的dispatchTouchEvent。3、onTouchEvent
代表是否该view消费了该事件,也就是是否对该事件的发生进行了相应的处理,返回true一个事件就到这里结束了,返回false说明事件没有被消费,事件向上返回到父控件的onTouchEvent方法。tips:
1、如果一个view没有捕获到ACTION_DOWN事件,或者捕获到DOWN事件后在onTouchEvent里返回了false,也就是没有消费该事件,那么将不会再收到后续事件。2、View只有onTouchEvent方法,ViewGroup添加了dispatchTouchEvent和onInterceptTouchEvent方法,也就是说容器才需要判断事件是否需要向下传递,view只需要处理事件就可以了,因为view已经是视图层里的做小单位了
3、View也可以设置onTouchListener(),里面有onTouch方法,如果一个View设置了onTouchListener,并且返回true,表明view消费了该事件,则不再执行onTouchEvent中的逻辑。
Reference
http://files.cnblogs.com/files/sunzn/PRE_andevcon_mastering-the-android-touch-system.pdf相关文章推荐
- 从源码角度带你分析 Android View 事件分发 dispatchTouchEvent,onTouch,onTouchEvent,onClick逻辑顺序过程(一)
- Android 从源码分析 Android 触摸事件分发过程
- Android VSync事件分发过程源码分析
- android 4.0.1 webkit Event 事件处理过程分析
- Android事件处理过程分析
- 【转】Android笔记:触摸事件的分析与总结----Touch事件分发方法dispatchTouchEvent()源码分析
- Android 从源码角度分析事件分发机制(三)
- Android Touch事件分发过程
- Android 4.0日历(calendar)源码分析之CalendarController(事件分发)
- Android Touch事件分发过程
- Android Touch事件分发过程
- Android Touch事件分发过程
- Android Touch事件的分发过程
- android 从源码分析ViewGroup事件分发
- android窗体加载过程剖析之事件的分发机制
- 分析Android的Touch事件分发机制
- 从系统源码角度分析Android事件分发
- Android事件分发机制详解(2)----分析ViewGruop的事件分发
- Android的Touch事件分发机制简单分析
- Android Touch事件的分发过程