Android View 事件分发流程(二) — ViewGroup.dispatchTouchEvent
1、ViewGroup.dispatchTouchEvent方法简述
dispatchTouchEvent在ViewGroup中主要做了三件事:获取拦截状态、获取目标子View、处理事件
2、获取拦截状态
获取拦截标志intercepted的状态,true为拦截分发到子View,false为可以分发事件到子View。这模块只有在事件为MotionEvent.ACTION_DOWN时或mFirstTouchTarget != null时(也就是按下状态或者在按下状态分发到子View(mFirstTouchTarget != null)之后的事件)才会执行,disallowIntercept 为禁止拦截标志
// Check for interception.
//拦截标志
final boolean intercepted;
//按下事件或者已经有事件分发到子View后的事件
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;
}
3、获取目标子View
这一节代码,首先是先确认当前事件不为ACTION_CHANCL事件并且不拦截子View事件,然后再进行事件确认,只有按下事件、多指按下事件、鼠标移动事件才会进行下一步处理,也就是普通的ACTION_DOWN、ACTION_UP、ACTION_MOVE事件,只有ACTION_DOWN事件才能进行获取目标子View,在DOWN的事件下获取到目标子View事件会直接调用dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)处理事件,并且获取到子View mFirstTouchTarget = target; 已处理标志置为true alreadyDispatchedToNewTouchTarget = true;
//事件不是ACTION_CANCEL事件和不拦截子View才可以进入事件分发
if (!canceled && !intercepted) {
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 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);
//获取子View数量
final int childrenCount = mChildrenCount;
if (newTouchTarget == null && childrenCount != 0) {
final float x =
isMouseEvent ? ev.getXCursorPosition() : ev.getX(actionIndex);
final float y =
isMouseEvent ? ev.getYCursorPosition() : 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--) {
final int childIndex = getAndVerifyPreorderedIndex(
childrenCount, i, customOrder);
final View child = getAndVerifyPreorderedView(
preorderedList, children, childIndex);
if (!child.canReceivePointerEvents()
|| !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);
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;
}
// 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;
}
}
}
4、处理事件
mFirstTouchTarget就装有目标子View的对象,如果为null,则调用dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS); child为null,处理的结果是super.dispatchTouchEvent(event);如果不为空,先判断是否已处理事件了alreadyDispatchedToNewTouchTarget && target == newTouchTarget (DOWN事件分发的时候已处理了事件),若已处理了事件则handled = true;,没有则调用dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)处理事件。cancelChild是一个比较重要的参数,涉及子View把事件给回到父View
// 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;
}
}
5、disptachTouchEvent 处理DOWN事件
DOWN事件流程是:先获取拦截标志intercepted,再根据intercepted去决定是否需要获取目标子View,获取目标子View时,如果获取到了马上就处理了事件dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)并赋值mFirstTouchTarget,在处理事件代码快就不用处理事件了,直接返回handle = true.如果没有获取到目标子View则调用handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);方法
6、disptachTouchEvent 处理MOVE事件
MOVE事件流程是:先获取拦截标志intercepted,然后再获取目标子View时,判断事件时,MOVE事件通不过if条件,所以就直接到了处理事件的代码了,在DOWN事件时会赋值mFirstTouchTarget,如果mFirstTouchTarget 为 null,则调用handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);方法;不为null时,则调用dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)方法