Android Touch 事件的分發和消費機制

2021-07-23 15:24:02 字數 3931 閱讀 6863

android 中與 touch 事件相關的方法包括:dispatchtouchevent(motionevent ev)、onintercepttouchevent(motionevent ev)、ontouchevent(motionevent ev);能夠響應這些方法的控制項包括:viewgroup 及其子類、activity。方法與控制項的對應關係如下表所示:

touch 事件相關方法

方法功能  

viewgroup   

activity     

public boolean dispatchtouchevent(motionevent ev)

事件分發 

yesyes

public boolean onintercepttouchevent(motionevent ev)  

事件攔截 

yesno

public boolean ontouchevent(motionevent ev)

事件響應 

yesyes

用乙個例項來說明。簡單實現乙個小功能:當觸控藍色區域時,紅色區域及最外成區域不響應,只有藍色區域響應。當觸控紅色區域時,只有紅色區域響應,其它不響應。

父子view關係如下:

最外層 ---> 紅色區域 --->藍色區域

onintercepttouchevent():如果返回值為true,則阻止向下傳播touch事件。(是向下方向,這裡是 :最外層 ---> 紅色區域 --->藍色區域 )。

ontouchevent():如果返回值為true,則阻止向上傳播touch事件。(是向上方向,這裡是 :最外層

事件方法的執行順序:

dispatchtouchevent --->onintercepttouchevent--->gettouchaction

*****==toucheventactivity.dispatchtouchevent:action_down

附上觸控藍色區域的log

09-23 12:03:54.299 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventfather.dispatchtouchevent:action_down

09-23 12:03:54.299 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventfather.onintercepttouchevent:action_down

09-23 12:03:54.299 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventchild.dispatchtouchevent:action_down

09-23 12:03:54.299 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventchild.onintercepttouchevent:action_down

09-23 12:03:54.300 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventchild.ontouchevent:action_down

09-23 12:03:54.353 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventactivity.dispatchtouchevent:action_up

09-23 12:03:54.353 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventfather.dispatchtouchevent:action_up

09-23 12:03:54.353 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventfather.onintercepttouchevent:action_up

09-23 12:03:54.354 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventchild.dispatchtouchevent:action_up

09-23 12:03:54.354 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventchild.ontouchevent:action_up

附上觸控紅色區域的log 

09-23 12:05:42.491 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventactivity.dispatchtouchevent:action_down

09-23 12:05:42.491 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventfather.dispatchtouchevent:action_down

09-23 12:05:42.492 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventfather.onintercepttouchevent:action_down

09-23 12:05:42.492 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventfather.ontouchevent:action_down

09-23 12:05:42.492 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventactivity.ontouchevent:action_down

09-23 12:05:42.545 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventactivity.dispatchtouchevent:action_up

09-23 12:05:42.545 15091-15091/com.zhuticity.eebbk.zhuticity d/liudezu_tag: *****==toucheventactivity.ontouchevent:action_up

@override

public boolean dispatchtouchevent(motionevent event)

@override

public boolean ontouchevent(motionevent event)

@override

public boolean dispatchtouchevent(motionevent event)

@override

public boolean onintercepttouchevent(motionevent event)

@override

public boolean ontouchevent(motionevent event)

@override

public boolean dispatchtouchevent(motionevent event)

@override

public boolean onintercepttouchevent(motionevent event)

@override

public boolean ontouchevent(motionevent event)

Android touch事件傳遞

android的touch事件分發機制,涉及到每一層級的處理和傳遞,比較複雜,本文是在參考以下日誌和android原始碼的基礎上總結的,在此表示感謝 1.touch事件傳遞過程 touch事件經過android核心層的處理,最終會傳遞到activity的dispatchtouchevent方法,由此...

android touch事件解析

android touch事件 乙個簡單的觸控螢幕所經歷的事件 action down action move action move action move.action move action up,即乙個acitondown,多個actionmove,乙個actionup android每個g...

android Touch事件流程

當乙個事件來臨的時候,會先傳遞給最外層的viewgroup 父view,比如linearlayout,framelayout 如果這個viewgroup沒有去攔截這個事件的話,才會給傳遞給下層的viewgroup或者view。如果被攔截掉的話,它會自己去處理這個事件,這個viewgroup內的子vi...