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
<?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信息如下:
[java]?view plaincopy
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信息如下:
[java]?view plaincopy
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信息如下:
[java]?view plaincopy
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信息如下:
[java]?view plaincopy
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信息如下:
[java]?view plaincopy
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信息如下:
[java]?view plaincopy
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信息如下:
[java]?view plaincopy
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信息如下:
[java]?view plaincopy
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#0-tsina-1-34953-397232819ff9a47a7b7e80a40613cfe1
總結
以上是生活随笔為你收集整理的Android onTouch事件传递机制的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。