【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 五 )
Android 事件分發 系列文章目錄
【Android 事件分發】事件分發源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
【Android 事件分發】事件分發源碼分析 ( Activity 中各層級的事件傳遞 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 一 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 二 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 三 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 四 | View 事件傳遞機制 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 五 )
文章目錄
- Android 事件分發 系列文章目錄
- 前言
- 一、ViewGroup | dispatchTransformedTouchEvent 方法返回值含義
- 二、記錄被消費的觸摸事件
- 三、觸摸事件沒有被消費的調用鏈分析
- 四、觸摸事件被攔截的調用鏈分析
- 五、ViewGroup 事件分發相關源碼
- 六、View 事件分發相關源碼
- 總結
前言
接上一篇博客 【Android 事件分發】事件分發源碼分析 ( View 事件傳遞機制 ) , 繼續分析 ViewGroup 的事件分發機制后續代碼 ;
Activity 事件傳遞 , 最終調用到了 ViewGroup 的 dispatchTouchEvent 方法 , 在該方法中有調用了 View 的 dispatchTouchEvent 方法 ;
View 的 dispatchTouchEvent 調用完畢后 , 會返回 true 或 false 結果 ;
那么繼續回到 ViewGroup 的 dispatchTransformedTouchEvent 方法 , 在該方法中 , 也就是遍歷 ViewGroup 子組件時 , 調用遍歷的子組件 View 的 dispatchTouchEvent 方法 , 處理相關觸摸和點擊相關的操作 ;
本博客繼續分析 ViewGroup 的后續調用鏈 ;
一、ViewGroup | dispatchTransformedTouchEvent 方法返回值含義
在 ViewGroup | dispatchTouchEvent 方法中 , 調用 ViewGroup | dispatchTransformedTouchEvent 方法進行事件的轉發 , 主要是遍歷子組件調用子組件的觸摸方法 ;
在 ViewGroup | dispatchTransformedTouchEvent 方法中 , 調用了 View 子組件的 View | dispatchTouchEvent 方法 , 該方法返回 true/false , 那么對應的 ViewGroup | dispatchTransformedTouchEvent 方法也會返回相同的值 ;
由下面的代碼可知 , handled 是子組件 child.dispatchTouchEvent 調用返回值 , 返回結果就是 handled , 然后有將該值直接返回 , 說明 View 子組件的 dispatchTouchEvent 調用結果 , 就是 ViewGroup 的 dispatchTransformedTouchEvent 方法的返回結果 ;
ViewGroup | dispatchTransformedTouchEvent 方法的返回值含義 :
① 如果返回 true , 證明 子組件 View 已經將事件消費完畢 , 后續不需要再繼續執行遍歷了 ;
② 如果返回 false , 證明 子組件并沒有將事件消費 , 后續還可以進行觸發 ;
public abstract class ViewGroup extends View implements ViewParent, ViewManager {private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,View child, int desiredPointerIdBits) {...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;}...} }
二、記錄被消費的觸摸事件
如果 ViewGroup | dispatchTransformedTouchEvent 方法的返回值為 true , 則會觸發記錄已經被消費的事件的分支 ; 如果返回 false , 則不會觸發 ;
// 正式開始分發觸摸事件 // 處理以下兩種情況 : // ① 情況一 : 子控件觸摸事件返回 true // ② 情況二 : 子控件觸摸事件返回 false if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {記錄時 , 同時會創建 TouchTarget , 并賦值給 ViewGroup | dispatchTouchEvent | newTouchTarget 局部變量 ;
// 記錄消費事件 // 添加觸摸目標 newTouchTarget = addTouchTarget(child, idBitsToAssign);調用 addTouchTarget 方法 , 創建被消費的事件 , 并將創建的值賦值給了 ViewGroup | mFirstTouchTarget 成員 ; 第一次消費事件被賦值 ;
private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);target.next = mFirstTouchTarget;mFirstTouchTarget = target;return target;}完整代碼如下 :
@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {...// 正式開始分發觸摸事件// 處理以下兩種情況 : // ① 情況一 : 子控件觸摸事件返回 true // ② 情況二 : 子控件觸摸事件返回 false if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {// Child wants to receive touch within its bounds.// 如果返回值為 true , 說明該事件已經被消費了 // 此時記錄這個已經被消費的事件 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();...}/*** Adds a touch target for specified child to the beginning of the list.* Assumes the target child is not already present.*/private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);target.next = mFirstTouchTarget;mFirstTouchTarget = target;return target;} }三、觸摸事件沒有被消費的調用鏈分析
如果上述事件分發方法 dispatchTransformedTouchEvent 返回 true , 就會創建 newTouchTarget 值 , 該值不會為空 , 同時 mFirstTouchTarget 不為空 ;
如果上述事件分發方法 dispatchTransformedTouchEvent 返回 false , 此時 newTouchTarget 值 , 就會為空 , 同時 mFirstTouchTarget 為空 ;
如果 ViewGroup | dispatchTransformedTouchEvent 方法的返回值為 false , 則不會記錄事件消費 ; 也就不會調用 ViewGroup | addTouchTarget 方法 , 此時
ViewGroup | dispatchTouchEvent | newTouchTarget 局部變量
ViewGroup | mFirstTouchTarget 成員變量
為空 ;
/*** Adds a touch target for specified child to the beginning of the list.* Assumes the target child is not already present.*/private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);target.next = mFirstTouchTarget;mFirstTouchTarget = target;return target;}當執行到 if (mFirstTouchTarget == null) 分支時 , 會命中該分支 , 進而調用 dispatchTransformedTouchEvent(ev, canceled, null, TouchTarget.ALL_POINTER_IDS) 方法 , 在 dispatchTransformedTouchEvent 方法中 , 如果 child 為空 , 則會調用父類的 super.dispatchTouchEvent 方法 ;
@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {...// 正式開始分發觸摸事件// 處理以下兩種情況 : // ① 情況一 : 子控件觸摸事件返回 true // ② 情況二 : 子控件觸摸事件返回 false if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {// Child wants to receive touch within its bounds.// 如果返回值為 true , 說明該事件已經被消費了 // 此時記錄這個已經被消費的事件 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();// 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);}...}/*** Adds a touch target for specified child to the beginning of the list.* Assumes the target child is not already present.*/private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);target.next = mFirstTouchTarget;mFirstTouchTarget = target;return target;}private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,View child, int desiredPointerIdBits) {final boolean handled;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;}...} }ViewGroup 繼承自 View , 在 ViewGroup 中調用 super.dispatchTouchEvent 方法 , 就是調用 View 的 dispatchTouchEvent 方法 ;
super.dispatchTouchEvent(event) 方法消費的是父容器自己的事件 ;
child.dispatchTouchEvent(event) 方法消費的是子組件的事件 ;
四、觸摸事件被攔截的調用鏈分析
如果上述事件分發方法 dispatchTransformedTouchEvent 返回 true , 就會創建 newTouchTarget 值 , 該值不會為空 , 同時 mFirstTouchTarget 不為空 ;
如果上述事件分發方法 dispatchTransformedTouchEvent 返回 false , 此時 newTouchTarget 值 , 就會為空 , 同時 mFirstTouchTarget 為空 ;
假設事件已經被消費了 , if (newTouchTarget == null && mFirstTouchTarget != null) 分支的代碼不會被命中 , 繼續向下分析 ;
同理如果事件被消費 , 也不會命中 if (mFirstTouchTarget == null) 分支 ;
下面討論另外一種情況 , 就是父容器攔截觸摸事件 ;
調用 ViewGroup | requestDisallowInterceptTouchEvent 可以設置是否攔截觸摸事件 , 如果傳入 true 則攔截觸摸事件 , 如果傳入 false , 則不攔截觸摸事件 ;
在該 ViewGroup | requestDisallowInterceptTouchEvent 方法中 , 會修改 mGroupFlags 值 ;
@Overridepublic void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {// 設置父容器是否要攔截子組件的觸摸事件if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {// We're already in this state, assume our ancestors are tooreturn;}if (disallowIntercept) {mGroupFlags |= FLAG_DISALLOW_INTERCEPT;} else {mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;}// Pass it up to our parentif (mParent != null) {mParent.requestDisallowInterceptTouchEvent(disallowIntercept);}}在 ViewGroup | dispatchTouchEvent 方法中 , 通過 mGroupFlags & FLAG_DISALLOW_INTERCEPT 判定父容器是否需要攔截子組件的觸摸事件 , 該值最終影響 ViewGroup | dispatchTouchEvent | intercepted 局部變量的判斷 ;
// Check for interception.final boolean intercepted;if (actionMasked == MotionEvent.ACTION_DOWN|| mFirstTouchTarget != null) {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 , if (!canceled && !intercepted) { 分支就不會被命中 , 該分支是事件分發的核心邏輯 , 全都被屏蔽了 , 最終 :
ViewGroup | dispatchTouchEvent | newTouchTarget 局部變量
ViewGroup | mFirstTouchTarget 成員變量
都為空 , 執行到 if (mFirstTouchTarget == null) 分支時 , 會命中該分支 , 進而調用 dispatchTransformedTouchEvent(ev, canceled, null, TouchTarget.ALL_POINTER_IDS) 方法 , 在 dispatchTransformedTouchEvent 方法中 , 如果 child 為空 , 則會調用父類的 super.dispatchTouchEvent 方法 ;
ViewGroup 繼承自 View , 在 ViewGroup 中調用 super.dispatchTouchEvent 方法 , 就是調用 View 的 dispatchTouchEvent 方法 ;
super.dispatchTouchEvent(event) 方法消費的是父容器自己的事件 ;
child.dispatchTouchEvent(event) 方法消費的是子組件的事件 ;
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,View child, int desiredPointerIdBits) {final boolean handled;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;}...}ViewGroup | dispatchTouchEvent 相關代碼 :
@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {...// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 true // 就會創建 newTouchTarget 值 , 該值不會為空 , 同時 mFirstTouchTarget 不為空// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 false // 此時 newTouchTarget 值 , 就會為空 , 同時 mFirstTouchTarget 為空 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;}}}// 如果事件被消費 , 事件分發方法 dispatchTransformedTouchEvent 返回 true // 就會創建 newTouchTarget 值 , 該值不會為空 , 同時 mFirstTouchTarget 不為空// 反之// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 false // 此時 newTouchTarget 值 , 就會為空 , 同時 mFirstTouchTarget 為空 // // 還有一個邏輯就是 , 如果該事件被父容器攔截 , mFirstTouchTarget 也是 null 值// 調用 dispatchTransformedTouchEvent , 但是傳入的子組件時 null // 在 dispatchTransformedTouchEvent 方法中觸發調用 if (child == null) 分支的 // handled = super.dispatchTouchEvent(event) 方法 , 調用父類的事件分發方法 // 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);}...}/*** Adds a touch target for specified child to the beginning of the list.* Assumes the target child is not already present.*/private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);target.next = mFirstTouchTarget;mFirstTouchTarget = target;return target;}/*** 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.*/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;}...} }五、ViewGroup 事件分發相關源碼
@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.// 如果返回值為 true , 說明該事件已經被消費了 // 此時記錄這個已經被消費的事件 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();}// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 true // 就會創建 newTouchTarget 值 , 該值不會為空 , 同時 mFirstTouchTarget 不為空// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 false // 此時 newTouchTarget 值 , 就會為空 , 同時 mFirstTouchTarget 為空 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;}}}// 如果事件被消費 , 事件分發方法 dispatchTransformedTouchEvent 返回 true // 就會創建 newTouchTarget 值 , 該值不會為空 , 同時 mFirstTouchTarget 不為空// 反之// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 false // 此時 newTouchTarget 值 , 就會為空 , 同時 mFirstTouchTarget 為空 // // 還有一個邏輯就是 , 如果該事件被父容器攔截 , mFirstTouchTarget 也是 null 值// 調用 dispatchTransformedTouchEvent , 但是傳入的子組件時 null // 在 dispatchTransformedTouchEvent 方法中觸發調用 if (child == null) 分支的 // handled = super.dispatchTouchEvent(event) 方法 , 調用父類的事件分發方法 // 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;}/*** Adds a touch target for specified child to the beginning of the list.* Assumes the target child is not already present.*/private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);target.next = mFirstTouchTarget;mFirstTouchTarget = target;return target;}@Overridepublic void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {// 設置父容器是否要攔截子組件的觸摸事件if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {// We're already in this state, assume our ancestors are tooreturn;}if (disallowIntercept) {mGroupFlags |= FLAG_DISALLOW_INTERCEPT;} else {mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;}// Pass it up to our parentif (mParent != null) {mParent.requestDisallowInterceptTouchEvent(disallowIntercept);}}}
六、View 事件分發相關源碼
public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {/*** Pass the touch screen motion event down to the target view, or this* view if it is the target.** @param event The motion event to be dispatched.* @return True if the event was handled by the view, false otherwise.*/public boolean dispatchTouchEvent(MotionEvent event) {// 無障礙調用 , 輔助功能 // If the event should be handled by accessibility focus first.if (event.isTargetAccessibilityFocus()) {// We don't have focus or no virtual descendant has it, do not handle the event.if (!isAccessibilityFocusedViewOrHost()) {return false;}// We have focus and got the event, then use normal event dispatch.event.setTargetAccessibilityFocus(false);}// 返回結果 boolean result = false;if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onTouchEvent(event, 0);}// 判定手指的動作 final int actionMasked = event.getActionMasked();if (actionMasked == MotionEvent.ACTION_DOWN) {// Defensive cleanup for new gesturestopNestedScroll();}if (onFilterTouchEventForSecurity(event)) {if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {result = true;}//noinspection SimplifiableIfStatement// 設置的 觸摸監聽器 就是封裝在該對象中 ListenerInfo li = mListenerInfo;// 判斷該組件是否被用戶設置了 觸摸監聽器 OnTouchListenerif (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED// 執行被用戶設置的 觸摸監聽器 OnTouchListener&& li.mOnTouchListener.onTouch(this, event)) {// 如果用戶設置的 觸摸監聽器 OnTouchListener 觸摸方法返回 true// 此時該分發方法的返回值就是 true result = true;}// 如果上面為 true ( 觸摸監聽器的觸摸事件處理返回 true ) , 就會阻斷該分支的命中 , 該分支不執行了 // 也就不會調用 View 組件自己的 onTouchEvent 方法 // 因此 , 如果用戶的 觸摸監聽器 OnTouchListener 返回 true // 則 用戶的 點擊監聽器 OnClickListener 會被屏蔽掉 // 如果同時設置了 點擊監聽器 OnClickListener 和 觸摸監聽器 OnTouchListener // 觸摸監聽器 OnTouchListener 返回 false , 點擊監聽器 OnClickListener 才能被調用到 if (!result && onTouchEvent(event)) {result = true;}}if (!result && mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);}// Clean up after nested scrolls if this is the end of a gesture;// also cancel it if we tried an ACTION_DOWN but we didn't want the rest// of the gesture.if (actionMasked == MotionEvent.ACTION_UP ||actionMasked == MotionEvent.ACTION_CANCEL ||(actionMasked == MotionEvent.ACTION_DOWN && !result)) {stopNestedScroll();}return result;}/*** Implement this method to handle touch screen motion events.* <p>* If this method is used to detect click actions, it is recommended that* the actions be performed by implementing and calling* {@link #performClick()}. This will ensure consistent system behavior,* including:* <ul>* <li>obeying click sound preferences* <li>dispatching OnClickListener calls* <li>handling {@link AccessibilityNodeInfo#ACTION_CLICK ACTION_CLICK} when* accessibility features are enabled* </ul>** @param event The motion event.* @return True if the event was handled, false otherwise.*/public boolean onTouchEvent(MotionEvent event) {final float x = event.getX();final float y = event.getY();final int viewFlags = mViewFlags;final int action = event.getAction();final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;if ((viewFlags & ENABLED_MASK) == DISABLED) {if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {setPressed(false);}mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;// A disabled view that is clickable still consumes the touch// events, it just doesn't respond to them.return clickable;}if (mTouchDelegate != null) {if (mTouchDelegate.onTouchEvent(event)) {return true;}}if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {switch (action) {case MotionEvent.ACTION_UP:// 點擊事件 Click 是 按下 + 抬起 事件 // 如果要判定點擊 , 需要同時有 MotionEvent.ACTION_DOWN + MotionEvent.ACTION_UP 事件 mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;if ((viewFlags & TOOLTIP) == TOOLTIP) {handleTooltipUp();}if (!clickable) {removeTapCallback();removeLongPressCallback();mInContextButtonPress = false;mHasPerformedLongPress = false;mIgnoreNextUpEvent = false;break;}boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {// take focus if we don't have it already and we should in// touch mode.boolean focusTaken = false;if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {focusTaken = requestFocus();}if (prepressed) {// The button is being released before we actually// showed it as pressed. Make it show the pressed// state now (before scheduling the click) to ensure// the user sees it.setPressed(true, x, y);}if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {// This is a tap, so remove the longpress checkremoveLongPressCallback();// Only perform take click actions if we were in the pressed stateif (!focusTaken) {// Use a Runnable and post this rather than calling// performClick directly. This lets other visual state// of the view update before click actions start.if (mPerformClick == null) {// 此處創建點擊對象 mPerformClick = new PerformClick();}// 調用點擊事件 if (!post(mPerformClick)) {performClickInternal();}}}if (mUnsetPressedState == null) {mUnsetPressedState = new UnsetPressedState();}if (prepressed) {postDelayed(mUnsetPressedState,ViewConfiguration.getPressedStateDuration());} else if (!post(mUnsetPressedState)) {// If the post failed, unpress right nowmUnsetPressedState.run();}removeTapCallback();}mIgnoreNextUpEvent = false;break;case MotionEvent.ACTION_DOWN:if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {mPrivateFlags3 |= PFLAG3_FINGER_DOWN;}mHasPerformedLongPress = false;if (!clickable) {checkForLongClick(0, x, y);break;}if (performButtonActionOnTouchDown(event)) {break;}// Walk up the hierarchy to determine if we're inside a scrolling container.boolean isInScrollingContainer = isInScrollingContainer();// For views inside a scrolling container, delay the pressed feedback for// a short period in case this is a scroll.if (isInScrollingContainer) {mPrivateFlags |= PFLAG_PREPRESSED;if (mPendingCheckForTap == null) {mPendingCheckForTap = new CheckForTap();}mPendingCheckForTap.x = event.getX();mPendingCheckForTap.y = event.getY();postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());} else {// Not inside a scrolling container, so show the feedback right awaysetPressed(true, x, y);checkForLongClick(0, x, y);}break;case MotionEvent.ACTION_CANCEL:if (clickable) {setPressed(false);}removeTapCallback();removeLongPressCallback();mInContextButtonPress = false;mHasPerformedLongPress = false;mIgnoreNextUpEvent = false;mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;break;case MotionEvent.ACTION_MOVE:if (clickable) {drawableHotspotChanged(x, y);}// Be lenient about moving outside of buttonsif (!pointInView(x, y, mTouchSlop)) {// Outside button// Remove any future long press/tap checksremoveTapCallback();removeLongPressCallback();if ((mPrivateFlags & PFLAG_PRESSED) != 0) {setPressed(false);}mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;}break;}return true;}return false;}/*** Entry point for {@link #performClick()} - other methods on View should call it instead of* {@code performClick()} directly to make sure the autofill manager is notified when* necessary (as subclasses could extend {@code performClick()} without calling the parent's* method).*/private boolean performClickInternal() {// Must notify autofill manager before performing the click actions to avoid scenarios where// the app has a click listener that changes the state of views the autofill service might// be interested on.notifyAutofillManagerOnClick();return performClick();}/*** Call this view's OnClickListener, if it is defined. Performs all normal* actions associated with clicking: reporting accessibility event, playing* a sound, etc.** @return True there was an assigned OnClickListener that was called, false* otherwise is returned.*/// NOTE: other methods on View should not call this method directly, but performClickInternal()// instead, to guarantee that the autofill manager is notified when necessary (as subclasses// could extend this method without calling super.performClick()).public boolean performClick() {// We still need to call this method to handle the cases where performClick() was called// externally, instead of through performClickInternal()notifyAutofillManagerOnClick();final boolean result;final ListenerInfo li = mListenerInfo;if (li != null && li.mOnClickListener != null) {playSoundEffect(SoundEffectConstants.CLICK);// 此處直接執行了 點擊監聽器 的點擊方法 li.mOnClickListener.onClick(this);result = true;} else {result = false;}sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);notifyEnterOrExitForAutoFillIfNeeded(true);return result;}}
源碼路徑 : /frameworks/base/core/java/android/view/View.java
總結
觸摸事件 , 子組件優先進行判定 , 消費觸摸事件 ;
如果子組件將事件消費掉 , 則父容器不再進行消費 , 即父容器的觸摸事件被屏蔽 ; ( 子組件 )
如果子組件沒有講事件消費掉 , 則在子組件調用返回后 , 在執行父容器的觸摸事件 ; ( 先子組件后父容器 )
如果父容器設置了事件攔截 , 此時不會遍歷子組件進行事件分發 , 直接調用父容器的觸摸事件 ; ( 父容器 )
總結
以上是生活随笔為你收集整理的【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 五 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 事件分发】事件分发源码
- 下一篇: 【Android 事件分发】事件分发源码