Android Touch事件传递机制解析

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



状态1:由center处理Touch事件

Xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <dk.touch.MyLayout
        android:id="@+id/out"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
            android:background="#ff345600"
         >
        <dk.touch.MyLayout
            android:id="@+id/middle"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:gravity="center"
            android:background="#ff885678"
            >
            <dk.touch.MyLayout
                android:id="@+id/center"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="#ff345678"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:clickable="true"
                 >
            </dk.touch.MyLayout>
        </dk.touch.MyLayout>
    </dk.touch.MyLayout>
</LinearLayout>
注意:只有center这个部分是会处理/消费 Touch事件。
2.png

事件传递记录结果如上图。
由于DownMoveUp事件处理流程略微不同,故分开分析。
ACTION_DOWN事件处理流程:
3.png
首先触摸事件发生时(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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <dk.touch.MyLayout
        android:id="@+id/out"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
            android:background="#ff345600"
         >
        <dk.touch.MyLayout
            android:id="@+id/middle"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:gravity="center"
            android:background="#ff885678"
            >
            <dk.touch.MyLayout
                android:id="@+id/center"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="#ff345678"
                 >
            </dk.touch.MyLayout>
        </dk.touch.MyLayout>
    </dk.touch.MyLayout>
</LinearLayout>
轻触center部分logcat输出结果

4.png

5.png

事件处理流程大致同上,区别是此状态下,所有组件都不会处理事件,事件并不会被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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值