Android中Button設置background過程的研究

咱們的目的是研究Button設置android:background的過程以及實現。php

首先要分兩種狀況。
第一種狀況,Button所在的Activity繼承於AppCompatActivity,這時使用Android Studio中的Layout Inspector工具解析屏幕中的控件,會發現Button被替換成了AppCompatButtonhtml

第二種狀況,Button所在的Activity繼承於Activity,這種狀況下,解析屏幕,Button沒有被替換。java

Button

那麼首先研究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

TextView

Button的構造方法最終是調用TextView的四個參數構造方法。app

public TextView( Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes){}複製代碼

通過對這個構造方法裏檢索,並無發現與background有關的代碼,那麼,真正的與background有關的代碼必定在TextView的父類View裏。工具

View

View中的四個參數構造方法中,找到了這樣的代碼:學習

int attr = a.getIndex(i);
switch (attr) {
    case com.android.internal.R.styleable.View_background:
        background = a.getDrawable(attr);
        break;
    ...
}複製代碼

追蹤的過程按下不表,這裏說一說最終的結果。ui

若是這個drawableColorDrawable的話,那麼就會實例化一個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中的靜態方法createFromResourceStreamcreateFromXml方法。

createFromResourceStream

這裏就是先實例化一個Bitmap對象,而後判斷這個Bitmap是否爲.9圖。若是是的話,則根據Bitmap生成一個NinePatchDrawable,不然,生成一個BitmapDrawable

createFromXml

這裏最終是調用了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

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屬性對應的方法:

  • setCornerRadii - 設置邊角半徑
  • setStroke - 設置邊框(寬度+顏色)
  • setSize
  • setShape - RECTANGLE | OVAL | LINE | RING
  • ...

GradientDrawable中使用了一個GradientState內部類對象保存狀態(即在xml裏設置的那些屬性)。

最後就是在GradientDrawabledraw方法中進行繪製。

好了,這就是爲Button設置一個android:background的大體過程。

想要了解更多的話,請移步本人的學習筆記,若是以爲有幫助的話,請點一個star。

相關文章
相關標籤/搜索