美文网首页基础Android CommunityAndroid技术知识
【Android Drawable】三、LayerDrawabl

【Android Drawable】三、LayerDrawabl

作者: 秀花123 | 来源:发表于2017-04-29 10:39 被阅读399次

    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】三、LayerDrawabl

        本文链接:https://www.haomeiwen.com/subject/flmwzttx.html