【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 三 )
Android 事件分發 系列文章目錄
【Android 事件分發】事件分發源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
【Android 事件分發】事件分發源碼分析 ( Activity 中各層級的事件傳遞 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 一 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 二 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 三 )
文章目錄
- Android 事件分發 系列文章目錄
- 前言
- 一、獲取子組件
- 二、當前遍歷的子組件的事件分發
- 三、ViewGroup 事件分發相關源碼
前言
接上一篇博客 【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 二 ) , 繼續分析 ViewGroup 的事件分發機制后續代碼 ;
一、獲取子組件
之前已經按照 Z 軸深度 , 將組件進行排序 , 放在集合中 ;
倒序遍歷排列好的組件 , 按照 Z 軸的上下順序 , 先遍歷的 Z 軸方向上 , 放在最上面的組件 , 也就是頂層組件 ;
for (int i = childrenCount - 1; i >= 0; i--) {先獲取組件索引 , 然后獲取索引對應的子組件 ;
// 獲取索引final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);// 獲取索引對應組件 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);然后判定組件是否符合要求 :
調用 canViewReceivePointerEvents 方法 , 判定組件是否可見 , 會否處于動畫中 ;
調用 isTransformedTouchPointInView 判定手指是否在控件上面 ;
/*** Returns true if a child view contains the specified point when transformed* into its coordinate space.* Child must not be null.* 判定手指是否觸摸到了組件 , 是否在組件區域范圍內 * @hide*/protected boolean isTransformedTouchPointInView(float x, float y, View child,PointF outLocalPoint) {// 獲取當前坐標 final float[] point = getTempPoint();point[0] = x;point[1] = y;transformPointToViewLocal(point, child);final boolean isInView = child.pointInView(point[0], point[1]);if (isInView && outLocalPoint != null) {outLocalPoint.set(point[0], point[1]);}return isInView;}如果上面 333 個條件只要存在 111 個不滿足 , 則不傳遞觸摸事件 , 在遍歷時直接 continue , 越過該控件 , 遍歷下一個控件 ;
// X 控件范圍 A , 如果手指按在 B 范圍 , 不會觸發 X 控件的事件 // 判定當前的組件是否可見 , 是否處于動畫過程中 // ① canViewReceivePointerEvents 判定組件是否可見 , 會否處于動畫 // ② isTransformedTouchPointInView 判定手指是否在控件上面 ; // 上述兩種情況 , 不觸發事件 if (!canViewReceivePointerEvents(child)|| !isTransformedTouchPointInView(x, y, child, null)) {ev.setTargetAccessibilityFocus(false);// 不觸發事件 continue;}ViewGroup | dispatchTouchEvent 方法相關源碼 :
@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {...// 倒序遍歷 按照 Z 軸的上下順序 , 排列好的組件 // 先遍歷的 Z 軸方向上 , 放在最上面的組件 , 也就是頂層組件 for (int i = childrenCount - 1; i >= 0; i--) {// 獲取索引final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);// 獲取索引對應組件 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);// If there is a view that has accessibility focus we want it// to get the event first and if not handled we will perform a// normal dispatch. We may do a double iteration but this is// safer given the timeframe.// 無障礙 輔助功能 if (childWithAccessibilityFocus != null) {if (childWithAccessibilityFocus != child) {continue;}childWithAccessibilityFocus = null;i = childrenCount - 1;}// X 控件范圍 A , 如果手指按在 B 范圍 , 不會觸發 X 控件的事件 // 判定當前的組件是否可見 , 是否處于動畫過程中 // ① canViewReceivePointerEvents 判定組件是否可見 , 會否處于動畫 // ② isTransformedTouchPointInView 判定手指是否在控件上面 ; // 上述兩種情況 , 不觸發事件 if (!canViewReceivePointerEvents(child)|| !isTransformedTouchPointInView(x, y, child, null)) {ev.setTargetAccessibilityFocus(false);// 不觸發事件 continue;}// 截止到此處 , 可以獲取子組件進行操作 ...}/*** Returns true if a child view can receive pointer events.* 判定控件是否可見 / 是否處于動畫中 * @hide*/private static boolean canViewReceivePointerEvents(@NonNull View child) {return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE|| child.getAnimation() != null;}/*** Returns true if a child view contains the specified point when transformed* into its coordinate space.* Child must not be null.* 判定手指是否觸摸到了組件 , 是否在組件區域范圍內 * @hide*/protected boolean isTransformedTouchPointInView(float x, float y, View child,PointF outLocalPoint) {// 獲取當前坐標 final float[] point = getTempPoint();point[0] = x;point[1] = y;transformPointToViewLocal(point, child);final boolean isInView = child.pointInView(point[0], point[1]);if (isInView && outLocalPoint != null) {outLocalPoint.set(point[0], point[1]);}return isInView;} }源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java
二、當前遍歷的子組件的事件分發
首先提取當前的子組件 , 第一次執行 getTouchTarget 代碼時 , 是沒有 mFirstTouchTarget 的 , 此時第一次返回 null ;
newTouchTarget = getTouchTarget(child);getTouchTarget 方法 , 判斷 mFirstTouchTarget 中的 child 字段 , 是否是當前遍歷的 子組件 View ;
如果是 , 則返回該 TouchTarget ;
如果不是 , 則返回空 ;
此時默認返回 null ;
調用 dispatchTransformedTouchEvent 正式開始分發觸摸事件 , 返回值只有兩種情況 ,
① 情況一 : 子控件觸摸事件返回 true
② 情況二 : 子控件觸摸事件返回 false
dispatchTransformedTouchEvent 方法 , 是正式分發觸摸事件的方法 , 注意參數中傳入了當前正在被遍歷的 child 子組件 ;
首先處理取消狀態 , 暫時不用分析 ;
// Canceling motions is a special case. We don't need to perform any transformations// or filtering. The important part is the action, not the contents.// 處理取消狀態 , 暫時不分析 ; final int oldAction = event.getAction();if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {event.setAction(MotionEvent.ACTION_CANCEL);if (child == null) {handled = super.dispatchTouchEvent(event);} else {handled = child.dispatchTouchEvent(event);}event.setAction(oldAction);return handled;}如果被遍歷的子組件為空 , 則調用父類的分發方法 , handled = super.dispatchTouchEvent(event); , 該分支很少執行 ;
如果被遍歷的子組件不為空 , 則調用子組件的分發方法 , handled = child.dispatchTouchEvent(event); , 子組件分發觸摸事件 , 此處調用的是 View 組件的 dispatchTouchEvent 方法 ;
ViewGroup | dispatchTouchEvent | dispatchTransformedTouchEvent 方法相關源碼 :
@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {...// 提取當前的子組件 // 第一次執行 getTouchTarget 代碼時 , 是沒有 mFirstTouchTarget 的// 此時第一次返回 null 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);// 正式開始分發觸摸事件// 處理以下兩種情況 : // ① 情況一 : 子控件觸摸事件返回 true // ② 情況二 : 子控件觸摸事件返回 false if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { ...}/*** Gets the touch target for specified child view.* Returns null if not found.* */private TouchTarget getTouchTarget(@NonNull View child) {// 判斷 mFirstTouchTarget 中的 child 字段 , 是否是當前遍歷的 子組件 View // 如果是 , 則返回該 TouchTarget // 如果不是 , 則返回空for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {if (target.child == child) {return target;}}return null;}/*** Transforms a motion event into the coordinate space of a particular child view,* filters out irrelevant pointer ids, and overrides its action if necessary.* If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.* 該方法是正式分發觸摸事件的方法 * 注意參數中傳入了當前正在被遍歷的 child 子組件 */private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,View child, int desiredPointerIdBits) {final boolean handled;// Canceling motions is a special case. We don't need to perform any transformations// or filtering. The important part is the action, not the contents.// 處理取消狀態 , 暫時不分析 ; final int oldAction = event.getAction();if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {event.setAction(MotionEvent.ACTION_CANCEL);if (child == null) {handled = super.dispatchTouchEvent(event);} else {handled = child.dispatchTouchEvent(event);}event.setAction(oldAction);return handled;}// Calculate the number of pointers to deliver.final int oldPointerIdBits = event.getPointerIdBits();final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;// If for some reason we ended up in an inconsistent state where it looks like we// might produce a motion event with no pointers in it, then drop the event.if (newPointerIdBits == 0) {return false;}// If the number of pointers is the same and we don't need to perform any fancy// irreversible transformations, then we can reuse the motion event for this// dispatch as long as we are careful to revert any changes we make.// Otherwise we need to make a copy.final MotionEvent transformedEvent;if (newPointerIdBits == oldPointerIdBits) {if (child == null || child.hasIdentityMatrix()) {if (child == null) {// 被遍歷的 child 子組件為空 // 調用父類的分發方法 handled = super.dispatchTouchEvent(event);} else {// 被遍歷的 child 子組件不為空 final float offsetX = mScrollX - child.mLeft;final float offsetY = mScrollY - child.mTop;event.offsetLocation(offsetX, offsetY);// 子組件分發觸摸事件 // 此處調用的是 View 組件的 dispatchTouchEvent 方法 ; handled = child.dispatchTouchEvent(event);event.offsetLocation(-offsetX, -offsetY);}return handled;}transformedEvent = MotionEvent.obtain(event);} else {transformedEvent = event.split(newPointerIdBits);}// Perform any necessary transformations and dispatch.if (child == null) {handled = super.dispatchTouchEvent(transformedEvent);} else {final float offsetX = mScrollX - child.mLeft;final float offsetY = mScrollY - child.mTop;transformedEvent.offsetLocation(offsetX, offsetY);if (! child.hasIdentityMatrix()) {transformedEvent.transform(child.getInverseMatrix());}handled = child.dispatchTouchEvent(transformedEvent);}// Done.transformedEvent.recycle();return handled;} }源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java
三、ViewGroup 事件分發相關源碼
ViewGroup 事件分發相關源碼 : 下面的代碼中 , 逐行注釋分析了 ViewGroup 的 dispatchTouchEvent 事件分發操作 ;
@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {// First touch target in the linked list of touch targets.private TouchTarget mFirstTouchTarget;@Overridepublic 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;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) {// 判斷是否需要攔截 , 可以使用 requestDisallowInterceptTouchEvent 方法進行設置final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;if (!disallowIntercept) {// 進行事件攔截 // 該 onInterceptTouchEvent 方法只返回是否進行事件攔截 , 返回一個布爾值 , 沒有進行具體的事件攔截 // 是否進行攔截 , 賦值給了 intercepted 局部變量 // 該值決定是否進行攔截 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;}// 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.// 檢查是否取消操作 , 手指是否移除了組件便捷 ; // 一般情況默認該值是 false ; 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;// 注意此處 newTouchTarget 為空 TouchTarget newTouchTarget = null;boolean alreadyDispatchedToNewTouchTarget = false;// 此處判定 , 是否攔截 // 假定不取消 , 也不攔截 // canceled 和 intercepted 二者都是 false , 才不能攔截 ; if (!canceled && !intercepted) {// If the event is targeting accessibility 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;// 判斷是否是按下操作 if (actionMasked == MotionEvent.ACTION_DOWN|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {// 獲取觸摸索引值 final int actionIndex = ev.getActionIndex(); // always 0 for downfinal 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);// 計算 ViewGroup 父容器下面有多少個子 View 組件 ; final int childrenCount = mChildrenCount;// TouchTarget newTouchTarget = null; 在上面聲明為空 , 此處肯定為 null ; // childrenCount 子組件個數不為 0 // 如果子組件個數為 0 , 則不走下一段代碼 , 如果子組件個數大于 0 , 則執行下一段代碼 ; // 說明下面的代碼塊中處理的是 ViewGroup 中子組件的事件分發功能 ; if (newTouchTarget == null && childrenCount != 0) {// 獲取單個手指的 x,y 坐標 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.// 子組件排序 , 按照 Z 軸排列的層級 , 從上到下進行排序 , // 控件會相互重疊 , Z 軸的排列次序上 , // 頂層的組件優先獲取到觸摸事件 final ArrayList<View> preorderedList = buildTouchDispatchChildList();final boolean customOrder = preorderedList == null&& isChildrenDrawingOrderEnabled();final View[] children = mChildren;// 倒序遍歷 按照 Z 軸的上下順序 , 排列好的組件 // 先遍歷的 Z 軸方向上 , 放在最上面的組件 , 也就是頂層組件 for (int i = childrenCount - 1; i >= 0; i--) {// 獲取索引final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);// 獲取索引對應組件 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);// If there is a view that has accessibility focus we want it// to get the event first and if not handled we will perform a// normal dispatch. We may do a double iteration but this is// safer given the timeframe.// 無障礙 輔助功能 if (childWithAccessibilityFocus != null) {if (childWithAccessibilityFocus != child) {continue;}childWithAccessibilityFocus = null;i = childrenCount - 1;}// X 控件范圍 A , 如果手指按在 B 范圍 , 不會觸發 X 控件的事件 // 判定當前的組件是否可見 , 是否處于動畫過程中 // ① canViewReceivePointerEvents 判定組件是否可見 , 會否處于動畫 // ② isTransformedTouchPointInView 判定手指是否在控件上面 ; // 上述兩種情況 , 不觸發事件 if (!canViewReceivePointerEvents(child)|| !isTransformedTouchPointInView(x, y, child, null)) {ev.setTargetAccessibilityFocus(false);// 不觸發事件 continue;}// 截止到此處 , 可以獲取子組件進行操作 // 提取當前的子組件 // 第一次執行 getTouchTarget 代碼時 , 是沒有 mFirstTouchTarget 的// 此時第一次返回 null 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);// 正式開始分發觸摸事件// 處理以下兩種情況 : // ① 情況一 : 子控件觸摸事件返回 true // ② 情況二 : 子控件觸摸事件返回 false 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 indexfor (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;}// The accessibility focus didn't handle the event, so clear// the flag and do a normal dispatch to all children.ev.setTargetAccessibilityFocus(false);}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;}@Overridepublic void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {// disallowIntercept 存在一個默認值 , 如果值為默認值 , 直接退出 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {// We're already in this state, assume our ancestors are tooreturn;}// 如果不是默認值 , 則進行相應更改 // 最終的值影響 mGroupFlags 是 true 還是 false if (disallowIntercept) {mGroupFlags |= FLAG_DISALLOW_INTERCEPT;} else {mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;}// Pass it up to our parentif (mParent != null) {mParent.requestDisallowInterceptTouchEvent(disallowIntercept);}}public boolean onInterceptTouchEvent(MotionEvent ev) {// 該方法只返回是否進行事件攔截 , 返回一個布爾值 , 沒有進行具體的事件攔截 if (ev.isFromSource(InputDevice.SOURCE_MOUSE)&& ev.getAction() == MotionEvent.ACTION_DOWN&& ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)&& isOnScrollbarThumb(ev.getX(), ev.getY())) {return true;}return false;}/*** Provide custom ordering of views in which the touch will be dispatched.* 按照事件傳遞的順序進行組件排序 ** This is called within a tight loop, so you are not allowed to allocate objects, including* the return array. Instead, you should return a pre-allocated list that will be cleared* after the dispatch is finished.* @hide*/public ArrayList<View> buildTouchDispatchChildList() {return buildOrderedChildList();}/*** Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children,* sorted first by Z, then by child drawing order (if applicable). This list must be cleared* after use to avoid leaking child Views.** Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated* children.*/ArrayList<View> buildOrderedChildList() {final int childrenCount = mChildrenCount;if (childrenCount <= 1 || !hasChildWithZ()) return null;if (mPreSortedChildren == null) {mPreSortedChildren = new ArrayList<>(childrenCount);} else {// callers should clear, so clear shouldn't be necessary, but for safety...mPreSortedChildren.clear();mPreSortedChildren.ensureCapacity(childrenCount);}final boolean customOrder = isChildrenDrawingOrderEnabled();// 下面的組件排序的核心邏輯 // 獲取當前所有組件的子組件的 Z 軸的深度 // 按照 Z 軸深度進行排序 // Z 軸方向上 , 對于事件傳遞 , 上面的組件優先級高于被覆蓋的下面的組件優先級for (int i = 0; i < childrenCount; i++) {// add next child (in child order) to end of listfinal int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);final View nextChild = mChildren[childIndex];final float currentZ = nextChild.getZ();// insert ahead of any Views with greater Z// 計算當前遍歷的組件應該被放到的索引位置int insertIndex = i;while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) {insertIndex--;}// 將當前遍歷的組件插入到指定索引位置上 mPreSortedChildren.add(insertIndex, nextChild);}return mPreSortedChildren;}// 獲取排序后的子組件的索引值private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) {final int childIndex;if (customOrder) {final int childIndex1 = getChildDrawingOrder(childrenCount, i);if (childIndex1 >= childrenCount) {throw new IndexOutOfBoundsException("getChildDrawingOrder() "+ "returned invalid index " + childIndex1+ " (child count is " + childrenCount + ")");}childIndex = childIndex1;} else {childIndex = i;}return childIndex;}// 獲取索引值對應的組件 private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children,int childIndex) {final View child;if (preorderedList != null) {child = preorderedList.get(childIndex);if (child == null) {throw new RuntimeException("Invalid preorderedList contained null child at index "+ childIndex);}} else {child = children[childIndex];}return child;}/*** Returns true if a child view can receive pointer events.* 判定控件是否可見 / 是否處于動畫中 * @hide*/private static boolean canViewReceivePointerEvents(@NonNull View child) {return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE|| child.getAnimation() != null;}/*** Returns true if a child view contains the specified point when transformed* into its coordinate space.* Child must not be null.* 判定手指是否觸摸到了組件 , 是否在組件區域范圍內 * @hide*/protected boolean isTransformedTouchPointInView(float x, float y, View child,PointF outLocalPoint) {// 獲取當前坐標 final float[] point = getTempPoint();point[0] = x;point[1] = y;transformPointToViewLocal(point, child);final boolean isInView = child.pointInView(point[0], point[1]);if (isInView && outLocalPoint != null) {outLocalPoint.set(point[0], point[1]);}return isInView;}/*** Gets the touch target for specified child view.* Returns null if not found.* */private TouchTarget getTouchTarget(@NonNull View child) {// 判斷 mFirstTouchTarget 中的 child 字段 , 是否是當前遍歷的 子組件 View // 如果是 , 則返回該 TouchTarget // 如果不是 , 則返回空for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {if (target.child == child) {return target;}}return null;}/*** Transforms a motion event into the coordinate space of a particular child view,* filters out irrelevant pointer ids, and overrides its action if necessary.* If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.* 該方法是正式分發觸摸事件的方法 * 注意參數中傳入了當前正在被遍歷的 child 子組件 */private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,View child, int desiredPointerIdBits) {final boolean handled;// Canceling motions is a special case. We don't need to perform any transformations// or filtering. The important part is the action, not the contents.// 處理取消狀態 , 暫時不分析 ; final int oldAction = event.getAction();if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {event.setAction(MotionEvent.ACTION_CANCEL);if (child == null) {handled = super.dispatchTouchEvent(event);} else {handled = child.dispatchTouchEvent(event);}event.setAction(oldAction);return handled;}// Calculate the number of pointers to deliver.final int oldPointerIdBits = event.getPointerIdBits();final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;// If for some reason we ended up in an inconsistent state where it looks like we// might produce a motion event with no pointers in it, then drop the event.if (newPointerIdBits == 0) {return false;}// If the number of pointers is the same and we don't need to perform any fancy// irreversible transformations, then we can reuse the motion event for this// dispatch as long as we are careful to revert any changes we make.// Otherwise we need to make a copy.final MotionEvent transformedEvent;if (newPointerIdBits == oldPointerIdBits) {if (child == null || child.hasIdentityMatrix()) {if (child == null) {// 被遍歷的 child 子組件為空 // 調用父類的分發方法 handled = super.dispatchTouchEvent(event);} else {// 被遍歷的 child 子組件不為空 final float offsetX = mScrollX - child.mLeft;final float offsetY = mScrollY - child.mTop;event.offsetLocation(offsetX, offsetY);// 子組件分發觸摸事件 // 此處調用的是 View 組件的 dispatchTouchEvent 方法 ; handled = child.dispatchTouchEvent(event);event.offsetLocation(-offsetX, -offsetY);}return handled;}transformedEvent = MotionEvent.obtain(event);} else {transformedEvent = event.split(newPointerIdBits);}// Perform any necessary transformations and dispatch.if (child == null) {handled = super.dispatchTouchEvent(transformedEvent);} else {final float offsetX = mScrollX - child.mLeft;final float offsetY = mScrollY - child.mTop;transformedEvent.offsetLocation(offsetX, offsetY);if (! child.hasIdentityMatrix()) {transformedEvent.transform(child.getInverseMatrix());}handled = child.dispatchTouchEvent(transformedEvent);}// Done.transformedEvent.recycle();return handled;}}源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java
總結
以上是生活随笔為你收集整理的【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 三 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 事件分发】事件分发源码
- 下一篇: 【Android 事件分发】事件分发源码