Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()
1.概述
Android中触摸事件传递过程中最重要的是dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()方法;有很多工作几年的或者初学者总是感到困惑的问题之一,困惑的问题主要就是事件的传递机制和响应机制;今天我们就整理一下dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()的处理过程,方便理解;
- dispatchTouchEvent()是处理触摸事件分发,事件(多数情况)是从Activity的dispatchTouchEvent()开发的;执行getWindow().superDispatchTouchEvent(ev)事件向下分发;
Activity
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
//调用Window下的superDispatchTouchEvent()方法
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
PhoneWindow
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
//调用Window窗口下的DecorView的superDispatchTouchEvent()方法
return mDecor.superDispatchTouchEvent(event);
}
DecorView
public boolean superDispatchTouchEvent(MotionEvent event) {
//调用DecorView父视图的super.dispatchTouchEvent()方法
return super.dispatchTouchEvent(event);
}
ViewGroup
public boolean dispatchTouchEvent(MotionEvent event) {
...
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
...
return handled;
}
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
final boolean handled;
...
//事件向子视图传递
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
...
return handled;
}
- onInterceptTouchEvent()是ViewGroup提供的方法,默认返回false,返回true表示拦截;
- onTouchEvent()是View中提供的方法,ViewGroup也有这个方法,view中不提供onInterceptTouchEvent();view中默认返回true,表示消费了这个事件;
2.事件传递的回调方法
View里有两个事件回调函数
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
ViewGroup里,有三个回调函数
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onInterceptTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
Activity里有两个回调函数
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
3.事件传递流程分析
Android中默认情况下事件传递是由最终的View的接收到,传递过程是从父视图到子视图,也就是从Activity到ViewGroup到View的过程,默认情况下,ViewGroup起到透传的作用;Android中事件传递过程(按箭头方向)处理,如下图

触摸事件是一连串ACTION_DOWN,ACTION_MOVE...MOVE,最后ACTION_UP,触摸事件还有ACTION_CANCEL事件,事件都是从ACTION_DOWN开始的,Activity的dispatchTouchEvent()首先接收到ACTION_DOWN,执行super.dispatchTouchEvent()或者child.dispatchTouchEvent(event),事件向下分发;
dispatchTouchEvent()返回true,后续事件(ACTION_MOVE,ACTION_UP)会在传递,如果返回false,dispatchTouchEvent()就接收不到ACTION_UP,ACTION_MOVE事件;
事件传递顺序如下:
图1.ACTION_DOWN都没被消费

图2-1.ACTION_DOWN被View消费了

图2-2.后续ACTION_MOVE和UP在不被拦截的情况下都会去找VIEW

图3.后续的被拦截了

图4.ACTION_DOWN一开始就被拦截

Android中的Touch事件都是从ACTION_DOWN开始的:
单手指操作:ACTION_DOWN---ACTION_MOVE----ACTION_UP
多手指操作:ACTION_DOWN---ACTION_POINTER_DOWN---ACTION_MOVE--ACTION_POINTER_UP---ACTION_UP
4.源码分析
4.1ViewGroup下dispatchTouchEvent()源码分析
ViewGroup.java
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
...
//1.这里是在对Touch事件初始化,虽然在dispatchTouchEvent中会在最后进行初始化,但是因为在一些异常情况下(app切换,anr等等)并没有进行到初始化的代码,所以在每次touch事件流程开始的时候就再进行一次初始化
if (actionMasked == MotionEvent.ACTION_DOWN) {
cancelAndClearTouchTargets(ev);
resetTouchState();
}
// 2.拦截判断
//用来判定ViewGroup是否拦截事件,从代码我们知道在满足当前事件为Down 或者 mFirstTouchTarget != null (mFirstTouchTarget即为事件处理View)的情况下才会调用onInterceptTouchEvent(),所以当事件如果为Move并且mFirstTouchTarget == null 的情况下(ViewGroup没有处理当前事件的子View)就直接intercepted = true,表明自己拦截
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
//disallowIntercept禁用拦截父视图传递给子视图事件
//用来标志当前ViewGroup能否拦截事件,可以通过requestDisallowInterceptTouchEvent()来改变 mGroupFlags 的值控制
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.从if中可以看出在事件没有cancel并且没有被上一步的intercepted所拦截的情况下就会进行mFirstTouchTarget 的相关初始化工作,即遍历能处理这个事件的子View,为什么是能够处理而不是全部呢因为其中有一个判断
if (!canceled && !intercepted) {
//寻找可以接收事件的子视图,从前到后的顺序
final ArrayList<View> preorderedList = buildTouchDispatchChildList();
//3.1通过手指的x,y来判断子View是否包含,如果不包含继续遍历,如果包含则加入到mFirstTouchTarget
if (!child.canReceivePointerEvents()
|| !isTransformedTouchPointInView(x, y, child, null)) {
continue;
}
}
//4.如果不存在mFirstTouchTarget则调用dispatchTransformedTouchEvent,我们看看它的关键代码
// 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.事件派发
//5.1.mFirstTouchTarget== null 的情况下,即child == null,就会调用super.dispatchTouchEvent( ),我们知道ViewGroup的父类为View,所以就到了View中了,具体View的dispatchTouchEvent( )下面会介绍
//5.2.mFirstTouchTarget!= null 的情况下,即child != null,就调用子类本生的dispatchTouchEvent( )
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;
}
}
View.java
//6.所以我们看到了onTouchEvent()就是在此刻调用的,能够成功调用的前提是mOnTouchListener 在不为空的情况下并且onTouch必须返回false,否则不能到onTouchEvent()中
public void dispatchTouchEvent(MotionEvent ev){
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;
}
}
}
参考: