android的Touch事件解析(dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent)

android的Touch事件解析(dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent)

(2012-05-20 12:34:04)

android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,

有必要对它进行深入的了解。 一个最简单的屏幕触摸动作触发了一系列Touch事件:

ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_MOVE->ACTION_UP
当屏幕中包含一个ViewGroup,而这个ViewGroup又包含一个子view,这个时候android系统如何

处理Touch事件呢?到底是ViewGroup来处理Touch事件,还是子view来处理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

当TouchEvent发生时,首先Activity将TouchEvent传递给最顶层的View,TouchEvent最先到达最顶层

view 的 dispatchTouchEvent ,然后由 dispatchTouchEvent 方法进行分发,

1、如果dispatchTouchEvent返回true,则交给这个view的onTouchEvent处理,    如果最终需要处理事件的viewonTouchEvent()返回了false,那么该事件将被传递至其上一层次的viewonTouchEvent()处理。如果最终需要处理事件的view onTouchEvent()返回了true,那么后续事件将可以继续传递给该viewonTouchEvent()处理。

2、如果dispatchTouchEvent返回 false ,则交给这个view的interceptTouchEvent方法来决定是否

要拦截这个事件,如果 interceptTouchEvent 返回 true,也就是

3、若果interceptTouchEvent返回true,表示拦截掉了,则交给它的onTouchEvent 来处理,

4、如果 interceptTouchEvent 返回 false,那么就传递给子view ,由子 view 的 d

ispatchTouchEvent 再来开始这个事件的分发。

5、如果事件传递到某一层的子 view 的onTouchEvent上了,这个方法返回了 false ,那么这个事件

会从这个view 往上传递,都是 onTouchEvent来接收。

6、如果传递到最上面的 onTouchEvent 也返回 false的话,这个事件就会“消失”,而且接收

不到下一次事件。
通过语言描述这个处理逻辑很抽象,下面我就用代码来具体说明一下。
layout配置文件 main.xml
<?xml version="1.0"encoding="utf-8"?>
<test.lzqdiy.MyLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<test.lzqdiy.MyTextView
android:layout_width="200px"
android:layout_height="200px"
android:id="@+id/tv"
android:text="lzqdiy"
android:textSize="40sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textColor="#0000FF"/>
</test.lzqdiy.MyLinearLayout>
节点层次很简单,一个LinearLayout中添加了一个TextView。
下面是java代码:
package test.lzqdiy;

import android.app.Activity;
import android.os.Bundle;

public class TestTouchEventApp extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
package test.lzqdiy;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class MyLinearLayout extends LinearLayout {
private final String TAG = "MyLinearLayout";

public MyLinearLayout(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, "dispatchTouchEvent action:ACTION_DOWN");

break;

case MotionEvent.ACTION_MOVE:

Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");

break;

case MotionEvent.ACTION_UP:

Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");

break;

case MotionEvent.ACTION_CANCEL:

Log.d(TAG, "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, "onInterceptTouchEvent action:ACTION_DOWN");

break;

case MotionEvent.ACTION_MOVE:

Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");

break;

case MotionEvent.ACTION_UP:

Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");

break;

case MotionEvent.ACTION_CANCEL:

Log.d(TAG, "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, "---onTouchEvent action:ACTION_DOWN");

break;

case MotionEvent.ACTION_MOVE:

Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");

break;

case MotionEvent.ACTION_UP:

Log.d(TAG, "---onTouchEvent action:ACTION_UP");

break;

case MotionEvent.ACTION_CANCEL:

Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");

break;

}

return true;
}

}

package test.lzqdiy;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

public class MyTextView extends TextView {

private final String TAG = "MyTextView";

public MyTextView(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();

switch (action) {

case MotionEvent.ACTION_DOWN:

Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");

break;

case MotionEvent.ACTION_MOVE:

Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");

break;

case MotionEvent.ACTION_UP:

Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");

break;

case MotionEvent.ACTION_CANCEL:

Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");

break;

}
return super.dispatchTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {

int action = ev.getAction();

switch (action) {

case MotionEvent.ACTION_DOWN:

Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");

break;

case MotionEvent.ACTION_MOVE:

Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");

break;

case MotionEvent.ACTION_UP:

Log.d(TAG, "---onTouchEvent action:ACTION_UP");

break;

case MotionEvent.ACTION_CANCEL:

Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");

break;

}

return true;

}

}

为了指代方便,下面将MyLinearLayout简称为L,将MyTextView简称为T,

L.onInterceptTouchEvent=true表示的含义为MyLinearLayout中的onInterceptTouchEvent

方法返回值为true,通过程序运行时输出的Log来说明调用时序。
第1种情况L.onInterceptTouchEvent=false&&L.onTouchEvent=true

&&T.onTouchEvent=true输出下面的Log:
D/MyLinearLayout(11865): dispatchTouchEventaction:ACTION_DOWN
D/MyLinearLayout(11865): onInterceptTouchEventaction:ACTION_DOWN
D/MyTextView(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(11865): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): dispatchTouchEventaction:ACTION_MOVE
D/MyLinearLayout(11865): onInterceptTouchEventaction:ACTION_MOVE
D/MyTextView(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyTextView(11865): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(11865): onInterceptTouchEventaction:ACTION_UP
D/MyTextView(11865): dispatchTouchEvent action:ACTION_UP
D/MyTextView(11865): ---onTouchEvent action:ACTION_UP
结论:TouchEvent完全由TextView处理。
第2种情况L.onInterceptTouchEvent=false&&L.onTouchEvent=true

&&T.onTouchEvent=false输出下面的Log:
D/MyLinearLayout(13101): dispatchTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13101): onInterceptTouchEventaction:ACTION_DOWN
D/MyTextView(13101): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): dispatchTouchEventaction:ACTION_MOVE
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_UP
结论:TextView只处理了ACTION_DOWN事件,LinearLayout处理了所有的TouchEvent。
第3种情况L.onInterceptTouchEvent=true&&L.onTouchEvent=true 输出下面的Log:
D/MyLinearLayout(13334): dispatchTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13334): onInterceptTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): dispatchTouchEventaction:ACTION_MOVE
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_UP
结论:LinearLayout处理了所有的TouchEvent。
第4种情况L.onInterceptTouchEvent=true&&L.onTouchEvent=false 输出下面的Log:
D/MyLinearLayout(13452): dispatchTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13452): onInterceptTouchEventaction:ACTION_DOWN
D/MyLinearLayout(13452): ---onTouchEvent action:ACTION_DOWN
结论:LinearLayout只处理了ACTION_DOWN事件,那么其他的TouchEvent被谁处理了呢?

答案是LinearLayout最外层的Activity处理了TouchEvent。


  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值