Android 开发中的 Drawable**
在 Android 开发中,Drawable 是 可绘制对象,用于表示图片、形状、颜色、动画等。它是 android.graphics.drawable.Drawable 类的实例,可以在 ImageView、TextView 背景等地方使用。
1. Drawable 的常见类型
(1) 直接使用图片 (BitmapDrawable)
可以使用 JPG、PNG、WEBP、SVG 等格式的图片,常见存放位置:
res/drawable/:不同密度的位图(如drawable-mdpi、drawable-xhdpi)res/mipmap/:应用图标assets/:不受res/限制的资源
🔹 使用方式
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sample_image"/>或者在 Java/Kotlin 代码中:
imageView.setImageDrawable(getResources().getDrawable(R.drawable.sample_image));⚠️** getDrawable() 过时,建议用:**
imageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.sample_image));(2) 纯色或渐变背景 (ShapeDrawable)
可以使用 XML 形状 (shape) 定义 圆角、边框、渐变等。
🔹 示例:圆角按钮
<!-- res/drawable/rounded_button.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FF6200EE"/>
<corners android:radius="16dp"/>
</shape>🔹 使用方式
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_button"
android:text="点击我"/>(3) StateListDrawable(不同状态的 Drawable)
用于 根据按钮的状态切换不同的背景(如按下时变色)。
🔹 示例:按钮按下变色
<!-- res/drawable/button_background.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/purple_700"/> <!-- 按下时 -->
<item android:drawable="@color/purple_500"/> <!-- 默认状态 -->
</selector>🔹 使用方式
<Button
android:background="@drawable/button_background"
android:text="按下试试"/>(4) LayerDrawable(叠加多个 Drawable)
可以将 多个 Drawable 叠加 在一起,例如 图片 + 遮罩。
🔹 示例:图片加边框
<!-- res/drawable/image_with_border.xml -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#FF0000"/> <!-- 红色边框 -->
<corners android:radius="8dp"/>
</shape>
</item>
<item android:drawable="@drawable/sample_image" android:left="5dp" android:top="5dp"/>
</layer-list>🔹 使用方式
<ImageView
android:src="@drawable/image_with_border"/>(5) InsetDrawable(内间距 Drawable)
给 Drawable 添加内边距:
<!-- res/drawable/inset_example.xml -->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="10dp"
android:insetTop="10dp"
android:insetRight="10dp"
android:insetBottom="10dp">
<shape android:shape="rectangle">
<solid android:color="#00FF00"/>
</shape>
</inset>(6) ClipDrawable(裁剪 Drawable)
可以 **根据进度裁剪 **Drawable,常用于 进度条、音量调节等。
🔹 示例:裁剪一半
<!-- res/drawable/clip_example.xml -->
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/sample_image"
android:clipOrientation="horizontal"
android:gravity="left"/>(7) ScaleDrawable(按比例缩放 Drawable)
可以 根据 View 大小自动缩放。
🔹 示例:缩放 50%
<!-- res/drawable/scale_example.xml -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/sample_image"
android:scaleWidth="50%"
android:scaleHeight="50%"/>(8) AnimatedDrawable(帧动画 Drawable)
用于播放 逐帧动画,类似 GIF。
🔹 示例:逐帧动画
<!-- res/drawable/frame_animation.xml -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/frame1" android:duration="100"/>
<item android:drawable="@drawable/frame2" android:duration="100"/>
<item android:drawable="@drawable/frame3" android:duration="100"/>
</animation-list>🔹 代码启动动画
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.frame_animation);
AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable();
animation.start();2. Drawable 在代码中的使用
**(1) 通过 getDrawable() 获取 **Drawable
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.sample_image);
imageView.setImageDrawable(drawable);**(2) 动态修改 **Drawable
可以改变 Drawable 的颜色、大小等:
drawable.setTint(Color.RED); // 改变颜色
drawable.setAlpha(128); // 设置透明度**(3) Bitmap 转 **Drawable
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_image);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);**(4) Drawable 转 **Bitmap
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();3. 总结
| 类型 | 作用 | 常见用途 |
|---|---|---|
BitmapDrawable | 直接使用图片 | 头像、图标 |
ShapeDrawable | 绘制形状 | 按钮背景、边框 |
StateListDrawable | 切换不同状态 | 按钮点击效果 |
LayerDrawable | 多个 Drawable 叠加 | 图片 + 边框 |
InsetDrawable | 给 Drawable 加内边距 | 控制间距 |
ClipDrawable | 根据进度裁剪 | 进度条 |
ScaleDrawable | 按比例缩放 | 自适应大小的图标 |
AnimationDrawable | 逐帧动画 | 加载动画、表情动画 |
🔹 Drawable** 的核心用途:**
- 优化性能:避免直接使用
Bitmap,减少内存占用。 - 灵活控制 UI:可以动态改变颜色、大小、形状等,而不需要修改图片资源。
- 支持 XML 复用:可以使用
selector、shape等 XML 定义不同的Drawable,减少代码量。
👉** 总之,Drawable 在 Android 开发中是非常强大的工具,它不仅仅是图片,还能实现复杂的 UI 效果!** 🎨🚀
在 Android 开发中,Drawable 是 可绘制对象,用于表示图片、形状、颜色、动画等。它是 android.graphics.drawable.Drawable 类的实例,可以在 ImageView、TextView 背景等地方使用。
更新: 2025-07-24 10:30:44
原文: https://www.yuque.com/dongpozhouzi-mshe3/zhm85g/hzfyt2l6q5lgc7xr