Android事件分发机制
研究了几天的事件分发,该是将它写下来的时候了,作为一次学习经历的总结,也为了避免后面再次使用时进坑。说起事件,估计大家最熟悉的莫过于按钮的点击事件,然后可能就是View的触摸事件,但是它们的实现过程是怎样,不知道又有多少人能说的清楚。事件分发的过程,实际上就是当一个MotionEvent(触摸事件)产生后,系统将这个事件分发到具体的View的过程。
事件分发中,最主要的有三个方法,它们分别是:
public boolean dispatchTouchEvent(MotionEvent ev);//用于事件分发
public boolean onInterceptTouchEvent(MotionEvent ev);//用于事件拦截,常用于带有子视图的ViewGroup中
public boolean onTouchEvent(MotionEvent event);//用于消费事件
事实上,Android应用层中的事件源于Activity,任何一次触摸屏幕的操作,都会产生一系列的事件,Activity中有这样一个方法
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
每产生一个事件,最先通过Activity的该方法,其中onUserInteraction()是一个空方法,可以通过实现该方法来了解事件分发给Activity之前用户与设备是如何交互的;再看代码,Android中getWindow()方法返回的是一个PhoneWindow对象,这段代码的意思就是如果PhoneWindow消费了事件,那么该方法执行完毕,否则由onTouchEvent(ev)来消费事件。下面来看PhoneWindow中的superDispatchTouchEvent()方法:
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
return mDecor.superDispatchTouchEvent(event);
}
显然,PhoneWindow把事件传递给了与之关联的mDecor对象,熟悉源码的应该都知道mDecor是Activity的根布局DecorView,也就是我们在Activity中通过setContentView()方法设置的布局,下面就是DecorView的superDispatchTouchEvent()方法:
public boolean superDispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
DecorView把事件传递给了父类,通过跟踪代码发现,传递给了ViewGroup类,那么来看看ViewGroup是如何实现dispatchTouchEvent()方法的:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
....
boolean handled = false;
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK;
// 为一系列触摸操作做初始化
if (actionMasked == MotionEvent.ACTION_DOWN) {
cancelAndClearTouchTargets(ev);
resetTouchState();
}
// 此处两个条件来判断是否要进行拦截操作
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;
}
...
// Check for cancelation.
final boolean canceled = resetCancelNextUpFlag(this)
|| actionMasked == MotionEvent.ACTION_CANCEL;
...
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 childrenCount = mChildrenCount;
if (newTouchTarget == null && childrenCount != 0) {
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 (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
...
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
}
}
}
}
// 如果mFirstTouchTarget == null,说明事件没有被子视图处理
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {
...
if (dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)) {
handled = true;
}
}
}
}
return handled;
}
由于代码比较长,此处只贴出一些关键代码,大致逻辑是这样:如果顶级ViewGroup拦截事件,onInterceptTouchEvent()方法返回true,则事件由该ViewGroup处理:如果ViewGroup设置了mOnTouchListener,那么onTouch()会被调用,否则onTouchEvent()会被调用,就是说onTouch()可以屏蔽掉onTouchEvent()方法;在onTouchEvent()方法中,如果设置了mOnClickListener()方法,那么会调用onClick()方法。如果顶级ViewGroup没有拦截事件,则交由子View的dispatchTouchEvent()方法去处理,此时事件已经从顶级View传递到了下一级View,接下来的传递过程和以上相同,直到完成事件的分发。
下面来分析一下上面的代码:当触摸发生时,即按下的那一瞬间,做了初始化的操作,然后两个条件来判断是否拦截,条件1:actionMasked == MotionEvent.ACTION_DOWN,条件2: mFirstTouchTarget != null,条件1很好理解,那么条件2是怎么回事呢?通过分析后面的代码可知,如果是子View处理了事件,那mFirstTouchTarget != null,如果当前View处理了事件,那mFirstTouchTarget == null,而且ACTION_DOWN后面的事件也不再进行拦截判断;根据代码可知,无论拦截与否,都会进入到dispatchTransformedTouchEvent()方法中,并且根据该方法的返回值判断该View的dispatchTouchEvent()的返回值;那么下面来看看dispatchTransformedTouchEvent()的实现:
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;
}
...
if (newPointerIdBits == oldPointerIdBits) {
if (child == null || child.hasIdentityMatrix()) {
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
final float offsetX = mScrollX - child.mLeft;
final float offsetY = mScrollY - child.mTop;
event.offsetLocation(offsetX, offsetY);
handled = child.dispatchTouchEvent(event);
event.offsetLocation(-offsetX, -offsetY);
}
return handled;
}
transformedEvent = MotionEvent.obtain(event);
} else {
transformedEvent = event.split(newPointerIdBits);
}
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);
}
transformedEvent.recycle();
return handled;
}
可以看出,如果child==null,那么会调用父类的dispatchTouchEvent()方法,即事件交给View处理,否则会调用child.dispatchTouchEvent()方法,现在思路基本上已经明了了,无论是ViewGroup还是子View,最终的事件处理都是要交给View,但是View所处的层级不同,现在是时候来看看View的实现过程了:
public boolean dispatchTouchEvent(MotionEvent event) {
boolean result = false;
...
if (onFilterTouchEventForSecurity(event)) {
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
//关键点
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;
}
}
if (!result && mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
}
...
return result;
}
很明显,就像前面所说的,如果设置了mOnTouchListener,那么onTouch()方法被调用,否则onTouchEvent()方法被调用,最后,我们来看看onTouchEvent()的真容:
public boolean onTouchEvent(MotionEvent event) {
if ((viewFlags & ENABLED_MASK) == DISABLED) {
if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
return (((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE);
}
if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
}
if (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
(viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {
switch (action) {
case MotionEvent.ACTION_UP:
boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (prepressed) {
setPressed(true, x, y);
}
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
removeLongPressCallback();
if (!focusTaken) {
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
}
}
if (mUnsetPressedState == null) {
mUnsetPressedState = new UnsetPressedState();
}
if (prepressed) {
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());
} else if (!post(mUnsetPressedState)) {
// If the post failed, unpress right now
mUnsetPressedState.run();
}
removeTapCallback();
}
mIgnoreNextUpEvent = false;
break;
case MotionEvent.ACTION_DOWN:
mHasPerformedLongPress = false;
if (performButtonActionOnTouchDown(event)) {
break;
}
boolean isInScrollingContainer = isInScrollingContainer();
if (isInScrollingContainer) {
mPrivateFlags |= PFLAG_PREPRESSED;
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
mPendingCheckForTap.x = event.getX();
mPendingCheckForTap.y = event.getY();
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
} else {
setPressed(true, x, y);
checkForLongClick(0, x, y);
}
break;
case MotionEvent.ACTION_CANCEL:
setPressed(false);
removeTapCallback();
removeLongPressCallback();
mInContextButtonPress = false;
mHasPerformedLongPress = false;
mIgnoreNextUpEvent = false;
break;
case MotionEvent.ACTION_MOVE:
drawableHotspotChanged(x, y);
if (!pointInView(x, y, mTouchSlop)) {
removeTapCallback();
if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
// Remove any future long press/tap checks
removeLongPressCallback();
setPressed(false);
}
}
break;
}
return true;
}
return false;
}
主要有三步,1、View不可用的状况下依旧可以消耗事件,2、View设置了代理时,会调用代理的onTouchEvent()方法,3、可用的情况下,消费事件,返回true。
到此,事件分发机制分析完成了,总结一下,事件分发分为ViewGroup和View两类;
ViewGroup关联方法:onInterceptTouchEvent()、dispatchTouchEvent()、onTouchEvent()
View关联方法:dispatchTouchEvent()、onTouchEvent()