从MotionEvent再次理解事件分发机制

很多博客都讲了事件分发机制,这篇博客来测试下点击事件的MotioEvent是如何传递的

首先我们的布局比较简单,最外层红色的是ParentView,里面灰绿色的是ChildView

xml布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".touchevent.view.TouchEventActivity">
    <com.justtest.touchevent.view.ParentView
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.justtest.touchevent.view.ChildView
            android:layout_gravity="center"
            android:background="@color/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="300dp">
        </com.justtest.touchevent.view.ChildView>
    </com.justtest.touchevent.view.ParentView>
</RelativeLayout>

ParentView代码:

public class ParentView extends LinearLayout {
    public static final String TAG = "Liangchaojie";

    public ParentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, TAG);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "1:dispatchTouchEvent action:ACTION_DOWN");
                //return true;
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG, "1:dispatchTouchEvent action:ACTION_MOVE");
                break;
            //break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "1:dispatchTouchEvent action:ACTION_UP");
                //return true;
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG, "1:dispatchTouchEvent action:ACTION_CANCEL");
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "1:onInterceptTouchEvent action:ACTION_DOWN");
                //return true;
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG, "1:onInterceptTouchEvent action:ACTION_MOVE");
               // return true;
                break;
            //break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "1:onInterceptTouchEvent action:ACTION_UP");
                //return true;
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG, "1:onInterceptTouchEvent action:ACTION_CANCEL");
                break;
            case MotionEvent.ACTION_OUTSIDE:
                Log.d(TAG, "1:onInterceptTouchEvent action:ACTION_OUTSIDE");
                break;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "1:onTouchEvent action:ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG, "1:onTouchEvent action:ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "1:onTouchEvent action:ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG, "1:onTouchEvent action:ACTION_CANCEL");
                break;
        }
        return true;
    }
}

ChildView代码

public class ChildView extends LinearLayout {
    public static final String TAG = "Liangchaojie";

    public ChildView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, TAG);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "2:dispatchTouchEvent action:ACTION_DOWN");
                //return true;
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG, "2:dispatchTouchEvent action:ACTION_MOVE");
                break;
            //break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "2:dispatchTouchEvent action:ACTION_UP");
                //return true;
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG, "2:dispatchTouchEvent action:ACTION_CANCEL");
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch(action){
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG,"2:onInterceptTouchEvent action:ACTION_DOWN");
                break;
            //return true;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG,"2:onInterceptTouchEvent action:ACTION_MOVE");
                break;
            //return true;
            case MotionEvent.ACTION_UP:
                Log.d(TAG,"2:onInterceptTouchEvent action:ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG,"2:onInterceptTouchEvent action:ACTION_CANCEL");
                break;
        }
        return false;
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch(action){
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG,"2:onTouchEvent action:ACTION_DOWN");
                //return false;
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG,"2:onTouchEvent action:ACTION_MOVE");
               // return false;
                break;
            //break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG,"2:onTouchEvent action:ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG,"2:onTouchEvent action:ACTION_CANCEL");
                break;
        }
        return true;
    }
}

1 让我们先测试下点击ParentView

2019-05-27 20:13:06.402 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_DOWN
2019-05-27 20:13:06.402 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_DOWN
2019-05-27 20:13:06.403 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_DOWN
2019-05-27 20:13:06.441 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_UP
2019-05-27 20:13:06.441 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_UP

结果和我们理解的一样,分别走了dispatchTouchEvent,onInterceptTouchEvent和onTouchEvent方法,不过View的事件分发是逐个执行的,什么意思呢?比如ACTION_DOWN事件,将这个事件彻底执行完才会执行ACTION_UP事件,一个事件从被捕获到处理,必须完成才会进入下一个事件。

如果当前的view确认消费此事件,下次再传递给此view的时候不会再拦截这个事件,这里ACTION_UP并没有在onInterceptTouchEvent方法里面执行,因为之前已经交给此View处理了,就不再拦截其消费了

2 点击ChildView

2019-05-27 20:31:07.040 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_DOWN
2019-05-27 20:31:07.040 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_DOWN
2019-05-27 20:31:07.040 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_DOWN
2019-05-27 20:31:07.040 27431-27431/com.justtest D/Liangchaojie: 2:onInterceptTouchEvent action:ACTION_DOWN
2019-05-27 20:31:07.041 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_DOWN
2019-05-27 20:31:07.115 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_UP
2019-05-27 20:31:07.115 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_UP
2019-05-27 20:31:07.115 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_UP
2019-05-27 20:31:07.115 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_UP

这里的日志更能看清楚事件分发机制,ParentView(1)传递给ChildView(2)

3 在ParentView上按下滑动一部分距离再松手

2019-05-27 20:33:13.748 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_DOWN
2019-05-27 20:33:13.749 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_DOWN
2019-05-27 20:33:13.749 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_DOWN
2019-05-27 20:33:13.793 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.793 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.808 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.809 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.825 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.826 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.842 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.842 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.861 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.861 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:33:13.862 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_UP
2019-05-27 20:33:13.863 27431-27431/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_UP

就是多了很多ACTION_MOVE操作

3 在ChildView上按下滑动一部分距离再松手

2019-05-27 20:35:02.327 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_DOWN
2019-05-27 20:35:02.327 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_DOWN
2019-05-27 20:35:02.327 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_DOWN
2019-05-27 20:35:02.327 27431-27431/com.justtest D/Liangchaojie: 2:onInterceptTouchEvent action:ACTION_DOWN
2019-05-27 20:35:02.327 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_DOWN
2019-05-27 20:35:02.408 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.409 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.409 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.409 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.425 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.426 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.426 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.426 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.439 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.440 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.440 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.440 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.456 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.456 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.456 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.456 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.472 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.473 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.473 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.473 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_MOVE
2019-05-27 20:35:02.474 27431-27431/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_UP
2019-05-27 20:35:02.474 27431-27431/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_UP
2019-05-27 20:35:02.474 27431-27431/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_UP
2019-05-27 20:35:02.474 27431-27431/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_UP

到此为止基本上正常的事件分发机制我们都知道了其执行过程

4 面试会问的ACTION_CANCEL

/**
     * Constant for {@link #getActionMasked}: The current gesture has been aborted.
     * You will not receive any more points in it.  You should treat this as
     * an up event, but not perform any action that you normally would.
     */
    public static final int ACTION_CANCEL           = 3;

这段话是说ACTION_CANCEL 意味着手势已经被废弃,你将收不到此手势的任何坐标点了

在ParentView的onInterceptTouchEvent方法中做一下修改

 case MotionEvent.ACTION_MOVE:
                Log.d(TAG, "1:onInterceptTouchEvent action:ACTION_MOVE");
                return true;

当检测是move事件时,我们父布局拦截这个事件交给父布局去处理,这样我们再去在ChildView上按下滑动一部分距离再松手

2019-05-27 20:44:23.759 28805-28805/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_DOWN
2019-05-27 20:44:23.759 28805-28805/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_DOWN
2019-05-27 20:44:23.759 28805-28805/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_DOWN
2019-05-27 20:44:23.759 28805-28805/com.justtest D/Liangchaojie: 2:onInterceptTouchEvent action:ACTION_DOWN
2019-05-27 20:44:23.759 28805-28805/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_DOWN
2019-05-27 20:44:23.824 28805-28805/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.824 28805-28805/com.justtest D/Liangchaojie: 1:onInterceptTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.824 28805-28805/com.justtest D/Liangchaojie: 2:dispatchTouchEvent action:ACTION_CANCEL
2019-05-27 20:44:23.824 28805-28805/com.justtest D/Liangchaojie: 2:onTouchEvent action:ACTION_CANCEL
2019-05-27 20:44:23.840 28805-28805/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.840 28805-28805/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.857 28805-28805/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.857 28805-28805/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.874 28805-28805/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.874 28805-28805/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.890 28805-28805/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.890 28805-28805/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.907 28805-28805/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.907 28805-28805/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_MOVE
2019-05-27 20:44:23.916 28805-28805/com.justtest D/Liangchaojie: 1:dispatchTouchEvent action:ACTION_UP
2019-05-27 20:44:23.916 28805-28805/com.justtest D/Liangchaojie: 1:onTouchEvent action:ACTION_UP

我们发现

1 父布局没有调用ACTION_CANCEL,还是正常的处理事件
2 子布局开始调用ACTION_CANCEL,也就是说这个ACTION_CANCEL有点像是死亡通知的意思

这个ACTION_CANCEL是在子布局没有能力处理触摸事件只能有父布局处理的时候,父布局对子布局下发的消息,告诉子布局不用处理了,以后的触摸事件不再给子布局了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值