一张图看懂Touch事件的传递

1、初识Touch事件 

完整的点击事件是由一个ACTION_DOWN,若干个ACTION_MOVE(可能是0个)和一个ACTION_UP组成的。每一个完整的点击事件都是有ACTION_DOWN开始的。


2、Touch事件在View中的传递

Touch事件的传递涉及到的两个主体——ViewGroup和View,三个方法——dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()。

我们将通过下面这张图分析Touch事件的传递(ViewGroup1只是为了辅助分析,以下的事件分析都是从ViewGroup2开始的)。



由于点击事件是由ACTION_DOWN、ACTION_MOVE和ACTION_UP共同组成的,所以在分析Touch事件传递的时候,为了能够分析透彻,我们结合上图分别分析以上三种点击事件。

首先看ACTION_DOWN,(由于ViewGroup1与ViewGroup2的处理逻辑一样,所以我们从ViewGroup2开始分析)当ACTION_DOWN传递到ViewGroup2时,首先调用ViewGroup2的dispatchTouchEvent()方法,dispatchTouchEvent()方法调用onInterceptTouchEvent()判断ViewGroup2是否拦截,如果ViewGroup2拦截ACTION_DOWN,将会调用ViewGroup2的onTouchEvent()处理ACTION_DOWN事件;如果ViewGroup2不拦截ACTION_DOWN,ACTION_DOWN会传到View,调用View的dispatchTouchEvent(),dispatchTouchEvent()方法调用onTouchEvent()方法处理ACTION_DOWN事件,如果onTouchEvent()方法返回true,ViewGroup2中就会添加Target;如果View的onTouchEvent()方法返回false,就会调用ViewGroup2的onTouchEvent()方法。同理,如果ViewGroup2的onTouchEvent()方法返回true,ViewGroup1中就会添加Target。

然后我们分析ACTION_MOVE和ACTION_UP(由于这两种Touch事件的处理逻辑相同,我们只分析其中一种,以ACTION_MOVE为例,这里我们也从ViewGroup2来分析),当ACTION_MOVE传到ViewGroup2之后,就会调用ViewGroup2的dispatchTouchEvent()方法,如果ViewGroup2的Target为null,ViewGroup2默认拦截ACTION_MOVE事件,之后调用ViewGroup2的onTouchEvent()方法处理ACTION_MOVE事件;如果ViewGroup2的Target不为空,调用ViewGroup2的onInterceptTouchEvent()方法判断是否拦截,如果ViewGroup2不拦截,ACTION_MOVE事件就会传递到View中,调用View的dispatchTouchEvent()方法,View的dispatchTouchEvent()方法调用onTouchEvent()方法处理ACTION_MOVE事件;如果ViewGroup2拦截ACTION_MOVE,就会给View传一个ACTION_CANCEL事件。

扫描二维码关注公众号,回复: 2513148 查看本文章


以下是ViewGroup和View类的关键代码

ViewGroup的关键代码:

/**
 * {
@inheritDoc}
 */
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    /**其他代码,暂不关心**/

    ……
    boolean handled = false;
    if (onFilterTouchEventForSecurity(ev)) {
        ……

        // Check for interception.
       
final boolean intercepted;

  /**

  * @ 1

  *

  * 注意接收到MotionEvent.ACTION_DOWN事件要判断是否需要拦截,如果 

  * mFirstTouchTarget不为空也要判断事件是否需要拦截,

  * 如果mFirstTouchTarget为空,则其他事件一律拦截

  * /
        if(actionMasked == MotionEvent.ACTION_DOWN||mFirstTouchTarget!=null) {
            intercepted = onInterceptTouchEvent(ev);
        } else {
           
intercepted = true;
        }
        ……

    if (!canceled && !intercepted) {

……

            final View[] children = mChildren;
            for (int i = childrenCount - 1; i >= 0; i--) { 

……

//将Touch事件传到child

if(dispatchTransformedTouchEvent(ev,false,child,idBitsToAssign)){

……

/*

* @ 2

*

* 如果子控件接收了Touch事件,

* 设置mFirsttouchTarget,mFirsttouchTarget是一个单链表

*/

newTouchTarget = addTouchTarget(child, idBitsToAssign);

alreadyDispatchedToNewTouchTarget = true;

                  break;

}

}
        }

  //如果mFirstTouchTarget,说明子控件没有处理ActionDown,就判断当前View是否处理
       
if (mFirstTouchTarget == null) {
           
//判断本View是否处理Touch事件

            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 {

/**

* 注意下面的代码,如果当前Touch被拦截了,而且target存在,

* 就给target传一个MotionEvent.ACTION_CANCEL,

* 同时将target清空,这样当下一个Touch事件传到当前View的时候

* 就会交由当前View处理

*/

final boolean cancelChild=resetCancelNextUpFlag(target.child)||intercepted;
            if (dispatchTransformedTouchEvent(ev, cancelChild,
                    target.child, target.pointerIdBits)) {
                handled = true;
            }
            ……

      }
    }
    return handled;
}


/**ViewGroup的拦截方法 **/

public boolean onInterceptTouchEvent(MotionEvent ev) {
    return false;//没开玩笑,ViewGroup默认是不拦截Touch事件的

}

/**传递Touch事件的方法**/

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;
    }
……


    final MotionEvent transformedEvent;
    if (newPointerIdBits == oldPointerIdBits) {
        if (child == null || child.hasIdentityMatrix()) {
            if (child == null) {
                handled = super.dispatchTouchEvent(event);
            } else {
                ……
                handled = child.dispatchTouchEvent(event);
           }
            return handled;
        }
    } else {
        ……

    }

    if (child == null) {
        handled = super.dispatchTouchEvent(transformedEvent);
    } else {
        ……

        handled = child.dispatchTouchEvent(transformedEvent);
    }

    // Done.
   
transformedEvent.recycle();
    return handled;
}


View的关键代码

/**View 分配点击事件 **/

public boolean dispatchTouchEvent(MotionEvent event) {
    ……


    boolean result = false;
    ……


    if (onFilterTouchEventForSecurity(event)) {

  /**如果设置了点击事件监听器,而且当前View是ENABLE的,

  * 而且监听器响应Touch事件,返回true 

  **/
        ListenerInfo li = mListenerInfo;
        if (li != null && li.mOnTouchListener != null
               
&& (mViewFlags & ENABLED_MASK) == ENABLED
               
&& li.mOnTouchListener.onTouch(this, event)) {
            result = true;
        }
  /**如果onTouchEvent返回true,就说明分配了Touch事件**/
        if (!result && onTouchEvent(event)) {
            result = true;
        }
    }
    ……


    return result;
}


public boolean onTouchEvent(MotionEvent event) {
    ……

    if ((viewFlags & ENABLED_MASK) == DISABLED) {
        ……

    /**只要当前View设置了CLICKABLE或LONG_CLICKABLE或CONTEXT_CLICKABLE,就返回true*/

        return (((viewFlags & CLICKABLE) == CLICKABLE
               
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
                || (viewFlags &
CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE);
    }

    /***如果设置了代理,而且设置的代理可以处理点击事件,就返回true****/
    if (mTouchDelegate != null) {
        if (mTouchDelegate.onTouchEvent(event)) {
            return true;
        }
    }

    /**只要当前View设置了CLICKABLE或LONG_CLICKABLE或CONTEXT_CLICKABLE,就返回true*/
    if (((viewFlags & CLICKABLE) == CLICKABLE ||
            (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
            (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {

//touch事件的处理逻辑
       ……


        return true;
    }
    return false;
}


3、Touch事件是怎样由Activity传到View或ViewGroup的?

当Touch事件传递到Activity之后会调用Activity的dispatchTouchEvent方法,这个方法如下:

public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        onUserInteraction();
    }
    if (getWindow().superDispatchTouchEvent(ev)) {
        return true;
    }
    return onTouchEvent(ev);
}

可以看到,这个方法会调用getWindow.superDispatchTouchEvent()方法。

getWindow会返回mWindow属性,通过搜索发现:

mWindow = new PhoneWindow(this);

可以看到在PhoneWindow中实现了superDispatchTouchEvent()方法,我们需要看下PhoneWindow的源码。


以下是PhoneWindow的部分源码

public class PhoneWindow extends Window implements MenuBuilder.Callback {  

// This is the top—level view of the window, containing the window decor.  

    private DecorView mDecor;    

。。。。。。 

@Override  

public boolean superDispatchTouchEvent(MotionEvent event) {  

    return mDecor.superDispatchTouchEvent(event);  

}  

。。。。。

  private final class DecorView extends FrameLayout { 

。。。。。。

public boolean superDispatchTouchEvent(KeyEvent event) {  

    return super.dispatchTouchEvent(event);  

}  

。。。。。。

  }

}


由上可见PhoneWindow的superDispatchtouchEvent()方法中调用了mDecor对象的superDispatchtouchEvent()方法,这个方法中会调用super.dispatchTouchEvent()方法,因为DecorView继承自FramLayout,所以这样就将Touch事件由Activity传到了View中。


如果有一个点击事件所有的View均不处理,就会交由Activity处理,此时会调用Activity的onTouchEvent(),代码如下:


public boolean onTouchEvent(MotionEvent event) {
    if (mWindow.shouldCloseOnTouch(this, event)) {
        finish();
        return true;
    }
    return false;
}


参考:http://blog.csdn.net/yangzl2008/article/details/7908509

http://www.cnblogs.com/linjzong/p/4191891.html  

《Android开发艺术探索》

感谢以上大牛!!!


猜你喜欢

转载自blog.csdn.net/kanglupeng/article/details/52796983