Android 事件分发

1. MotionEvent事件

在MotionEvent操作里有多种手势,常用手势有

    ACTION_DOWN,按下
    ACTION_UP,抬起
    ACTION_MOVE,移动
    ACTION_CANCEL,取消

所有的操作都会在Activity、View和ViewGroup中处理。
在View和Activity中,有两个方法dispatchTouchEvent(MotionEvent)和onTouchEvent(MotionEvent),在ViewGroup中还有另外一个方法onInterceptTouchEvent(MotionEvent)。
2. 测试控件

TouchEventView和TouchEventViewGroup用来跟踪手势的传递。

public class TouchEventView extends View {
    public final static String LOG_TAG = "TouchEventView";

    public TouchEventView(Context context) {
        super(context);
    }

    public TouchEventView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        LogTool.logi(LOG_TAG, "before dispatchTouchEvent " + event.getAction());
        boolean handle = super.dispatchTouchEvent(event);
        LogTool.logi(LOG_TAG, "after dispatchTouchEvent");
        return handle;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        LogTool.logi(LOG_TAG, "before onTouchEvent " + event.getAction());
        boolean handle = super.onTouchEvent(event);
        LogTool.logi(LOG_TAG, "after onTouchEvent");
        return handle;
    }

}

public class TouchEventViewGroup extends RelativeLayout {
    private final static String LOG_TAG = "TouchRelativeLayout";

    public TouchEventViewGroup(Context context) {
        super(context);
    }

    public TouchEventViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        LogTool.logi(LOG_TAG, "before dispatchTouchEvent " + event.getAction());
        boolean handle = super.dispatchTouchEvent(event);
        LogTool.logi(LOG_TAG, "after dispatchTouchEvent");
        return handle;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        LogTool.logi(LOG_TAG, "before onInterceptTouchEvent " + ev.getAction());
        boolean handle = super.onInterceptTouchEvent(ev);
        LogTool.logi(LOG_TAG, "after onInterceptTouchEvent");
        return handle;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        LogTool.logi(LOG_TAG, "before onTouchEvent " + event.getAction());
        boolean handle = super.onTouchEvent(event);
        LogTool.logi(LOG_TAG, "after onTouchEvent");
        return handle;
    }

}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffffff" >

    <com.blog.demo.custom.widget.TouchEventViewGroup
        android:layout_width="240dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="#ffff0000" >

        <com.blog.demo.custom.widget.TouchEventView
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_centerInParent="true"
            android:background="#ff0000ff" />

    </com.blog.demo.custom.widget.TouchEventViewGroup>

</RelativeLayout>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

3. MotionEvent流程

在Activity里面有一个ViewGroup,ViewGroup里面包含一个View,点击View后会发出MotionEvent事件,每个事件都会从ACTION_DOWN开始,到ACTION_UP结束,也会有ACTION_MOVE、ACTION_CANCEL。

    每次ACTION_DOWN,都会调用dispatchTouchEvent(MotionEvent)方法,从Activity->ViewGroup->View,一层层往下传递。在View的dispatchTouchEvent(MotionEvent)中调用onTouchEvent(MotionEvent),返回false的话,往上传递给ViewGroup和Activity,依次调用onTouchEvent(MotionEvent)。

    如果ACTION_DOWN没有在View或ViewGroup中没有处理,Activity将在onTouchEvent(MotionEvent)中处理接下来的所有事件。如果ACTION_DOWN在View或ViewGroup处理,但没有处理其他MotionEvent事件,Activity依然会调用onTouchEvent(MotionEvent)。

    如果在View的dispatchTouchEvent(MotionEvent)或者onTouchEvent(MotionEvent)里面返回true,表明View将处理Action,不会调用ViewGroup和Activy的onTouchEvent(MotionEvent)方法。其它MotionEvent事件先在View的onTouchEvent(MotionEvent)中处理。未处理的交给Activity的onTouchEvent(MotionEvent)方法。

    如果在ViewGroup的dispatchTouchEvent(MotionEvent)或者onTouchEvent(MotionEvent)里面返回true,表明ViewGroup将处理。接下来的MotionEvent事件先在ViewGroup中的onTouchEvent(MotionEvent)中处理。未处理的交给Activity的onTouchEvent(MotionEvent)方法。

    在ViewGroup中会先调用onInterceptTouchEvent(MotionEvent)过滤,然后才会传递给View。如果ViewGroup的onInterceptTouchEvent(MotionEvent)返回true,直接调用ViewGroup的onTouchEvent(MotionEvent)。onInterceptTouchEvent(MotionEvent)只会调用一次。

    如果View中处理了ACTION_DOWN,在其它MotionEvent事件时ViewGroup的onInterceptTouchEvent(MotionEvent)返回true,则会传递ACTION_CANCEL给View,接下来的MotionEvent事件都会调用ViewGroup的onTouchEvent(MotionEvent)处理。

4. 总结

    没有处理的事件都会在Activity的onTouchEvent(MotionEvent)中处理。

    ACTION_DOWN事件在哪里处理,接下来的MotionEvent事件都只会传递到那里。

    如果ViewGroup中的onInterceptTouchEvent(MotionEvent)返回true,事件将不会再往下传递。onInterceptTouchEvent(MotionEvent)返回true后不会再次调用。

    如果ViewGroup的子控件在ACTION_DOWN时处理了事件,那么在onInterceptTouchEvent(MotionEvent)返回true后代替子控件处理MotionEvent。

6. OnTouchListener

View可以添加监听器,使用setOnTouchListener(OnTouchListener)方法添加。在dispatchTouchEvent(MotionEvent)中,先调用OnTouchListener的onTouch(View, MotionEvent)。如果返回true,返回到上一层,否则调用onTouchEvent(MotionEvent)。

public boolean dispatchTouchEvent(MotionEvent event) {
    ... ...

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

    ... ...
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

相关文章
Android 事件分发
Android Scroller和VelocityTracker类
---------------------
作者:假装你是大灰狼
来源:CSDN
原文:https://blog.csdn.net/chennai1101/article/details/87097099
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2019-06-12 18:05  天涯海角路  阅读(111)  评论(0)    收藏  举报