【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 的每个 item 也是通过 android:drawable 属性或者在 <item> 标签内定义 drawable 进行 Drawable 的引用,每个 item 有 top、left、bottom、right 等属性表示图层相对于 View 上下左右的偏移量,单位为像素;
每一个 Drawable 图层都会被缩放至 View 大小,bitmap 需要通过 gravity 来控制图片的显示效果;
下面的 item 会覆盖上面的 item;
LayerDrawable 的构造方法:

public LayerDrawable(@NonNull Drawable[] layers)
LayerDrawable(@NonNull Drawable[] layers, @Nullable LayerState state)
LayerDrawable() 
LayerDrawable(@Nullable LayerState state, @Nullable Resources res)

在外部可以调用的就是第一个传入 Drawable 数组的构造方法,把所以的图层
放在 Drawable 数组里面传进去
也对外提供了一系列操作 Drawable 的方法,通过 id 或 index 对 Layer 进行设置:

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 类似,每一个 item 都
都有一个 minLevel 和 maxLevel。
通过 Drawable#setLevel 方法设置 level ,根据 level 的取值显示不同的 Drawable。
LevelListDrawable 外部可以调用的构造方法只有一个无参的构造方法

public LevelListDrawable()
private LevelListDrawable(LevelListState state, Resources res)

内部调用一个两个参数的构造方法,默认两个参数都为 null,其中一个参数
为 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 很简单,标签为 <color> 的 xml 文件,只有一个属性:

<color xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="">
</color>

public 构造方法有两个,一个参数为空,一个参数是一个 int 类型的 color,可以是十六进制数值,也可以是 res/values/colors.xml 中定义的颜色,当然也可以是系统资源中定义的颜色,还可以是 Color 类生成的 Color:

public ColorDrawable()
public ColorDrawable(@ColorInt int color)
private ColorDrawable(ColorState state, Resources res)

对外提供了设置颜色的方法:

public void setColor(@ColorInt int color) {
        if (mColorState.mBaseColor != color || mColorState.mUseColor != color) {
            mColorState.mBaseColor = mColorState.mUseColor = color;
            invalidateSelf();
        }
    }

此外还有一个 ColorStateList 类,是定义在 res/color 目录里的 xml 文件生成的对象,该 xml 文件的根标签是 <selector>,每一个 item 都有一个 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 的构造方法:

public ColorStateList(int[][] states, @ColorInt int[] colors)

参数就是两个数组,分别存储 state 和对应的 color。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 Android把任何可绘制在屏幕上的图形图像都称为drawable 资源,你可以通过类似getDrawabl...
    小芸论阅读 7,734评论 2 5
  • 1、Drawable 简介 Drawable——可简单理解为可绘制物,表示一些可以绘制在 Canvas 上的对象。...
    牧秦丶阅读 15,202评论 0 15
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,335评论 25 709
  • 一提起时光倒流,我就想起这段话来: “你好像瘦了,头发也变长了,背影陌生到让我觉得,见你是上个世纪的事,然后你开口...
    云时之间阅读 4,035评论 2 4
  • 我都忘记这是来到山传的第几个夜晚,或者说也从来没有记得过哪一个夜晚。 大一的时候每次上过晚自习都会急匆匆的从教室赶...
    wordwide阅读 1,248评论 0 1