1. 程式人生 > >Android onTouch事件傳遞機制

Android onTouch事件傳遞機制

Android onTouch事件介紹:


Android的觸控事件:onClick, onScroll, onFling等等,都是由許多個Touch組成的。其中Touch的第一個狀態肯定是ACTION_DOWN, 表示按下了螢幕。之後,touch將會有後續事件,可能是:

ACTION_MOVE  //表示為移動手勢

ACTION_UP  //表示為離開螢幕

ACTION_CANCEL  //表示取消手勢,不會由使用者產生,而是由程式產生的

一個Action_DOWN, n個ACTION_MOVE, 1個ACTION_UP,就構成了Android中眾多的事件。

在Android中,有一類控制元件是中還可以包含其他的子控制元件,這類控制元件是繼承於ViewGroup類,例如:ListView, Gallery, GridView,LinearLayout。

還有一類控制元件是不能再包含子控制元件,例如:TextView。

在觸發OnTouch事件的時候Android的GroupView會呼叫如下三個函式:

public boolean dispatchTouchEvent(MotionEvent ev)      //用於事件的分發

public boolean onInterceptTouchEvent(MotionEvent ev)    //  用於事件的攔截

public boolean onTouchEvent(MotionEvent ev)     //處理事件


本文的主要討論物件就是ViewGroup類的控制元件巢狀時事件觸發情況。

對於ViewGroup類的控制元件,有一個很重要的方法,就是onInterceptTouchEvent(),用於處理事件並改變事件的傳遞方向,它的返回值是一個布林值,決定了Touch事件是否要向它包含的子View繼續傳遞,這個方法是從父View向子View傳遞。而方法onTouchEvent(),用於接收事件並處理,它的返回值也是一個布林值,決定了事件及後續事件是否繼續向上傳遞,這個方法是從子View向父View傳遞。touch事件在 onInterceptTouchEvent()和onTouchEvent以及各個childView間的傳遞機制完全取決於onInterceptTouchEvent()和onTouchEvent()的返回值。返回值為true表示事件被正確接收和處理了,返回值為false表示事件沒有被處理,將繼續傳遞下去。

ACTION_DOWN事件會傳到某個ViewGroup類的onInterceptTouchEvent,如果返回false,則DOWN事件繼續向子ViewGroup類的onInterceptTouchEvent傳遞,如果子View不是ViewGroup類的控制元件,則傳遞給它的onTouchEvent。

如果onInterceptTouchEvent返回了true,則DOWN事件傳遞給它的onTouchEvent,不再繼續傳遞,並且之後的後續事件也都傳遞給它的onTouchEvent。
如果某View的onTouchEvent返回了false,則DOWN事件繼續向其父ViewGroup類的onTouchEvent傳遞;如果返回了true,則後續事件會直接傳遞給其onTouchEvent繼續處理。(後續事件只會傳遞給對於必要事件ACTION_DOWN返回了true的onTouchEvent。

onInterceptTouchEvent()用於處理事件並改變事件的傳遞方向。處理事件這個不用說了,你在函式內部編寫程式碼處理就可以了。而決定傳遞方向的是返回值,返回為false時事件會傳遞給子控制元件的onInterceptTouchEvent();返回值為true時事件會傳遞給當前控制元件的onTouchEvent(),而不在傳遞給子控制元件,這就是所謂的Intercept(截斷)。

onTouchEvent() 用於處理事件,返回值決定當前控制元件是否消費(consume)了這個事件。可能你要問是否消費了又區別嗎,反正我已經針對事件編寫了處理程式碼?答案是有區別!比如ACTION_MOVE或者ACTION_UP發生的前提是一定曾經發生了ACTION_DOWN,如果你沒有消費ACTION_DOWN,那麼系統會認為ACTION_DOWN沒有發生過,所以ACTION_MOVE或者ACTION_UP就不能被捕獲。

在沒有重寫onInterceptTouchEvent()和onTouchEvent()的情況下(他們的返回值都是false)。

 

onTouch事件傳遞測試:

<?xml version="1.0" encoding="utf-8"?>  
<com.rpset.test.MyLinearLayout1 xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
    <com.rpset.test.MyLinearLayout2  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:gravity="center"  
        android:orientation="vertical" >  
        <com.rpset.test.MyTextView  
            android:id="@+id/tv"  
            android:layout_width="200px"  
            android:layout_height="200px"  
            android:background="#FFFFFF"  
            android:text="MyTextView"  
            android:textColor="#0000FF"  
            android:textSize="40sp"  
            android:textStyle="bold" />  
    </com.rpset.test.MyLinearLayout2>  
</com.rpset.test.MyLinearLayout1>  


注: 當點選MyTextView時,程式會先進入到LinearLayout1的dispatchTouchEvent中,這個類必須呼叫super.dispatchTouchEvent(ev);否則後面的兩個方法無法觸發,所以發現這個方法根本沒有必要重寫,因為框架是在 super.dispatchTouchEvent(ev)中來呼叫onInterceptTouchEvent和onTouchEvent方法的,所以手動的設定dispatchTouchEvent的返回值是無效的,除非你不想讓框架觸發這兩個方法。

 

對於MyTextView進行測試:

測試一:當三個view的dispatchTouchEvent,onInterceptTouchEvent(MyTextView沒有此方法),onTouchEvent均返回false,也就是說事件始終沒有被消費,那後續事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不會觸發。Log資訊如下:

04-09 12:01:55.019: D/MyLinearLayout(5435): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
04-09 12:01:55.027: D/MyLinearLayout(5435): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
04-09 12:01:55.043: D/MyLinearLayout(5435): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
04-09 12:01:55.043: D/MyLinearLayout(5435): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
04-09 12:01:55.047: D/MyLinearLayout(5435): MyTextView——dispatchTouchEvent action:ACTION_DOWN  
04-09 12:01:55.051: D/MyLinearLayout(5435): MyTextView——-onTouchEvent action:ACTION_DOWN  
04-09 12:01:55.051: D/MyLinearLayout(5435): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
04-09 12:01:55.054: D/MyLinearLayout(5435): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN  

  

結論:MyLinearLayout1,MyLinearLayout2,MyTextView都只處理了ACTION_DOWN,其餘的TouchEvent被外層的Activity處理了

 

傳遞示意圖:


 測試二:當只有MyTextView的onTouchEvent返回true,即事件最終在這裡消費,(action:ACTION_MOVE會重複出現多次,這裡僅代表一下) Log資訊如下:

04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
04-09 11:58:22.000: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
04-09 11:58:22.000: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_DOWN  
04-09 11:58:22.000: D/MyLinearLayout(4621): MyTextView——-onTouchEvent action:ACTION_DOWN  
  
04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE  
04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_MOVE  
04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_MOVE  
04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_MOVE  
04-09 11:58:22.133: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_MOVE  
04-09 11:58:22.133: D/MyLinearLayout(4621): MyTextView——-onTouchEvent action:ACTION_MOVE  
  
04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP  
04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP  
04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_UP  
04-09 11:58:22.179: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_UP  
04-09 11:58:22.179: D/MyLinearLayout(4621): MyTextView——-onTouchEvent action:ACTION_UP  


結論:MyTextView處理了所有的TouchEvent。

 

傳遞示意圖:

對MyLinearLayout2進行測試:

測試一:當MyLinearLayout2的onInterceptTouchEvent方法返回true時,傳送截斷,事件不再向下傳遞而是直接給當前MyLinearLayout2的onTouchEvent處理,那後續事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不會觸發。log資訊如下:

04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN  


結論:MyLinearLayout2,MyLinearLayout1都處理了ACTION_DOWN,其餘的由最外層的Activity處理了。

 

傳遞示意圖:


測試二:當MyLinearLayout2的onTouchEvent方法返回true時,後續的MOVE,Up事件會經過LayoutView1的onInterceptTouchEvent函式,然後到LayoutView2的onTouchEvent函式中去。不再經過MyLinearLayout2的onInterceptTouchEvent函式。 Log資訊如下:

04-09 18:17:26.410: D/MyLinearLayout(21504): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
04-09 18:17:26.410: D/MyLinearLayout(21504): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
04-09 18:17:26.414: D/MyLinearLayout(21504): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
04-09 18:17:26.414: D/MyLinearLayout(21504): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
04-09 18:17:26.418: D/MyLinearLayout(21504): MyTextView——dispatchTouchEvent action:ACTION_DOWN  
04-09 18:17:26.418: D/MyLinearLayout(21504): MyTextView——-onTouchEvent action:ACTION_DOWN  
04-09 18:17:26.418: D/MyLinearLayout(21504): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
  
04-09 18:17:26.437: D/MyLinearLayout(21504): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
04-09 18:17:26.437: D/MyLinearLayout(21504): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP  
04-09 18:17:26.441: D/MyLinearLayout(21504): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP  
04-09 18:17:26.445: D/MyLinearLayout(21504): MyLinearLayout2——-onTouchEvent action:ACTION_UP  

結論:MyTextView只處理了ACTION_DOWN事件,MyLinearLayout2處理了所有的TouchEvent事件。


傳遞示意圖:

 

測試三:當MyLinearLayout2的onInterceptTouchEvent和onTouchEvent 方法都返回true時,後續的MOVE,Up事件依然會經過LayoutView1的onInterceptTouchEvent函式,然後到LayoutView2的onTouchEvent函式中去。不再經過MyLinearLayout2的onInterceptTouchEvent函式。 Log資訊如下:

04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
  
04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE  
04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_MOVE  
04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_MOVE  
04-09 13:17:30.551: D/MyLinearLayout(9935): MyLinearLayout2——-onTouchEvent action:ACTION_MOVE  
  
04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP  
04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP  
04-09 13:17:30.648: D/MyLinearLayout(9935): MyLinearLayout2——-onTouchEvent action:ACTION_UP  


結論:MyLinearLayout2處理了所有的TouchEvent。

 傳遞示意圖:

對於MyLinearLayout1進行測試:

測試一:當MyLinearLayout1的onInterceptTouchEvent方法返回true時,傳送截斷,事件不再向下傳遞而是直接給當前MyLinearLayout1的onTouchEvent處理,那後續事件(ACTION_DOWN的ACTION_MOVE或者ACTION_UP)不會觸發。log資訊如下:

04-09 13:52:06.789: D/MyLinearLayout(12363): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
04-09 13:52:06.789: D/MyLinearLayout(12363): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
04-09 13:52:06.789: D/MyLinearLayout(12363): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN  

結論:MyLinearLayout1只處理了ACTION_DOWN,其餘的被外層的Avtivity處理了。

傳遞示意圖:


測試二:當MyLinearLayout1的onTouchEvent 方法返回true時,後續的MOVE,Up事件會直接傳給MyLinearLayout1的onTouchEvent()。不再經過MyLinearLayout1的onInterceptTouchEvent函式。Log資訊如下:

04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
04-09 18:26:58.125: D/MyLinearLayout(22294): MyTextView——dispatchTouchEvent action:ACTION_DOWN  
04-09 18:26:58.125: D/MyLinearLayout(22294): MyTextView——-onTouchEvent action:ACTION_DOWN  
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN  
  
04-09 18:26:58.215: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE  
04-09 18:26:58.215: D/MyLinearLayout(22294): MyLinearLayout1——-onTouchEvent action:ACTION_MOVE  
  
04-09 18:26:58.281: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
04-09 18:26:58.285: D/MyLinearLayout(22294): MyLinearLayout1——-onTouchEvent action:ACTION_UP  

結論:MyTextView和MyLinearLayout2只處理了ACTION_DOWN事件,MyLinearLayout1處理了所有的TouchEvent。

傳遞示意圖:



測試三:當MyLinearLayout1的onInterceptTouchEvent和onTouchEvent 方法都返回true時,後續的MOVE,Up事件會直接傳給MyLinearLayout1的onTouchEvent(),不傳給其他任何控制元件的任何函式。不再經過MyLinearLayout1的onInterceptTouchEvent函式。Log資訊如下:

04-09 13:58:04.199: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
04-09 13:58:04.199: D/MyLinearLayout(13116): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
04-09 13:58:04.203: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN  
  
04-09 13:58:04.324: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE  
04-09 13:58:04.328: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_MOVE  
  
04-09 13:58:04.367: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
04-09 13:58:04.367: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_UP  

結論:MyLinearLayout1處理了所有的TouchEvent。


 傳遞示意圖:

原文出處:http://blog.csdn.net/hyp712/article/details/8777835