欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析(下)

 更新時(shí)間:2018年05月10日 17:11:02   作者:guolin  
這篇文章主要介紹了Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析(下),具有很好的參考價(jià)值,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

記得在前面的文章中,我?guī)Т蠹乙黄饛脑创a的角度分析了Android中View的事件分發(fā)機(jī)制,相信閱讀過的朋友對(duì)View的事件分發(fā)已經(jīng)有比較深刻的理解了。

還未閱讀過的朋友,請(qǐng)先參考Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析。 

那么今天我們將繼續(xù)上次未完成的話題,從源碼的角度分析ViewGroup的事件分發(fā)。

首先我們來探討一下,什么是ViewGroup?它和普通的View有什么區(qū)別?

顧名思義,ViewGroup就是一組View的集合,它包含很多的子View和子VewGroup,是Android中所有布局的父類或間接父類,像LinearLayout、RelativeLayout等都是繼承自ViewGroup的。但ViewGroup實(shí)際上也是一個(gè)View,只不過比起View,它多了可以包含子View和定義布局參數(shù)的功能。ViewGroup繼承結(jié)構(gòu)示意圖如下所示:

 

可以看到,我們平時(shí)項(xiàng)目里經(jīng)常用到的各種布局,全都屬于ViewGroup的子類。

簡(jiǎn)單介紹完了ViewGroup,我們現(xiàn)在通過一個(gè)Demo來演示一下Android中VewGroup的事件分發(fā)流程吧。

首先我們來自定義一個(gè)布局,命名為MyLayout,繼承自LinearLayout,如下所示:

public class MyLayout extends LinearLayout { 
 public MyLayout(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 } 
} 

然后,打開主布局文件activity_main.xml,在其中加入我們自定義的布局:

<com.example.viewgrouptouchevent.MyLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:id="@+id/my_layout" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" > 
 <Button 
 android:id="@+id/button1" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:text="Button1" /> 
 <Button 
 android:id="@+id/button2" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:text="Button2" /> 
 
</com.example.viewgrouptouchevent.MyLayout> 

可以看到,我們?cè)贛yLayout中添加了兩個(gè)按鈕,接著在MainActivity中為這兩個(gè)按鈕和MyLayout都注冊(cè)了監(jiān)聽事件:

myLayout.setOnTouchListener(new OnTouchListener() { 
 @Override 
 public boolean onTouch(View v, MotionEvent event) { 
 Log.d("TAG", "myLayout on touch"); 
 return false; 
 } 
}); 
button1.setOnClickListener(new OnClickListener() { 
 @Override 
 public void onClick(View v) { 
 Log.d("TAG", "You clicked button1"); 
 } 
}); 
button2.setOnClickListener(new OnClickListener() { 
 @Override 
 public void onClick(View v) { 
 Log.d("TAG", "You clicked button2"); 
 } 
}); 

我們?cè)贛yLayout的onTouch方法,和Button1、Button2的onClick方法中都打印了一句話?,F(xiàn)在運(yùn)行一下項(xiàng)目,效果圖如下所示:

 

分別點(diǎn)擊一下Button1、Button2和空白區(qū)域,打印結(jié)果如下所示:

 

你會(huì)發(fā)現(xiàn),當(dāng)點(diǎn)擊按鈕的時(shí)候,MyLayout注冊(cè)的onTouch方法并不會(huì)執(zhí)行,只有點(diǎn)擊空白區(qū)域的時(shí)候才會(huì)執(zhí)行該方法。你可以先理解成Button的onClick方法將事件消費(fèi)掉了,因此事件不會(huì)再繼續(xù)向下傳遞。

那就說明Android中的touch事件是先傳遞到View,再傳遞到ViewGroup的?現(xiàn)在下結(jié)論還未免過早了,讓我們?cè)賮碜鲆粋€(gè)實(shí)驗(yàn)。 

查閱文檔可以看到,ViewGroup中有一個(gè)onInterceptTouchEvent方法,我們來看一下這個(gè)方法的源碼:

/** 
 * Implement this method to intercept all touch screen motion events. This 
 * allows you to watch events as they are dispatched to your children, and 
 * take ownership of the current gesture at any point. 
 * 
 * <p>Using this function takes some care, as it has a fairly complicated 
 * interaction with {@link View#onTouchEvent(MotionEvent) 
 * View.onTouchEvent(MotionEvent)}, and using it requires implementing 
 * that method as well as this one in the correct way. Events will be 
 * received in the following order: 
 * 
 * <ol> 
 * <li> You will receive the down event here. 
 * <li> The down event will be handled either by a child of this view 
 * group, or given to your own onTouchEvent() method to handle; this means 
 * you should implement onTouchEvent() to return true, so you will 
 * continue to see the rest of the gesture (instead of looking for 
 * a parent view to handle it). Also, by returning true from 
 * onTouchEvent(), you will not receive any following 
 * events in onInterceptTouchEvent() and all touch processing must 
 * happen in onTouchEvent() like normal. 
 * <li> For as long as you return false from this function, each following 
 * event (up to and including the final up) will be delivered first here 
 * and then to the target's onTouchEvent(). 
 * <li> If you return true from here, you will not receive any 
 * following events: the target view will receive the same event but 
 * with the action {@link MotionEvent#ACTION_CANCEL}, and all further 
 * events will be delivered to your onTouchEvent() method and no longer 
 * appear here. 
 * </ol> 
 * 
 * @param ev The motion event being dispatched down the hierarchy. 
 * @return Return true to steal motion events from the children and have 
 * them dispatched to this ViewGroup through onTouchEvent(). 
 * The current target will receive an ACTION_CANCEL event, and no further 
 * messages will be delivered here. 
 */ 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
 return false; 
} 

如果不看源碼你還真可能被這注釋嚇到了,這么長(zhǎng)的英文注釋看得頭都大了。可是源碼竟然如此簡(jiǎn)單!只有一行代碼,返回了一個(gè)false!

好吧,既然是布爾型的返回,那么只有兩種可能,我們?cè)贛yLayout中重寫這個(gè)方法,然后返回一個(gè)true試試,代碼如下所示:

public class MyLayout extends LinearLayout { 
 public MyLayout(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 } 
 @Override 
 public boolean onInterceptTouchEvent(MotionEvent ev) { 
 return true; 
 } 
 
} 

現(xiàn)在再次運(yùn)行項(xiàng)目,然后分別Button1、Button2和空白區(qū)域,打印結(jié)果如下所示:

 

你會(huì)發(fā)現(xiàn),不管你點(diǎn)擊哪里,永遠(yuǎn)都只會(huì)觸發(fā)MyLayout的touch事件了,按鈕的點(diǎn)擊事件完全被屏蔽掉了!這是為什么呢?如果Android中的touch事件是先傳遞到View,再傳遞到ViewGroup的,那么MyLayout又怎么可能屏蔽掉Button的點(diǎn)擊事件呢? 

看來只有通過閱讀源碼,搞清楚Android中ViewGroup的事件分發(fā)機(jī)制,才能解決我們心中的疑惑了,不過這里我想先跟你透露一句,Android中touch事件的傳遞,絕對(duì)是先傳遞到ViewGroup,再傳遞到View的。記得在Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析 中我有說明過,只要你觸摸了任何控件,就一定會(huì)調(diào)用該控件的dispatchTouchEvent方法。這個(gè)說法沒錯(cuò),只不過還不完整而已。實(shí)際情況是,當(dāng)你點(diǎn)擊了某個(gè)控件,首先會(huì)去調(diào)用該控件所在布局的dispatchTouchEvent方法,然后在布局的dispatchTouchEvent方法中找到被點(diǎn)擊的相應(yīng)控件,再去調(diào)用該控件的dispatchTouchEvent方法。如果我們點(diǎn)擊了MyLayout中的按鈕,會(huì)先去調(diào)用MyLayout的dispatchTouchEvent方法,可是你會(huì)發(fā)現(xiàn)MyLayout中并沒有這個(gè)方法。那就再到它的父類LinearLayout中找一找,發(fā)現(xiàn)也沒有這個(gè)方法。那只好繼續(xù)再找LinearLayout的父類ViewGroup,你終于在ViewGroup中看到了這個(gè)方法,按鈕的dispatchTouchEvent方法就是在這里調(diào)用的。修改后的示意圖如下所示:

  

那還等什么?快去看一看ViewGroup中的dispatchTouchEvent方法的源碼吧!代碼如下所示:

public boolean dispatchTouchEvent(MotionEvent ev) { 
 final int action = ev.getAction(); 
 final float xf = ev.getX(); 
 final float yf = ev.getY(); 
 final float scrolledXFloat = xf + mScrollX; 
 final float scrolledYFloat = yf + mScrollY; 
 final Rect frame = mTempRect; 
 boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; 
 if (action == MotionEvent.ACTION_DOWN) { 
 if (mMotionTarget != null) { 
  mMotionTarget = null; 
 } 
 if (disallowIntercept || !onInterceptTouchEvent(ev)) { 
  ev.setAction(MotionEvent.ACTION_DOWN); 
  final int scrolledXInt = (int) scrolledXFloat; 
  final int scrolledYInt = (int) scrolledYFloat; 
  final View[] children = mChildren; 
  final int count = mChildrenCount; 
  for (int i = count - 1; i >= 0; i--) { 
  final View child = children[i]; 
  if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE 
   || child.getAnimation() != null) { 
   child.getHitRect(frame); 
   if (frame.contains(scrolledXInt, scrolledYInt)) { 
   final float xc = scrolledXFloat - child.mLeft; 
   final float yc = scrolledYFloat - child.mTop; 
   ev.setLocation(xc, yc); 
   child.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; 
   if (child.dispatchTouchEvent(ev)) { 
    mMotionTarget = child; 
    return true; 
   } 
   } 
  } 
  } 
 } 
 } 
 boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) || 
  (action == MotionEvent.ACTION_CANCEL); 
 if (isUpOrCancel) { 
 mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; 
 } 
 final View target = mMotionTarget; 
 if (target == null) { 
 ev.setLocation(xf, yf); 
 if ((mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { 
  ev.setAction(MotionEvent.ACTION_CANCEL); 
  mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; 
 } 
 return super.dispatchTouchEvent(ev); 
 } 
 if (!disallowIntercept && onInterceptTouchEvent(ev)) { 
 final float xc = scrolledXFloat - (float) target.mLeft; 
 final float yc = scrolledYFloat - (float) target.mTop; 
 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; 
 ev.setAction(MotionEvent.ACTION_CANCEL); 
 ev.setLocation(xc, yc); 
 if (!target.dispatchTouchEvent(ev)) { 
 } 
 mMotionTarget = null; 
 return true; 
 } 
 if (isUpOrCancel) { 
 mMotionTarget = null; 
 } 
 final float xc = scrolledXFloat - (float) target.mLeft; 
 final float yc = scrolledYFloat - (float) target.mTop; 
 ev.setLocation(xc, yc); 
 if ((target.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { 
 ev.setAction(MotionEvent.ACTION_CANCEL); 
 target.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; 
 mMotionTarget = null; 
 } 
 return target.dispatchTouchEvent(ev); 
} 

這個(gè)方法代碼比較長(zhǎng),我們只挑重點(diǎn)看。首先在第13行可以看到一個(gè)條件判斷,如果disallowIntercept和!onInterceptTouchEvent(ev)兩者有一個(gè)為true,就會(huì)進(jìn)入到這個(gè)條件判斷中。disallowIntercept是指是否禁用掉事件攔截的功能,默認(rèn)是false,也可以通過調(diào)用requestDisallowInterceptTouchEvent方法對(duì)這個(gè)值進(jìn)行修改。那么當(dāng)?shù)谝粋€(gè)值為false的時(shí)候就會(huì)完全依賴第二個(gè)值來決定是否可以進(jìn)入到條件判斷的內(nèi)部,第二個(gè)值是什么呢?竟然就是對(duì)onInterceptTouchEvent方法的返回值取反!也就是說如果我們?cè)趏nInterceptTouchEvent方法中返回false,就會(huì)讓第二個(gè)值為true,從而進(jìn)入到條件判斷的內(nèi)部,如果我們?cè)趏nInterceptTouchEvent方法中返回true,就會(huì)讓第二個(gè)值為false,從而跳出了這個(gè)條件判斷。

這個(gè)時(shí)候你就可以思考一下了,由于我們剛剛在MyLayout中重寫了onInterceptTouchEvent方法,讓這個(gè)方法返回true,導(dǎo)致所有按鈕的點(diǎn)擊事件都被屏蔽了,那我們就完全有理由相信,按鈕點(diǎn)擊事件的處理就是在第13行條件判斷的內(nèi)部進(jìn)行的!

那我們重點(diǎn)來看下條件判斷的內(nèi)部是怎么實(shí)現(xiàn)的。在第19行通過一個(gè)for循環(huán),遍歷了當(dāng)前ViewGroup下的所有子View,然后在第24行判斷當(dāng)前遍歷的View是不是正在點(diǎn)擊的View,如果是的話就會(huì)進(jìn)入到該條件判斷的內(nèi)部,然后在第29行調(diào)用了該View的dispatchTouchEvent,之后的流程就和 Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析中講解的是一樣的了。我們也因此證實(shí)了,按鈕點(diǎn)擊事件的處理確實(shí)就是在這里進(jìn)行的。

然后需要注意一下,調(diào)用子View的dispatchTouchEvent后是有返回值的。我們已經(jīng)知道,如果一個(gè)控件是可點(diǎn)擊的,那么點(diǎn)擊該控件時(shí),dispatchTouchEvent的返回值必定是true。因此會(huì)導(dǎo)致第29行的條件判斷成立,于是在第31行給ViewGroup的dispatchTouchEvent方法直接返回了true。這樣就導(dǎo)致后面的代碼無法執(zhí)行到了,也是印證了我們前面的Demo打印的結(jié)果,如果按鈕的點(diǎn)擊事件得到執(zhí)行,就會(huì)把MyLayout的touch事件攔截掉。

那如果我們點(diǎn)擊的不是按鈕,而是空白區(qū)域呢?這種情況就一定不會(huì)在第31行返回true了,而是會(huì)繼續(xù)執(zhí)行后面的代碼。那我們繼續(xù)往后看,在第44行,如果target等于null,就會(huì)進(jìn)入到該條件判斷內(nèi)部,這里一般情況下target都會(huì)是null,因此會(huì)在第50行調(diào)用super.dispatchTouchEvent(ev)。這句代碼會(huì)調(diào)用到哪里呢?當(dāng)然是View中的dispatchTouchEvent方法了,因?yàn)閂iewGroup的父類就是View。之后的處理邏輯又和前面所說的是一樣的了,也因此MyLayout中注冊(cè)的onTouch方法會(huì)得到執(zhí)行。之后的代碼在一般情況下是走不到的了,我們也就不再繼續(xù)往下分析。

再看一下整個(gè)ViewGroup事件分發(fā)過程的流程圖吧,相信可以幫助大家更好地去理解:

 

現(xiàn)在整個(gè)ViewGroup的事件分發(fā)流程的分析也就到此結(jié)束了,我們最后再來簡(jiǎn)單梳理一下吧。 

1. Android事件分發(fā)是先傳遞到ViewGroup,再由ViewGroup傳遞到View的。

2. 在ViewGroup中可以通過onInterceptTouchEvent方法對(duì)事件傳遞進(jìn)行攔截,onInterceptTouchEvent方法返回true代表不允許事件繼續(xù)向子View傳遞,返回false代表不對(duì)事件進(jìn)行攔截,默認(rèn)返回false。

3. 子View中如果將傳遞的事件消費(fèi)掉,ViewGroup中將無法接收到任何事件。 

好了,Android事件分發(fā)機(jī)制完全解析到此全部結(jié)束,結(jié)合上下兩篇,相信大家對(duì)事件分發(fā)的理解已經(jīng)非常深刻了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android編程之光線傳感器用法詳解

    Android編程之光線傳感器用法詳解

    這篇文章主要介紹了Android編程之光線傳感器用法,結(jié)合實(shí)例形式分析了Android光線傳感器的功能、實(shí)現(xiàn)步驟與相關(guān)注意事項(xiàng),并給出了相關(guān)demo示例,需要的朋友可以參考下
    2017-11-11
  • Android編程之Activity中onDestroy()調(diào)用分析

    Android編程之Activity中onDestroy()調(diào)用分析

    這篇文章主要介紹了Android編程之Activity中onDestroy()調(diào)用方法,針對(duì)onDestroy引起的內(nèi)存泄露及解決方法進(jìn)行了分析,并給出了解決方案,需要的朋友可以參考下
    2015-12-12
  • android 字體顏色選擇器(ColorPicker)介紹

    android 字體顏色選擇器(ColorPicker)介紹

    本文將詳細(xì)介紹android 字體顏色選擇器(ColorPicker)需要了解更多的朋友可以參考下
    2012-11-11
  • Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果(7)

    Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果(7)

    這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android 混合動(dòng)畫詳解及實(shí)現(xiàn)代碼

    Android 混合動(dòng)畫詳解及實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android 混合動(dòng)畫詳解及實(shí)現(xiàn)代碼的相關(guān)資料,簡(jiǎn)單的一種動(dòng)畫(如旋轉(zhuǎn)、縮放、漸變、位移等)有時(shí)候并不能滿足我們項(xiàng)目的要求,這時(shí)候就需要運(yùn)用到混合動(dòng)畫,需要的朋友可以參考下
    2016-11-11
  • Android隱藏手機(jī)底部虛擬按鍵的方法

    Android隱藏手機(jī)底部虛擬按鍵的方法

    這篇文章主要為大家詳細(xì)介紹了Android隱藏手機(jī)底部虛擬按鍵的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android Fragment源碼分析Add方法

    Android Fragment源碼分析Add方法

    Fragment是Android3.0后引入的一個(gè)新的API,他出現(xiàn)的初衷是為了適應(yīng)大屏幕的平板電腦, 當(dāng)然現(xiàn)在他仍然是平板APP UI設(shè)計(jì)的寵兒,而且我們普通手機(jī)開發(fā)也會(huì)加入這個(gè)Fragment, 我們可以把他看成一個(gè)小型的Activity,又稱Activity片段
    2022-08-08
  • Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(七)

    Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(七)

    這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的第七篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • android實(shí)現(xiàn)可自由移動(dòng)、監(jiān)聽點(diǎn)擊事件的懸浮窗

    android實(shí)現(xiàn)可自由移動(dòng)、監(jiān)聽點(diǎn)擊事件的懸浮窗

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可自由移動(dòng)、監(jiān)聽點(diǎn)擊事件的懸浮窗,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android WebView開發(fā)之WebView與Native交互

    Android WebView開發(fā)之WebView與Native交互

    隨著H5的廣泛使用,Android開發(fā)過程中免不了會(huì)使用網(wǎng)頁來做展示,那么web與native之間的通信就顯得尤其重要了,其實(shí)際上是JavaScript與java之間的通信。本文將為大家詳細(xì)介紹二者是如何實(shí)現(xiàn)交互的,需要的朋友可以參考一下
    2021-12-12

最新評(píng)論