Android Touch事件传递机制解析 android系统中的每个View的子类都具有下面三个和TouchEvent处理密切相关的方法: 1public boolean dispatchTouchEvent(MotionEvent ev)  这个方法用来分发TouchEvent 2public boolean onInterceptTouchEvent(MotionEvent ev) 这个方法用来拦截TouchEvent 3public boolean onTouchEvent(MotionEvent ev) 这个方法用来处理TouchEvent 测试程序界面 下述3Layout包含关系见如下界面图。   2012-4-25 19:12 上传 下载附件  (20 KB)


状态1:由center处理Touch事件

Xml如下:<?xml version="1.0" encoding="utf-8"?>                                                注意:只有center这个部分是会处理/消费 Touch事件。

事件传递记录结果如上图。 由于DownMoveUp事件处理流程略微不同,故分开分析。 ACTION_DOWN事件处理流程: 首先触摸事件发生时(ACTION_DOWN),由系统调用ActivitydispatchTouchEvent方法,分发该事件。根据触摸事件的坐标,将此事件传递给outdispatchTouchEvent处理,out则调用onInterceptTouchEvent 判断事件是由自己处理,还是继续分发给子View。此处由于out不处理Touch事件,故根据事件发生坐标,将事件传递给out的直接子View(即middle)。 MiddleCenter中事件处理过程同上。但是由于Center组件是clickable 表示其能处理Touch事件,故center中的onInterceptTouchEvent方法将事件传递给center自己的onTouchEvent方法处理。至此,此Touch事件已被处理,不继续进行传递。 Move和 up 事件处理流程类似,但是再center内的dispatchTouchEvent方法内被直接分配给onTouchEvent处理,不需经过onInterceptTouchEvent判断。这是由于,android系统中将1down事件、nmove事件、1up事件整体作为一次逻辑上的触控操作,Down事件已经确定了处理事件的对象,则后续的moveup事件也确定了处理事件的对象。 状态2:都不处理事件

Xml如下:<?xml version="1.0" encoding="utf-8"?>                                                轻触center部分logcat输出结果




事件处理流程大致同上,区别是此状态下,所有组件都不会处理事件,事件并不会被centeronTouchEvent方法“消费”,则事件会层层逆向传递回到Activity,若Activity也不对此事件进行处理,此事件相当于消失了(无效果)。 对于后续的moveup事件,由于第一个down事件已经确定由Activity处理事件,故up事有由ActivitydispatchTouchEvent直接分发给自己的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 

更多相关文章

  1. Android中Activity全局共享方法AppContext
  2. 《Android面试宝典》学习笔记(第四章:对话框、信息提示和菜单)
  3. Android学习笔记―第十一章 Fragment
  4. Gson解析(详解)
  5. 国内目前最全面的介绍――Android中的BroadCastReceiver
  6. Android日志打印与处理 UncaughtExceptionHandler 提交服务器
  7. Android-计算器
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. android后台运行服务,加验证
  2. Android的线程使用来更新UI----Thread、H
  3. Hierarchy Viewer 帮你分析应用程序UI布
  4. ap与sta共存
  5. Android开发之Android(安卓)Studio安装教
  6. Android(安卓)Material Design :LinearLay
  7. SAP BSP, Java Web Project,Android和微信
  8. Ubuntu搭建Eclipse+JDK+SDK的Android
  9. Android(安卓)用axis2 webService产生jav
  10. Gradle build 报错:Received status code