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事件傳遞測試:

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <com.rpset.test.MyLinearLayout1 xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <com.rpset.test.MyLinearLayout2  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         android:gravity="center"  
  10.         android:orientation="vertical" >  
  11.         <com.rpset.test.MyTextView  
  12.             android:id="@+id/tv"  
  13.             android:layout_width="200px"  
  14.             android:layout_height="200px"  
  15.             android:background="#FFFFFF"  
  16.             android:text="MyTextView"  
  17.             android:textColor="#0000FF"  
  18.             android:textSize="40sp"  
  19.             android:textStyle="bold" />  
  20.     </com.rpset.test.MyLinearLayout2>  
  21. </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信息如下:

[java] view plaincopy
  1. 04-09 12:01:55.019: D/MyLinearLayout(5435): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
  2. 04-09 12:01:55.027: D/MyLinearLayout(5435): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
  3. 04-09 12:01:55.043: D/MyLinearLayout(5435): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
  4. 04-09 12:01:55.043: D/MyLinearLayout(5435): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
  5. 04-09 12:01:55.047: D/MyLinearLayout(5435): MyTextView——dispatchTouchEvent action:ACTION_DOWN  
  6. 04-09 12:01:55.051: D/MyLinearLayout(5435): MyTextView——-onTouchEvent action:ACTION_DOWN  
  7. 04-09 12:01:55.051: D/MyLinearLayout(5435): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
  8. 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信息如下:

[java] view plaincopy
  1. 04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
  2. 04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
  3. 04-09 11:58:21.992: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
  4. 04-09 11:58:22.000: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
  5. 04-09 11:58:22.000: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_DOWN  
  6. 04-09 11:58:22.000: D/MyLinearLayout(4621): MyTextView——-onTouchEvent action:ACTION_DOWN  
  7.   
  8. 04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE  
  9. 04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_MOVE  
  10. 04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_MOVE  
  11. 04-09 11:58:22.117: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_MOVE  
  12. 04-09 11:58:22.133: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_MOVE  
  13. 04-09 11:58:22.133: D/MyLinearLayout(4621): MyTextView——-onTouchEvent action:ACTION_MOVE  
  14.   
  15. 04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
  16. 04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP  
  17. 04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP  
  18. 04-09 11:58:22.179: D/MyLinearLayout(4621): MyLinearLayout2——onInterceptTouchEvent action:ACTION_UP  
  19. 04-09 11:58:22.179: D/MyLinearLayout(4621): MyTextView——dispatchTouchEvent action:ACTION_UP  
  20. 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信息如下:

[java] view plaincopy
  1. 04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
  2. 04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
  3. 04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
  4. 04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
  5. 04-09 12:54:44.398: D/MyLinearLayout(9177): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
  6. 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信息如下:

[java] view plaincopy
  1. 04-09 18:17:26.410: D/MyLinearLayout(21504): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
  2. 04-09 18:17:26.410: D/MyLinearLayout(21504): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
  3. 04-09 18:17:26.414: D/MyLinearLayout(21504): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
  4. 04-09 18:17:26.414: D/MyLinearLayout(21504): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
  5. 04-09 18:17:26.418: D/MyLinearLayout(21504): MyTextView——dispatchTouchEvent action:ACTION_DOWN  
  6. 04-09 18:17:26.418: D/MyLinearLayout(21504): MyTextView——-onTouchEvent action:ACTION_DOWN  
  7. 04-09 18:17:26.418: D/MyLinearLayout(21504): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
  8.   
  9. 04-09 18:17:26.437: D/MyLinearLayout(21504): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
  10. 04-09 18:17:26.437: D/MyLinearLayout(21504): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP  
  11. 04-09 18:17:26.441: D/MyLinearLayout(21504): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP  
  12. 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信息如下:

[java] view plaincopy
  1. 04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
  2. 04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
  3. 04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
  4. 04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
  5. 04-09 13:17:30.515: D/MyLinearLayout(9935): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
  6.   
  7. 04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE  
  8. 04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_MOVE  
  9. 04-09 13:17:30.547: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_MOVE  
  10. 04-09 13:17:30.551: D/MyLinearLayout(9935): MyLinearLayout2——-onTouchEvent action:ACTION_MOVE  
  11.   
  12. 04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
  13. 04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout1——onInterceptTouchEvent action:ACTION_UP  
  14. 04-09 13:17:30.644: D/MyLinearLayout(9935): MyLinearLayout2——dispatchTouchEvent action:ACTION_UP  
  15. 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信息如下:

[java] view plaincopy
  1. 04-09 13:52:06.789: D/MyLinearLayout(12363): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
  2. 04-09 13:52:06.789: D/MyLinearLayout(12363): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
  3. 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信息如下:

[java] view plaincopy
  1. 04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
  2. 04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
  3. 04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——dispatchTouchEvent action:ACTION_DOWN  
  4. 04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——onInterceptTouchEvent action:ACTION_DOWN  
  5. 04-09 18:26:58.125: D/MyLinearLayout(22294): MyTextView——dispatchTouchEvent action:ACTION_DOWN  
  6. 04-09 18:26:58.125: D/MyLinearLayout(22294): MyTextView——-onTouchEvent action:ACTION_DOWN  
  7. 04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout2——-onTouchEvent action:ACTION_DOWN  
  8. 04-09 18:26:58.125: D/MyLinearLayout(22294): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN  
  9.   
  10. 04-09 18:26:58.215: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE  
  11. 04-09 18:26:58.215: D/MyLinearLayout(22294): MyLinearLayout1——-onTouchEvent action:ACTION_MOVE  
  12.   
  13. 04-09 18:26:58.281: D/MyLinearLayout(22294): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
  14. 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信息如下:

[java] view plaincopy
  1. 04-09 13:58:04.199: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_DOWN  
  2. 04-09 13:58:04.199: D/MyLinearLayout(13116): MyLinearLayout1——onInterceptTouchEvent action:ACTION_DOWN  
  3. 04-09 13:58:04.203: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_DOWN  
  4.   
  5. 04-09 13:58:04.324: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_MOVE  
  6. 04-09 13:58:04.328: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_MOVE  
  7.   
  8. 04-09 13:58:04.367: D/MyLinearLayout(13116): MyLinearLayout1——dispatchTouchEvent action:ACTION_UP  
  9. 04-09 13:58:04.367: D/MyLinearLayout(13116): MyLinearLayout1——-onTouchEvent action:ACTION_UP  

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


 傳遞示意圖:


文章原址:http://blog.csdn.net/hyp712/article/details/8777835#0-tsina-1-34953-397232819ff9a47a7b7e80a40613cfe1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章