咱們的目的是研究Button
設置android:background
的過程以及實現。php
首先要分兩種狀況。
第一種狀況,Button
所在的Activity
繼承於AppCompatActivity
,這時使用Android Studio
中的Layout Inspector
工具解析屏幕中的控件,會發現Button
被替換成了AppCompatButton
。html
第二種狀況,Button
所在的Activity
繼承於Activity
,這種狀況下,解析屏幕,Button
沒有被替換。java
那麼首先研究Button
的源碼,Button
的源碼不多,只是實現了4個構造方法和一個getAccessibilityClassName
方法。android
而可以產生Button
與其父類(TextView
)的差別是體如今第二個構造方法裏git
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}複製代碼
這裏,將defStyleAttr
(即默認的Style)設置爲com.android.internal.R.attr.buttonStyle
,因此正是由於這個屬性,Button
才與TextView
顯示得不同。github
Button
的構造方法最終是調用TextView
的四個參數構造方法。app
public TextView( Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes){}複製代碼
通過對這個構造方法裏檢索,並無發現與background
有關的代碼,那麼,真正的與background
有關的代碼必定在TextView
的父類View
裏。工具
在View
中的四個參數構造方法中,找到了這樣的代碼:學習
int attr = a.getIndex(i);
switch (attr) {
case com.android.internal.R.styleable.View_background:
background = a.getDrawable(attr);
break;
...
}複製代碼
追蹤的過程按下不表,這裏說一說最終的結果。ui
若是這個drawable
是ColorDrawable
的話,那麼就會實例化一個ColorDrawable
對象,不然,就會調用loadDrawableForCookie
方法,即從XML
中或者resources stream
中加載drawable
。
loadDrawableForCookie
方法中的主要邏輯是這樣的:
if (file.endsWith(".xml")) {
final XmlResourceParser rp = loadXmlResourceParser(
file, id, value.assetCookie, "drawable");
dr = Drawable.createFromXml(wrapper, rp, theme);
rp.close();
} else {
final InputStream is = mAssets.openNonAsset(
value.assetCookie, file, AssetManager.ACCESS_STREAMING);
dr = Drawable.createFromResourceStream(wrapper, value, is, file, null);
is.close();
}複製代碼
那麼,接下來咱們就瞭解一下Drawable
中的靜態方法createFromResourceStream
和createFromXml
方法。
這裏就是先實例化一個Bitmap
對象,而後判斷這個Bitmap
是否爲.9
圖。若是是的話,則根據Bitmap
生成一個NinePatchDrawable
,不然,生成一個BitmapDrawable
。
這裏最終是調用了DrawableInflater.inflateFromXml
方法,這個方法返回一個drawable
,可是其實返回的是它的子類。下面這段代碼就是根據XML
中的TAG
進行斷定這個drawable
是屬於什麼類型的。
private Drawable inflateFromTag(@NonNull String name) {
switch (name) {
case "selector":
return new StateListDrawable();
case "animated-selector":
return new AnimatedStateListDrawable();
case "level-list":
return new LevelListDrawable();
case "layer-list":
return new LayerDrawable();
case "transition":
return new TransitionDrawable();
case "ripple":
return new RippleDrawable();
case "color":
return new ColorDrawable();
case "shape":
return new GradientDrawable();
case "vector":
return new VectorDrawable();
case "animated-vector":
return new AnimatedVectorDrawable();
case "scale":
return new ScaleDrawable();
case "clip":
return new ClipDrawable();
case "rotate":
return new RotateDrawable();
case "animated-rotate":
return new AnimatedRotateDrawable();
case "animation-list":
return new AnimationDrawable();
case "inset":
return new InsetDrawable();
case "bitmap":
return new BitmapDrawable();
case "nine-patch":
return new NinePatchDrawable();
default:
return null;
}
}複製代碼
到這裏,終於見到了咱們熟悉的東西了。咱們這裏就研究一下shape
所對應的GradientDrawable
。
Gradient
這個詞咱們就很熟悉了,它就是shape
中的漸變屬性嘛,以下面的代碼:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!-- 設置邊框 -->
<stroke android:width="2dp" android:color="@android:color/black" />
<!-- 設置顏色漸變 -->
<gradient android:angle="45" android:endColor="@android:color/holo_orange_dark" android:startColor="@android:color/holo_blue_light" android:type="linear" />
<corners android:radius="@dimen/activity_horizontal_margin" />
</shape>複製代碼
效果圖以下,是一個從橘黃色到藍色的漸變:
GradientDrawable
中提供了一些和xml
屬性對應的方法:
在GradientDrawable
中使用了一個GradientState
內部類對象保存狀態(即在xml
裏設置的那些屬性)。
最後就是在GradientDrawable
的draw
方法中進行繪製。
好了,這就是爲Button
設置一個android:background
的大體過程。
想要了解更多的話,請移步本人的學習筆記,若是以爲有幫助的話,請點一個star。