Event Distribution Process
As you can see from the figure above, the focus of event distribution is dispatchTouchEvent
Analysis of ViewGroup
DispatchTouchEvent analysis for ViewGroup
Step1: Clears the state if it is a new event
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();
}
Copy the code
The Down event is the start of a time series that clears the previous state. Reset TouchTargets clear View held by TouchTarget Step2: Whether to intercept
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 intercepted = true; }Copy the code
If it is a Down event or mFirstTouchTarget is not null, the event is consumed. OnInterceptTouchEvent (EV) returns false by default
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;
}
Copy the code
Step3: execute the process normally
if (! canceled && ! Intercepted) {/ / for children with focus View childWithAccessibilityFocus = ev. IsTargetAccessibilityFocus ()? findChildWithAccessibilityFocus() : null; / / if the down event enter below the 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; / / remove pointer id removePointersFromTouchTargets (idBitsToAssign); final int childrenCount = mChildrenCount; if (newTouchTarget == null && childrenCount ! = 0) { final float x = ev.getX(actionIndex); final float y = ev.getY(actionIndex); / / find children can receive events, from the beginning to the end to find final ArrayList < View > preorderedList = buildTouchDispatchChildList (); final boolean customOrder = preorderedList == null && isChildrenDrawingOrderEnabled(); final View[] children = mChildren; for (int i = childrenCount - 1; i >= 0; i--) { final int childIndex = getAndVerifyPreorderedIndex( childrenCount, i, customOrder); final View child = getAndVerifyPreorderedView( preorderedList, children, childIndex); / / if the child has a focus to traverse the if (childWithAccessibilityFocus! = null) { if (childWithAccessibilityFocus ! = child) { continue; } childWithAccessibilityFocus = null; i = childrenCount - 1; } / / filter cannot receive events and to be outside the scope of finger click on children / / canViewReceivePointerEvent visible or in the execution animation representative to receive events / / isTransformedTouchPointInView x, y point is within the scope of the children if (! canViewReceivePointerEvents(child) || ! isTransformedTouchPointInView(x, y, child, null)) { ev.setTargetAccessibilityFocus(false); continue; } newTouchTarget = getTouchTarget(child); if (newTouchTarget ! = null) {/ / to touch the target set for the new pointer exit loop newTouchTarget. PointerIdBits | = idBitsToAssign; break; } // Reset the child's up flag resetCancelNextUpFlag(child); / / distribution convert touch events if (dispatchTransformedTouchEvent (ev, false, child, idBitsToAssign)) {/ / 1 record down time, MLastTouchDownTime = ev.getdowntime (); 1 if (preorderedList ! = null) {2 // 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(); 3 mLastTouchDownY = ev.getY(); NewTouchTarget = addTouchTarget(Child, idBitsToAssign); // Add the child to the start of the touch target. 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; // Distribute events to touch targets 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; }Copy the code
Distribution process
// ViewGroup
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = false;
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK;
//1.
if (actionMasked == MotionEvent.ACTION_DOWN) {
// 在down事件开始 重置状态 重置记录的触摸目标
cancelAndClearTouchTargets(ev);
resetTouchState();
}
// 2. 检查拦截
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 {
// 没有触摸目标
intercepted = true;
}
// 3.分发事件
final boolean canceled = resetCancelNextUpFlag(this)
|| actionMasked == MotionEvent.ACTION_CANCEL;
// 多点触控
final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
if (!canceled && !intercepted) {// 非取消非拦截
View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
? findChildWithAccessibilityFocus() : null;
// down事件进入
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;
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 = buildTouchDispatchChildList();
final boolean customOrder = preorderedList == null
&& isChildrenDrawingOrderEnabled();
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {
// 查找触控落点的view,把事件分发给他
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
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);
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
ev.setTargetAccessibilityFocus(false);
}
}
// 把找到的触摸目标加到链表
if (newTouchTarget == null && mFirstTouchTarget != null) {
while (newTouchTarget.next != null) {
newTouchTarget = newTouchTarget.next;
}
newTouchTarget.pointerIdBits |= idBitsToAssign;
}
}
}
// 4. 把事件分发给触摸目标 Dispatch to touch targets.
if (mFirstTouchTarget == null) {
// 这一步会让ViewGroup退化成View执行dispatchTouchEvent
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {
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;
}
}
}
return handled;
}
// 5. 分发给子View
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
final boolean handled;
final int oldAction = event.getAction();
// a. 处理cancel事件
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;
}
if (child == null) {
//b. 执行View的dispatchTouchEvent
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());
}
// c. 分发给子View
handled = child.dispatchTouchEvent(transformedEvent);
}
// Done.
transformedEvent.recycle();// 回收池回收MotionEvent对象
return handled;
}
// View
public boolean dispatchTouchEvent(MotionEvent event) {
if (onFilterTouchEventForSecurity(event)) {
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
// OnTouchListener.onTouch
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
// onTouchEvent
if (!result && onTouchEvent(event)) {
result = true;
}
}
return result;
}
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;
// a.如果enable为false 直接返回
if ((viewFlags & ENABLED_MASK) == DISABLED) {
if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
return clickable;
}
if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
switch (action) {
case MotionEvent.ACTION_UP: // b. up事件执行performClick 内部会回调mOnClickListener.onClick
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClickInternal();
}
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
return false;
}
// 场景1:没有子View消费任何事件 ViewGroup退化成View执行dispatchTouchEvent最终执行onTouchEvent 所以就出现 子View不消费 一级一级抛给上层消费
2021-09-07 10:17:01.889 18900-18900/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Down
2021-09-07 10:17:01.892 18900-18900/com.example.testandroid I/Liuzj: MyLinearLayout----dispatchTouchEvent: Down
2021-09-07 10:17:01.892 18900-18900/com.example.testandroid I/Liuzj: MyLinearLayout----onInterceptTouchEvent: Down
2021-09-07 10:17:01.892 18900-18900/com.example.testandroid I/Liuzj: MyFrameLayout----dispatchTouchEvent: Down
2021-09-07 10:17:01.892 18900-18900/com.example.testandroid I/Liuzj: MyFrameLayout----onInterceptTouchEvent: Down
2021-09-07 10:17:01.893 18900-18900/com.example.testandroid I/Liuzj: MyTextView----dispatchTouchEvent: Down
2021-09-07 10:17:01.893 18900-18900/com.example.testandroid I/Liuzj: MyTextView----onTouchEvent: Down
2021-09-07 10:17:01.893 18900-18900/com.example.testandroid I/Liuzj: MyFrameLayout----onTouchEvent: Down 退化为View
2021-09-07 10:17:01.893 18900-18900/com.example.testandroid I/Liuzj: MyLinearLayout----onTouchEvent: Down 退化为View
2021-09-07 10:17:01.894 18900-18900/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Down 退化为View
2021-09-07 10:17:01.978 18900-18900/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Up 没有子View消费所以DecorView变为拦截后续事件所以只有Activity处理后续事件
2021-09-07 10:17:01.979 18900-18900/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Up
2021-09-07 10:45:45.140 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:45:45.141 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:45:45.360 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:45:45.361 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:45:45.377 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:45:45.377 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:45:45.393 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:45:45.393 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:45:45.410 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:45:45.410 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:45:45.427 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:45:45.427 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:45:45.525 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:45:45.525 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:45:45.542 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:45:45.542 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:45:45.664 21064-21064/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Up
2021-09-07 10:45:45.664 21064-21064/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Up
// 场景2 子View消费Down事件 由于有子View消费事件所以TouchTarget不会空 所以后续事件都可以分发
2021-09-07 10:47:55.919 21271-21271/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Down
2021-09-07 10:47:55.922 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----dispatchTouchEvent: Down
2021-09-07 10:47:55.923 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----onInterceptTouchEvent: Down
2021-09-07 10:47:55.923 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----dispatchTouchEvent: Down
2021-09-07 10:47:55.923 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----onInterceptTouchEvent: Down
2021-09-07 10:47:55.924 21271-21271/com.example.testandroid I/Liuzj: MyTextView----dispatchTouchEvent: Down
2021-09-07 10:47:55.924 21271-21271/com.example.testandroid I/Liuzj: MyTextView----onTouchEvent: Down
2021-09-07 10:47:56.269 21271-21271/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:47:56.271 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----dispatchTouchEvent: Move
2021-09-07 10:47:56.271 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----onInterceptTouchEvent: Move
2021-09-07 10:47:56.271 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----dispatchTouchEvent: Move
2021-09-07 10:47:56.272 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----onInterceptTouchEvent: Move
2021-09-07 10:47:56.272 21271-21271/com.example.testandroid I/Liuzj: MyTextView----dispatchTouchEvent: Move
2021-09-07 10:47:56.272 21271-21271/com.example.testandroid I/Liuzj: MyTextView----onTouchEvent: Move
2021-09-07 10:47:56.273 21271-21271/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:47:56.890 21271-21271/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:47:56.890 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----dispatchTouchEvent: Move
2021-09-07 10:47:56.891 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----onInterceptTouchEvent: Move
2021-09-07 10:47:56.891 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----dispatchTouchEvent: Move
2021-09-07 10:47:56.891 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----onInterceptTouchEvent: Move
2021-09-07 10:47:56.891 21271-21271/com.example.testandroid I/Liuzj: MyTextView----dispatchTouchEvent: Move
2021-09-07 10:47:56.891 21271-21271/com.example.testandroid I/Liuzj: MyTextView----onTouchEvent: Move
2021-09-07 10:47:56.891 21271-21271/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:47:56.911 21271-21271/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Move
2021-09-07 10:47:56.911 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----dispatchTouchEvent: Move
2021-09-07 10:47:56.911 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----onInterceptTouchEvent: Move
2021-09-07 10:47:56.911 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----dispatchTouchEvent: Move
2021-09-07 10:47:56.912 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----onInterceptTouchEvent: Move
2021-09-07 10:47:56.912 21271-21271/com.example.testandroid I/Liuzj: MyTextView----dispatchTouchEvent: Move
2021-09-07 10:47:56.912 21271-21271/com.example.testandroid I/Liuzj: MyTextView----onTouchEvent: Move
2021-09-07 10:47:56.912 21271-21271/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Move
2021-09-07 10:47:56.928 21271-21271/com.example.testandroid I/Liuzj: MainActivity----dispatchTouchEvent: Up
2021-09-07 10:47:56.929 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----dispatchTouchEvent: Up
2021-09-07 10:47:56.929 21271-21271/com.example.testandroid I/Liuzj: MyLinearLayout----onInterceptTouchEvent: Up
2021-09-07 10:47:56.929 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----dispatchTouchEvent: Up
2021-09-07 10:47:56.929 21271-21271/com.example.testandroid I/Liuzj: MyFrameLayout----onInterceptTouchEvent: Up
2021-09-07 10:47:56.929 21271-21271/com.example.testandroid I/Liuzj: MyTextView----dispatchTouchEvent: Up
2021-09-07 10:47:56.929 21271-21271/com.example.testandroid I/Liuzj: MyTextView----onTouchEvent: Up
2021-09-07 10:47:56.930 21271-21271/com.example.testandroid I/Liuzj: MainActivity----onTouchEvent: Up
Copy the code
About TouchTarget
www.jianshu.com/p/5951ebdd2…