【Android Drawable】三、LayerDrawable、LevelListDrawable、ColorDrawable、ColorStateList

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。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 Android把任何可繪制在屏幕上的圖形圖像都稱為drawable 資源,你可以通過類似getDrawabl...
    小蕓論閱讀 2,906評論 2 5
  • 1、Drawable 簡介 Drawable——可簡單理解為可繪制物,表示一些可以繪制在 Canvas 上的對象。...
    牧秦丶閱讀 15,351評論 0 15
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,048評論 25 709
  • 一提起時(shí)光倒流,我就想起這段話來: “你好像瘦了,頭發(fā)也變長了,背影陌生到讓我覺得,見你是上個(gè)世紀(jì)的事,然后你開口...
    云時(shí)之間閱讀 863評論 2 4
  • 我都忘記這是來到山傳的第幾個(gè)夜晚,或者說也從來沒有記得過哪一個(gè)夜晚。 大一的時(shí)候每次上過晚自習(xí)都會急匆匆的從教室趕...
    wordwide閱讀 225評論 0 1

友情鏈接更多精彩內(nèi)容