Activity dispatchTouchEvent事件过程分析(预览篇)

来源:互联网 发布:箭牌官方旗舰店 知乎 编辑:程序博客网 时间:2024/06/10 06:49

主界面的整体效果如上。

其中Layout布局如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center">    <miss.goddess.touchdispatch.view.TextViewExt        android:layout_width="100dip"        android:layout_height="100dip"        android:background="#ccc"        android:padding="50dip"        android:text="@string/hello_world"/></LinearLayout>

--------------------------------------------------------------------------------

Activity覆盖了两个方法

@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {Log.d("touch:activity-dispatch", EventName.getName(event));return super.dispatchTouchEvent(event);}@Overridepublic boolean onTouchEvent(MotionEvent event) {Log.d("touch:activity-onTouch", EventName.getName(event));return super.onTouchEvent(event);}
TextViewExt主要覆盖了onTouchEvent方法

public class TextViewExt extends TextView {public TextViewExt(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onTouchEvent(MotionEvent event) {Log.d("touch:view", EventName.getName(event));return super.onTouchEvent(event);}}
开始测试

-----------------------------------------------------------------------------------------------------

分两种情况触摸屏幕

1、 滑动轨迹【A-O-B】


打印的日志如下

06-04 14:40:03.950: D/touch:activity-dispatch(14390): ACTION_DOWN---06-04 14:40:03.960: D/touch:activity-onTouch(14390): ACTION_DOWN---06-04 14:40:04.060: D/touch:activity-dispatch(14390): ACTION_MOVE06-04 14:40:04.060: D/touch:activity-onTouch(14390): ACTION_MOVE。。。。。。。。。。。。。。。。。。。。(省略)06-04 14:40:04.550: D/touch:activity-dispatch(14390): ACTION_MOVE06-04 14:40:04.550: D/touch:activity-onTouch(14390): ACTION_MOVE06-04 14:40:04.600: D/touch:activity-dispatch(14390): ACTION_UP06-04 14:40:04.600: D/touch:activity-onTouch(14390): ACTION_UP

可以发现。TextViewExt的onTouch事件根本不会执行

所有的touch事件都被Activity拦截


2、 滑动轨迹【O-B】

06-04 14:49:29.380: D/touch:activity-dispatch(14390): ACTION_DOWN---06-04 14:49:29.380: D/touch:view(14390): ACTION_DOWN---06-04 14:49:29.380: D/touch:activity-onTouch(14390): ACTION_DOWN---06-04 14:49:29.450: D/touch:activity-dispatch(14390): ACTION_MOVE06-04 14:49:29.450: D/touch:activity-onTouch(14390): ACTION_MOVE。。。。。。。。。。。。。。。。。。。。(省略)06-04 14:49:29.730: D/touch:activity-dispatch(14390): ACTION_MOVE06-04 14:49:29.730: D/touch:activity-onTouch(14390): ACTION_MOVE06-04 14:49:29.730: D/touch:activity-dispatch(14390): ACTION_UP06-04 14:49:29.730: D/touch:activity-onTouch(14390): ACTION_UP

可以发现TextViewExt只接收到Action_Down事件,其他事件都被拦截了                                             
0 0