@Override public boolean dispatchTouchEvent(MotionEvent ev) { if (mInputEventConsistencyVerifier != null) { mInputEventConsistencyVerifier.onTouchEvent(ev, 1); } // If the event targets the accessibility focused view and this is it, start // normal event dispatch. Maybe a descendant is what will handle the click. if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) { ev.setTargetAccessibilityFocus(false); } boolean handled = false; //当View被遮挡时是否继续事件分发 if (onFilterTouchEventForSecurity(ev)) { final int action = ev.getAction(); final int actionMasked = action & MotionEvent.ACTION_MASK;//ACTION_MASK = 0xff // Handle an initial down. //一个事件时有DOWN开始,经过无数的MOVE,最后以UP结束 //所以如果为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. //把上一个事件取消,即发送Action为ACTION_CANCEL的事件给mFirstTouchTarget这个链表上的View //并且清空mFirstTouchTarget上的View,因为新的事件来了,要重置接受的View cancelAndClearTouchTargets(ev); resetTouchState(); } // Check for interception. //判断是否要拦截事件,即是否分发给Child View final boolean intercepted; if (actionMasked == MotionEvent.ACTION_DOWN || mFirstTouchTarget != null) { //requestDisallowInterceptTouchEvent(true)时 ---> disallowIntercept = true final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; if (!disallowIntercept) { 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; } //如果intercepted = true,意味着ViewGroup拦截了该事件,那么Child View将不会收到该事件 //反之Child View可以收到事件 // If intercepted, start normal event dispatch. Also if there is already // a view that is handling the gesture, do normal event dispatch. if (intercepted || mFirstTouchTarget != null) { ev.setTargetAccessibilityFocus(false); } // Check for cancelation. //检查是否这个事件已经被取消 final boolean canceled = resetCancelNextUpFlag(this) || actionMasked == MotionEvent.ACTION_CANCEL; // Update list of touch targets for pointer down, if needed. // 默认为true
,分发给多个View
(比如几个子View
位置重叠) final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; TouchTarget newTouchTarget = null; boolean alreadyDispatchedToNewTouchTarget = false; //事件没有被取消,并且当前ViewGroup没有拦截该事件, //那么执行下面的分发过程 if (!canceled && !intercepted) { // If the event is targeting accessiiblity focus we give it to the // view that has accessibility focus and if it does not handle it // we clear the flag and dispatch the event to all children as usual. // We are looking up the accessibility focused host to avoid keeping // state since these events are very rare. View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus() ? findChildWithAccessibilityFocus() : null; //这里特别注意,只有在DOWN的时候会执行,也就是平时我们知道的 //之后在对DOWN事件做了处理(拦截),在以后的事件序列我们才可以收的到, //因为后续的事件不会再判断哪些child会处理该事件,而仅仅是通过DOWN是 //保存在mFirstTouchTarget中的数据来分发,不再mFirstTouchTarget当然也就 //不能接收到事件了 //也就是说,如果ViewGroup的某个孩子没有接受ACTION_DOWN事件;那么, //ACTION_MOVE和ACTION_UP等事件也一定不会分发给这个孩子! if (actionMasked == MotionEvent.ACTION_DOWN || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { //获取touch 事件的index final int actionIndex = ev.getActionIndex(); // always 0 for down //ev.getPointerId() ==> //* Return the pointer identifier associated with a particular pointer //* data index is this event. The identifier tells you the actual pointer //* number associated with the data, accounting for individual pointers //* going up and down since the start of the current gesture. 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); //返回X方向坐标 final float y = ev.getY(actionIndex); // Find a child that can receive the event. // Scan children from front to back. final ArrayList
Android View 事件处理
2016-03-12