LayerDrawable
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id=""
android:drawable=""
android:gravity=""
android:left=""
android:top=""
android:right=""
android:bottom=""
android:width=""
android:height=""
android:start=""
android:end="">
</item>
</layer-list>
layer-list 的每個(gè) item 也是通過 android:drawable 屬性或者在 <item> 標(biāo)簽內(nèi)定義 drawable 進(jìn)行 Drawable 的引用,每個(gè) item 有 top、left、bottom、right 等屬性表示圖層相對于 View 上下左右的偏移量,單位為像素;
每一個(gè) Drawable 圖層都會被縮放至 View 大小,bitmap 需要通過 gravity 來控制圖片的顯示效果;
下面的 item 會覆蓋上面的 item;
LayerDrawable 的構(gòu)造方法:
public LayerDrawable(@NonNull Drawable[] layers)
LayerDrawable(@NonNull Drawable[] layers, @Nullable LayerState state)
LayerDrawable()
LayerDrawable(@Nullable LayerState state, @Nullable Resources res)
在外部可以調(diào)用的就是第一個(gè)傳入 Drawable 數(shù)組的構(gòu)造方法,把所以的圖層
放在 Drawable 數(shù)組里面?zhèn)鬟M(jìn)去
也對外提供了一系列操作 Drawable 的方法,通過 id 或 index 對 Layer 進(jìn)行設(shè)置:
public int addLayer(Drawable dr)
public Drawable findDrawableByLayerId(int id)
public void setId(int index, int id)
public int getId(int index)
public int getNumberOfLayers()
public boolean setDrawableByLayerId(int id, Drawable drawable)
public int findIndexByLayerId(int id)
public void setDrawable(int index, Drawable drawable)
public Drawable getDrawable(int index)
public void setLayerSize(int index, int w, int h)
public void setLayerWidth(int index, int w)
public void setLayerHeight(int index, int h)
public void setLayerGravity(int index, int gravity)
public void setLayerInset(int index, int l, int t, int r, int b)
public void setLayerInsetRelative(int index, int s, int t, int e, int b)
public void setLayerInsetLeft(int index, int l)
public void setLayerInsetRight(int index, int r)
public void setLayerInsetTop(int index, int t)
public void setLayerInsetBottom(int index, int b)
public void setLayerInsetStart(int index, int s)
public void setLayerInsetEnd(int index, int e)
public void setPaddingMode(int mode)
public void setPadding(int left, int top, int right, int bottom)
LevelListDrawable
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="" android:minLevel="" android:maxLevel=""/>
</level-list>
LevelListDrawable 的 Drawable 引用和 LayerDrawable 類似,每一個(gè) item 都
都有一個(gè) minLevel 和 maxLevel。
通過 Drawable#setLevel 方法設(shè)置 level ,根據(jù) level 的取值顯示不同的 Drawable。
LevelListDrawable 外部可以調(diào)用的構(gòu)造方法只有一個(gè)無參的構(gòu)造方法
public LevelListDrawable()
private LevelListDrawable(LevelListState state, Resources res)
內(nèi)部調(diào)用一個(gè)兩個(gè)參數(shù)的構(gòu)造方法,默認(rèn)兩個(gè)參數(shù)都為 null,其中一個(gè)參數(shù)
為 ConstantState 的子類 LevelListState 對象。
對外提供了添加 Drawable 的方法:
public void addLevel(int low, int high, Drawable drawable) {
if (drawable != null) {
mLevelListState.addLevel(low, high, drawable);
// in case the new state matches our current state...
onLevelChange(getLevel());
}
}
ColorDrawable & ColorStateList
ColorDrawable 很簡單,標(biāo)簽為 <color> 的 xml 文件,只有一個(gè)屬性:
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="">
</color>
public 構(gòu)造方法有兩個(gè),一個(gè)參數(shù)為空,一個(gè)參數(shù)是一個(gè) int 類型的 color,可以是十六進(jìn)制數(shù)值,也可以是 res/values/colors.xml 中定義的顏色,當(dāng)然也可以是系統(tǒng)資源中定義的顏色,還可以是 Color 類生成的 Color:
public ColorDrawable()
public ColorDrawable(@ColorInt int color)
private ColorDrawable(ColorState state, Resources res)
對外提供了設(shè)置顏色的方法:
public void setColor(@ColorInt int color) {
if (mColorState.mBaseColor != color || mColorState.mUseColor != color) {
mColorState.mBaseColor = mColorState.mUseColor = color;
invalidateSelf();
}
}
此外還有一個(gè) ColorStateList 類,是定義在 res/color 目錄里的 xml 文件生成的對象,該 xml 文件的根標(biāo)簽是 <selector>,每一個(gè) item 都有一個(gè) android:color 屬性,例如
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="@color/testcolor1"/>
<item android:state_pressed="true" android:state_enabled="false" android:color="@color/testcolor2" />
<item android:state_enabled="false" android:color="@color/testcolor3" />
<item android:color="@color/testcolor5"/>
</selector>
public 的構(gòu)造方法:
public ColorStateList(int[][] states, @ColorInt int[] colors)
參數(shù)就是兩個(gè)數(shù)組,分別存儲 state 和對應(yīng)的 color。