美文网首页
Android——Drawable的常用使用方法

Android——Drawable的常用使用方法

作者: 四喜汤圆 | 来源:发表于2020-03-07 15:00 被阅读0次

学习笔记 drawable使用方法

一、概念

1. Android 资源分类

Android 应用下除了 res 目录用于存放资源之外,assets 目录也用于存放资源。一般来说,assets 目录下存放的资源代表应用无法直接访问的原生资源,应用程序需要通过 AssetManager 以二进制流的形式来读取资源。而res 目录下的资源,Android SDK 会在编译该应用时,自动在 R.java 文件中为这些资源创建索引,程序可直接通过 R 资源清单类访问。

  • 无法直接访问的原生资源:保存在 asset 目录下
  • 可通过 R 资源清单类访问的资源,保存在 res 目录下

2. Drawable 资源

.png、.jpg、xml文件都可被系统编译成 Drawable 类对象


二、使用

1.StateListDrawable(<selector>)

可包含一个 Drawable 数组,让目标组件在不同状态显示不同 Drawable。SateListDrawable 显示的 Drawable 会随目标组件的状态改变而自动切换。

对应的 xml 文件的根节点 <selector>

2.LayerDrawable(<layer-list>)

可包含一个 Drawable 数组,系统会按这些 Drawable 对象的数组顺序来绘制它们,索引最大的 Drawable 对象将会被绘制在最上面。类似于布局中的 FrameLayout 的效果。

对应的 xml 文件的根元素为<layer-list>,该元素可包含多个<item>元素。

【实现一个带灰色背景的 Logo】

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@android:color/darker_gray" />
        </shape>
    </item>
    <item
        android:gravity="center">
        <bitmap
            android:gravity="center"
            android:src="@drawable/iot_loading" />
    </item>
</layer-list>

3.ShapeDrawable(<shape>)

【带边框线圆角背景图】

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <solid android:color="@color/colorAccent" />
    <corners android:radius="14dp" />
    <stroke
        android:width="2dp"
        android:color="@android:color/white" />
</shape>

【带单边框背景图】

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#e1e1e1"/>
        </shape>
    </item>

    <!--bottom:距离container底部100px-->
    <item android:bottom="100px">
        <shape>
            <solid android:color="@android:color/white" />
        </shape>
    </item>

</layer-list>

4.BitmapDrawable

表示一张图片,与直接引用原始图片相比可以设置一些效果

5.RotateDrawable

可以在当前的基础上旋转任意一个 Drawable。

https://www.jianshu.com/p/0e0de2cdd2bb

【提示气泡效果】

6.LevelListDrawable

A resource that manages a number of alternate Drawables, each assigned a maximum numerical value. Setting the level value of the object with `[Drawable.setLevel(int)](https://developer.android.com/reference/android/graphics/drawable/Drawable#setLevel(int))` will load the image with the next greater or equal value assigned to its max attribute. A good example use of a LevelListDrawable would be a battery level indicator icon, with different images to indicate the current battery level.

可包含一组 Drawable,根据 level 来决定显示哪个 Drawable,为每一个 Drawable 分配一个level值(maximum numerical value),

通过Drawable.setLevel(int value)设置 Drawable 的 level 值,以决定加载哪一个 Drawable,选择和 设置的 level 最接近的那个 Drawable。

如果想要一个 ImageView 上,根据不同条件显示不同的 Drawable,可以使用<level-list>。例如:实现一个开关的灯泡,两种状态的图片,一个表开灯、一个关灯。

LevelListDrawable的基本使用

带层级的 Drawable 组合。

(1)将两个状态的图片组合在level-list

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/light_open"
        android:maxLevel="0"
        android:minLevel="0">
    </item>

    <item
        android:drawable="@drawable/light_close"
        android:maxLevel="1"
        android:minLevel="1">
    </item>
</level-list>

(2)将上述 Drawable 设置为 ImageView 的src

<ImageView
        android:id="@+id/first_layout_level_iv"
        android:layout_width="200dp"
        android:src="@drawable/light_level"
        android:layout_height="200dp" />

    <Button
        android:id="@+id/first_layout_switch_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切换状态" />

(3)通过改变 Drawable 的level切换图片

mLevelIv = findViewById(R.id.first_layout_level_iv);
mSwitchBtn = findViewById(R.id.first_layout_switch_btn);

mSwitchBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Drawable d = mLevelIv.getDrawable();
        int tempIndex = (index++) % 2;
        d.setLevel(nums[tempIndex]);
    }
});

三、重点说明

四、参考文献

不同的 xml 节点下属性的说明_Google-dev

相关文章

网友评论

      本文标题:Android——Drawable的常用使用方法

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