Android(安卓)Touch事件传递机制解析
16lz
2022-01-17
Android Touch事件传递机制解析 android系统中的每个View的子类都具有下面三个和TouchEvent处理密切相关的方法: 1)public boolean dispatchTouchEvent(MotionEvent ev) 这个方法用来分发TouchEvent 2)public boolean onInterceptTouchEvent(MotionEvent ev) 这个方法用来拦截TouchEvent 3)public boolean onTouchEvent(MotionEvent ev) 这个方法用来处理TouchEvent 测试程序界面 下述3个Layout包含关系见如下界面图。 2012-4-25 19:12 上传 下载附件 (20 KB)
事件传递记录结果如上图。 由于Down、Move、Up事件处理流程略微不同,故分开分析。 ACTION_DOWN事件处理流程: 首先触摸事件发生时(ACTION_DOWN),由系统调用Activity的dispatchTouchEvent方法,分发该事件。根据触摸事件的坐标,将此事件传递给out的dispatchTouchEvent处理,out则调用onInterceptTouchEvent 判断事件是由自己处理,还是继续分发给子View。此处由于out不处理Touch事件,故根据事件发生坐标,将事件传递给out的直接子View(即middle)。 Middle及Center中事件处理过程同上。但是由于Center组件是clickable 表示其能处理Touch事件,故center中的onInterceptTouchEvent方法将事件传递给center自己的onTouchEvent方法处理。至此,此Touch事件已被处理,不继续进行传递。 Move和 up 事件处理流程类似,但是再center内的dispatchTouchEvent方法内被直接分配给onTouchEvent处理,不需经过onInterceptTouchEvent判断。这是由于,android系统中将1个down事件、n个move事件、1个up事件整体作为一次逻辑上的触控操作,Down事件已经确定了处理事件的对象,则后续的move、up事件也确定了处理事件的对象。 状态2:都不处理事件
工程下载见:
Touch.7z.zip
状态1:由center处理Touch事件
Xml如下:<?xml version="1.0" encoding="utf-8"?> 注意:只有center这个部分是会处理/消费 Touch事件。
事件传递记录结果如上图。 由于Down、Move、Up事件处理流程略微不同,故分开分析。 ACTION_DOWN事件处理流程: 首先触摸事件发生时(ACTION_DOWN),由系统调用Activity的dispatchTouchEvent方法,分发该事件。根据触摸事件的坐标,将此事件传递给out的dispatchTouchEvent处理,out则调用onInterceptTouchEvent 判断事件是由自己处理,还是继续分发给子View。此处由于out不处理Touch事件,故根据事件发生坐标,将事件传递给out的直接子View(即middle)。 Middle及Center中事件处理过程同上。但是由于Center组件是clickable 表示其能处理Touch事件,故center中的onInterceptTouchEvent方法将事件传递给center自己的onTouchEvent方法处理。至此,此Touch事件已被处理,不继续进行传递。 Move和 up 事件处理流程类似,但是再center内的dispatchTouchEvent方法内被直接分配给onTouchEvent处理,不需经过onInterceptTouchEvent判断。这是由于,android系统中将1个down事件、n个move事件、1个up事件整体作为一次逻辑上的触控操作,Down事件已经确定了处理事件的对象,则后续的move、up事件也确定了处理事件的对象。 状态2:都不处理事件
Xml如下:<?xml version="1.0" encoding="utf-8"?> 轻触center部分logcat输出结果
事件处理流程大致同上,区别是此状态下,所有组件都不会处理事件,事件并不会被center的onTouchEvent方法“消费”,则事件会层层逆向传递回到Activity,若Activity也不对此事件进行处理,此事件相当于消失了(无效果)。 对于后续的move、up事件,由于第一个down事件已经确定由Activity处理事件,故up事有由Activity的dispatchTouchEvent直接分发给自己的onTouchEvent方法处理。
源代码:package dk.touch;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyLayout out=(MyLayout) findViewById(R.id.out); out.setName("out"); MyLayout middle=(MyLayout) findViewById(R.id.middle); middle.setName("middle"); MyLayout center=(MyLayout) findViewById(R.id.center); center.setName("center"); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action=ev.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println("Activity"+"|"+actionName+":dispatchTouchEvent"); return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { int action=event.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println("Activity"+"|"+actionName+":onTouchEvent"); return super.onTouchEvent(event); } }package dk.touch;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.LinearLayout;public class MyLayout extends LinearLayout { private String name=""; public MyLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) { int action=event.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println(name+"|"+actionName+":onTouchEvent"); return super.onTouchEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action=ev.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println(name+"|"+actionName+":dispatchTouchEvent"); return super.dispatchTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { int action=ev.getAction(); String actionName=""; switch(action) { case MotionEvent.ACTION_DOWN: actionName="ACTION_DOWN"; break; case MotionEvent.ACTION_MOVE: actionName="ACTION_MOVE"; break; case MotionEvent.ACTION_UP: actionName="ACTION_UP"; break; } System.out.println(name+"|"+actionName+":onInterceptTouchEvent"); return super.onInterceptTouchEvent(ev); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
工程下载见:
Touch.7z.zip
更多相关文章
- Android中Activity全局共享方法AppContext
- 《Android面试宝典》学习笔记(第四章:对话框、信息提示和菜单)
- Android学习笔记―第十一章 Fragment
- Gson解析(详解)
- 国内目前最全面的介绍――Android中的BroadCastReceiver
- Android日志打印与处理 UncaughtExceptionHandler 提交服务器
- Android-计算器
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用