本文轉自:http://mobile.51cto.com/abased-374715.htm:
一個最簡單的屏幕觸摸動作觸發了一系列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 方法進行分發,如果dispatchTouchEvent返回true ,則交給這個view的onTouchEvent處理,如果dispatchTouchEvent返回 false ,則交給這個 view 的 interceptTouchEvent 方法來決定是否要攔截這個事件,如果 interceptTouchEvent 返回 true ,也就是攔截掉了,則交給它的 onTouchEvent 來處理,如果 interceptTouchEvent 返回 false ,那麼就傳遞給子 view ,由子 view 的 dispatchTouchEvent 再來開始這個事件的分發。如果事件傳遞到某一層的子 view 的 onTouchEvent 上了,這個方法返回了 false ,那麼這個事件會從這個 view 往上傳遞,都是 onTouchEvent 來接收。而如果傳遞到最上面的 onTouchEvent 也返回 false 的話,這個事件就會“消失”,而且接收不到下一次事件。
通過語言描述這個處理邏輯很抽象,下面就用代碼來具體說明一下。
layout配置文件 main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <test.lzqdiy.MyLinearLayout xmlns: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 {
- /** Called when the activity is first created. */
- @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): dispatchTouchEvent action:ACTION_DOWN D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_DOWN D/MyTextView(11865): dispatchTouchEvent action:ACTION_DOWN D/MyTextView(11865): ---onTouchEvent action:ACTION_DOWN D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_MOVE D/MyLinearLayout(11865): onInterceptTouchEvent action: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): onInterceptTouchEvent action: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): dispatchTouchEvent action:ACTION_DOWN D/MyLinearLayout(13101): onInterceptTouchEvent action: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): dispatchTouchEvent action: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): dispatchTouchEvent action:ACTION_DOWN D/MyLinearLayout(13334): onInterceptTouchEvent action:ACTION_DOWN D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_DOWN D/MyLinearLayout(13334): dispatchTouchEvent action: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): dispatchTouchEvent action:ACTION_DOWN D/MyLinearLayout(13452): onInterceptTouchEvent action:ACTION_DOWN D/MyLinearLayout(13452): ---onTouchEvent action:ACTION_DOWN
結論:LinearLayout只處理了ACTION_DOWN事件,那麼其他的TouchEvent被誰處理了呢?答案是LinearLayout最外層的Activity處理了TouchEvent。