《Android 群英傳》——徐宜生 讀書筆記
Android 控件架構(gòu)
通常雳攘,在Activity中使用setContentView() 方法來(lái)設(shè)置一個(gè)布局,在調(diào)用該方法后温学,布局內(nèi)容才真正顯示出來(lái)剃诅。首先我們來(lái)看一下Android界面的架構(gòu)圖:每個(gè)Activity都包含一個(gè)Window對(duì)象,在Android中Window對(duì)象通常由PhoneWindow來(lái)實(shí)現(xiàn)醉拓,PhoneWindow將一個(gè)DecorView作為整個(gè)應(yīng)用窗口的根View墩瞳,即窗口界面的頂級(jí)視圖驼壶。DecorView將整個(gè)界面分為兩部分,頂部的ActionBar和主體的ContentView喉酌,而setContentView()方法就是為ContentView部分加載布局的热凹。并且,我們平時(shí)編寫的界面基本都是在ContentView中定義的泪电。下面我們可以來(lái)驗(yàn)證一下:
LinearLayout linearLayout = findViewById(R.id.linear_layout);
ViewGroup content = (ViewGroup) linearLayout.getParent();
Log.d("jh", content.toString());
ViewGroup viewGroup = (ViewGroup)content.getParent();
Log.d("jh", viewGroup.toString());
ViewGroup decorView = (ViewGroup)viewGroup.getParent();
Log.d("jh", decorView.toString());
-
先打印出來(lái)看一下:
這樣還不夠清晰般妙,我們?cè)賮?lái)從DecorView向下打印來(lái)看看:
Log.d("jh", decorView.toString());
Log.d("jh", String.valueOf(decorView.getChildCount()));
Log.d("jh", viewGroup.toString());
Log.d("jh", String.valueOf( viewGroup.getChildCount()));
Log.d("jh", viewGroup.getChildAt(0).toString());
Log.d("jh", viewGroup.getChildAt(1).toString());
Log.d("jh", String.valueOf(content.getChildCount()));
Log.d("jh", content.getChildAt(0).toString());
可以看到,DecorView實(shí)際上是一個(gè)FrameLayout歪架,它有一個(gè)子View股冗,是一個(gè)FitWindowsLinearLayout,這個(gè)LinearLayout有兩個(gè)子View和蚪,也就是我們的 ActionBar(加了一個(gè)ViewStub嵌套止状,因?yàn)锳ctionBar可以設(shè)置顯示與否) 和 ContentView(id 是 android.R.id.content 烹棉,實(shí)際也是一個(gè)FrameLayout),而ContentView 的子布局就是我們平時(shí)編寫界面的最外層布局怯疤,也就是說(shuō)浆洗,其實(shí)setContentView方法就是將我們的 activity_main.xml 動(dòng)態(tài)加載到這個(gè)ContentView里面的。雖然繞了一大圈集峦,但是我們對(duì)我們所能看到的UI界面的架構(gòu)也了解的清晰了很多伏社。
View的測(cè)量
Android在繪制一個(gè)View前,必須先對(duì)View進(jìn)行測(cè)量塔淤,確定View的寬高摘昌,這個(gè)過程在onMeasure()方法中進(jìn)行。Android系統(tǒng)提供了一個(gè)MeasureSpec類高蜂,來(lái)幫助我們測(cè)量View聪黎。MeasureSpec定義了一個(gè)32位的Int值,其中高2位表示測(cè)量模式备恤,低30位表示測(cè)量值的大小稿饰。
-
測(cè)量模式有三種:
- EXACTLY:即精確值模式,當(dāng)我們指定控件的 layout_width 或 layout_height 屬性為具體數(shù)值或match_parent(實(shí)際也是準(zhǔn)確值)時(shí)露泊,系統(tǒng)會(huì)使用 EXACTLY 模式喉镰。
- AT_MOST:即最大值模式,當(dāng)控件的 layout_width 或 layout_height 指定為wrap_content 時(shí)惭笑,View的大小是根據(jù)View的內(nèi)容寬高來(lái)確定的侣姆,并且受制于父View的寬高,這時(shí)系統(tǒng)會(huì)使用AT_MOST模式脖咐。
- UNSPECIFIED:這種模式下铺敌,不限定View的寬高,想多大就多大屁擅,一般在自定義View時(shí)使用。
View類默認(rèn)的onMeasure()方法只支持EXACTLY模式产弹,因此派歌,當(dāng)我們自定義View時(shí),如果想為控件指定wrap_content屬性痰哨,就必須重寫onMeasure()方法胶果,來(lái)對(duì)其進(jìn)行處理。下面斤斧,我們來(lái)繪制一個(gè)簡(jiǎn)單的矩形早抠,來(lái)觀察一下這三種測(cè)量模式。
-
簡(jiǎn)單實(shí)例
- 首先我們要新建一個(gè)RectangleView類撬讽,繼承View類蕊连。
public class RectangleView extends View { public RectangleView(Context context, AttributeSet attr) { super(context, attr); } }
- 我們查看View類的 onMeasure() 方法悬垃,會(huì)發(fā)現(xiàn)它其實(shí)是調(diào)用了setMeasuredDimension() 方法將測(cè)量所得的寬高傳遞進(jìn)去,從而完成測(cè)量工作甘苍。因此我們需要做的就是對(duì)這個(gè)方法傳遞的參數(shù)進(jìn)行處理尝蠕,從而定制我們的矩形寬高。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); }
onMeasure()方法的兩個(gè)參數(shù)就是系統(tǒng)確定的當(dāng)前View的寬高载庭。
- 我們定義一個(gè)measureWidth() 和 一個(gè)measureHeight() 方法看彼,用來(lái)控制View的寬高(下面以measureWidth為例)。
private int measureWidth(int measureSpec) { int result; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; }else { result = 200; if (specMode == MeasureSpec.AT_MOST){ result = Math.min(result, specSize); } } return result; }
這里傳入的參數(shù)就是onMeasure()方法指定的寬高囚聚,我們首先從這個(gè)MeasureSpec對(duì)象中提取出具體的測(cè)量模式和大小靖榕。如果測(cè)量模式是EXACTLY模式,就表明這里的寬高就是我們通過layout_width屬性指定的精確寬高顽铸,那么就直接返回這個(gè)值序矩;如果測(cè)量模式是另外兩種,我們可以給這個(gè)View指定一個(gè)默認(rèn)的大邪掀啤(單位是px)簸淀,當(dāng)測(cè)量模式是UNSPECIFIED模式,矩形的寬就是這個(gè)默認(rèn)值毒返,如果是AT_MOST模式(也就意味著我們指定的layout_width是wrap_content)租幕,我們還需要取我們指定的默認(rèn)大小和系統(tǒng)確定的寬度(一般就是父View的寬)中的較小值,因?yàn)檫@種模式下拧簸,View的寬高不能大于父View的寬高劲绪。最后就是將處理好的寬度值返回。
- 在onMeasure()方法中調(diào)用 setMeasuredDimension() 方法盆赤。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); }
下面來(lái)看看我們自定義的RectangleView是什么效果:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <com.laughter.view.views.RectangleView android:layout_width="300px" android:layout_height="300px" android:background="@color/colorPrimary"/> <com.laughter.view.views.RectangleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@color/colorPrimary"/> <com.laughter.view.views.RectangleView android:layout_width="200px" android:layout_height="300px" android:layout_marginTop="20dp" android:background="@color/colorPrimary"/> </LinearLayout>
可以看出贾富,當(dāng)我們?yōu)镽ectngleView指定具體寬高時(shí),寬高就是指定值牺六;當(dāng)我們指定為wrap_content時(shí)颤枪,寬高就是默認(rèn)大小(200px)淑际,當(dāng)然畏纲,如果父View的寬高小于200px,這里的寬高就會(huì)適應(yīng)父View(不能大于父View的寬高)春缕。還有一點(diǎn)需要注意:前面說(shuō)過盗胀,View類的onMeasure方法只支持EXACTLY模式,因此锄贼,如果我們不重寫onMeasure()方法票灰,也就意味著不處理AT_MOST模式,當(dāng)我們指定wrap_content時(shí),View的寬高會(huì)匹配父View(即和match_parent效果相同)屑迂。感興趣可以自行測(cè)試浸策。
關(guān)于View繪制的部分,由于書上沒有詳細(xì)講到關(guān)于Canvas的內(nèi)容屈糊,這里推薦一個(gè)系列教程的榛,不過建議先理解清楚上面的內(nèi)容,再去看的話會(huì)很輕松:
Android自定義View系列教程
這個(gè)系列博客內(nèi)容十分詳實(shí)逻锐,由淺入深夫晌,很適合初學(xué)者學(xué)習(xí)。