Android事件分发源码分析
文章目录
- Android事件分发源码分析
- 1.1 Activity->dispatchTouchEvent()
- 1.2 Window->superDispatchKeyEvent()
- 1.3 View对点击事件的分发过程
- 1.3.1 点击事件的分发过程概述
- 1.3.2 ViewGroup点击事件分发处理过程分析
- 1.3.3 View点击事件分发处理过程分析
- 1.4 参考资料
1.1 Activity->dispatchTouchEvent()
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
- 事件产生后交给Activity所附属的Window进行分发。
- 如果事件被消耗(返回true),则结束;如果事件未被消耗,Activity的onTouchEvent方法被调用。
1.2 Window->superDispatchKeyEvent()
public abstract boolean superDispatchKeyEvent(KeyEvent event);
- Window是抽象类,superDispatchKeyEvent方法也是抽象方法。
- Window的实现是PhoneWindow。
public boolean superDispatchKeyEvent(KeyEvent event){
return mDecor.superDispatchKeyEvent(event);
}
- PhoneWindow将事件传递给了DecorView。
- 可以通过以下方法获取Activity设置的View:
((ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0)
可以看出,mDecor就是getWindow().getDecorView()返回的View,通过setContentView设置的View是它的一个子View。DecorView继承自FrameLayout且是父View,即事件最终会传递给View。
- 通过setContentView设置的View是顶级View,也叫根View,一般是ViewGroup。
1.3 View对点击事件的分发过程
1.3.1 点击事件的分发过程概述
- 点击事件来到顶级View(一般是ViewGroup)后,会调用ViewGroup的dispatchTouchEvent方法。
- 如果ViewGroup拦截事件(onInterceptTouchEvent返回true),则事件由ViewGroup处理,这时如果ViewGroup设置了mOnTouchListener,则onTouch会被调用,否则onTouchEvent会被调用。注意:onTouch会屏蔽掉onTouchEvent。如果onTouchEvent中设置了mOnClickListener,则onClick会被调用。
- 如果ViewGroup不拦截事件(onInterceptTouchEvent返回false),事件会传递给它所在的点击事件链上的子View,这时子View的dispatchTouchEvent方法被调用。这时,事件已经从顶级View传递给了下一层View,接下来的过程与该过程相似,循环直到完成整个事件的分发。
1.3.2 ViewGroup点击事件分发处理过程分析
// 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;
}
- ViewGroup会在两种情况下判断是否要拦截当前事件:事件类型为ACTION_DOWN或mFirstTouchTarget 不为空。
- 当事件由ViewGroup的子元素处理成功时,mFirstTouchTarget会被赋值并指向子元素。即mFirstTouchTarget 不为空时:ViewGroup不拦截事件并将事件交由子元素处理。
- 一旦事件由ViewGroup拦截,mFirstTouchTarget为空,事件ACTION_MOVE和ACTION_UP事件到达时,ViewGroup的onInterceptTouchEvent不会被调用。即同一事件序列中的其它事件默认交由该ViewGroup处理。即当ViewGroup决定拦截事件后,那么后续的点击事件将会默认交给它处理,不再调用它的onInterceptTouchEvent方法。
- 这里有一种特殊情况:FLAG_DISALLOW_INTERCEPT标记位。该标记位一般用于子View中,通过requestDisallowInterceptTouchEvent方法设置。该标记位一旦设置,ViewGroup将无法拦截除ACTION_DOWN以外的点击事件。因为ViewGroup在分发事件时,如果是ACTION_DOWN就会重置FLAG_DISALLOW_INTERCEPT标记位(如下述源码片段所示),导致子View中设置的标记位无效。因此,面对ACTION_DOWN事件,ViewGroup总会调用自己的onInterceptTouchEvent方法来询问时候要拦截事件。
// 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();
}
- 小结:
onInterceptTouchEvent不是每次事件都会被调用,如果想提前处理所有的点击事件,应该选择despatchTouchEvent方法(在事件可以传递到当前ViewGroup的前提下)。- 当ViewGroup不拦截事件时,事件会下发给它的子View进行处理。
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 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;
}
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)) {
// 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;
}
- 首先遍历ViewGroup的所有子元素,然后判断子元素是否能够接收到点击事件。子元素是否能够接收到点击事件由两点衡量:1.子元素是否在播放动画;2.点击事件的坐标是否落在子元素的区域内。如果某个子元素满足这两个条件,事件就会交给它处理,dispatchTransformedTouchEvent实际上调用的就是子元素的dispatchTouchEvent方法。
- 在dispatchTransformedTouchEvent方法存在以下内容:
if (child == null) { handled = super.dispatchTouchEvent(event); } else { handled = child.dispatchTouchEvent(event); }
如果传给dispatchTransformedTouchEvent的child不为bull,会直接调用子元素的dispatchTouchEvent方法,完成一轮事件分发。
- 如果子元素的dispatchTouchEvent返回true,那么mFirstTouchTarget会被赋值(在addTouchTarget方法里对mFirstTouchTarget赋值),同时跳出for循环。
newTouchTarget = addTouchTarget(child, idBitsToAssign); alreadyDispatchedToNewTouchTarget = true; break;
如果子元素dispatchTouchEvent返回false,dispatchTouchEvent会继续分发事件给下一个存在的子元素。
private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) { final TouchTarget target = TouchTarget.obtain(child, pointerIdBits); target.next = mFirstTouchTarget; mFirstTouchTarget = target; return target; }
addTouchTarget内部是一种单链表策略结构。mFirstTouchTarget的赋值与否,直接关系到ViewGroup的拦截策略(上面提及过这点)。
- 如果遍历所有子元素后,事件仍然未被消耗处理,包含两种情况:1.ViewGroup没有子元素;2.子元素处理了点击事件,但是在dispatchTouchEvent中返回了false,一般是因为子元素在onTouchEvent中返回了false。在这两种情况下,ViewGroup会自己处理点击事件。(如下述源码片段所示)
// 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);
第三个参数为null,则会调用super.dispatchTouchEvent(event)。
1.3.3 View点击事件分发处理过程分析
- 注意:这里的View不包括ViewGroup。
public boolean dispatchTouchEvent(MotionEvent event) {
...
boolean result = false;
...
if (onFilterTouchEventForSecurity(event)) {
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
if (!result && onTouchEvent(event)) {
result = true;
}
}
...
return result;
}
- View是一个单独的元素,没有子元素就无需向下传递事件了,只能自己处理事件。
- 首先会判断有没有设置mOnTouchListener,如果mOnTouchListener中的onTouch方法返回true,那么onTouchEvent就不会被调用。好处是:方便在外界处理点击事件。
//onTouchEvent实现的片段:
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;
}
- 从上述onTouchEvent实现代码片段中可见,当View处于不可用状态下点击事件仍然会消耗。
- 如果View设置了代理,那么还会执行TouchDelegate和onTouchEvent方法,这个onTouchEvent工作机制和OnTouchListener类似。(如下述源码片段所示)
if (mTouchDelegate != null) { if (mTouchDelegate.onTouchEvent(event)) { return true; } }
//onTouchEvent中对点击事件的具体处理的片段代码
final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;
if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
switch (action) {
case MotionEvent.ACTION_UP:
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
if ((viewFlags & TOOLTIP) == TOOLTIP) {
handleTooltipUp();
}
if (!clickable) {
removeTapCallback();
removeLongPressCallback();
mInContextButtonPress = false;
mHasPerformedLongPress = false;
mIgnoreNextUpEvent = false;
break;
}
...
}
return true;
}
- 上述代码为onTouchEvent中对点击事件的具体处理的片段代码。可见只要View的CLICKABLE和LONG_CLICKABLE有一个为true,那么clickable就为true,那么onTouchEvent就会返回true。
- 在ACTION_UP事件发生时,会触发PerformClick方法:
if (mPerformClick == null) { mPerformClick = new PerformClick(); }
如果View设置了OnClickListener,那么PerformClick方法会调用它的onClick方法。(如下所示)
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;
}
- View的LONG_CLICKABLE属性默认为false,而CLICKABLE属性是否为false和具体的View有关。通过setClickable和setLongClickable可以改变View的CLICKABLE和LONG_CLICKABLE属性。并且,setOnClickListener会自动将View的CLICKABLE属性设为true,setOnLongClickListener会自动将View的LONG_CLICKABLE属性设为true。(如下所示)
public void setOnClickListener(@Nullable OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
getListenerInfo().mOnClickListener = l;
}
public void setOnLongClickListener(@Nullable OnLongClickListener l) {
if (!isLongClickable()) {
setLongClickable(true);
}
getListenerInfo().mOnLongClickListener = l;
}
1.4 参考资料
- Android开发艺术探索