愛在花開的季節 发表于 4 天前

Android Studio 中 Drawable 具体全解

Drawable 是 Android 中用于表现可绘制图形资源的抽象概念,它是 Android 应用视觉呈现的告急组成部分。下面是对 Android Studio 中 Drawable 的全面剖析。
一、Drawable 概述

Drawable 是一个可以绘制在屏幕上的抽象对象,它可以是:


[*]位图图像(BitmapDrawable)
[*]矢量图形(VectorDrawable)
[*]外形(ShapeDrawable)
[*]图层(LayerDrawable)
[*]状态列表(StateListDrawable)
[*]动画(AnimatedVectorDrawable)等
二、Drawable 类型详解

1. 位图 Drawable (BitmapDrawable)

特点:


[*]基于像素的图像
[*]支持 PNG、JPEG、GIF、WebP 等格式
[*]资源存放在 res/drawable 或 res/drawable-xxx 目次
XML 定义:
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/my_image"
    android:gravity="center"
    android:tileMode="disabled" />
属性:


[*]android:src - 指定图片资源
[*]android:gravity - 图片在容器中的位置
[*]android:tileMode - 平铺模式(repeat, mirror, clamp)
2. 矢量 Drawable (VectorDrawable)

特点:


[*]基于 XML 的矢量图形
[*]缩放不失真
[*]文件体积小
[*]支持 API 21+(通过支持库可向下兼容)
XML 定义:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
    <path
      android:fillColor="#FF000000"
      android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zm0,3c1.66,0 3,1.34 3,3s-1.34,3 -3,3 -3,-1.34 -3,-3 1.34,-3 3,-3zm0,14.2c-2.5,0 -4.71,-1.28 -6,-3.22 0.03,-1.99 4,-3.08 6,-3.08 1.99,0 5.97,1.09 6,3.08 -1.29,1.94 -3.5,3.22 -6,3.22z"/>
</vector>
常用元素:


[*]<vector> - 根元素
[*]<path> - 定义路径
[*]<group> - 定义一组路径或子组
[*]<clip-path> - 定义裁剪路径
3. 外形 Drawable (ShapeDrawable)

特点:


[*]通过 XML 定义几何外形
[*]支持矩形、椭圆、线、环
[*]可定义渐变、圆角、描边等结果
XML 定义:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="8dp" />
    <solid android:color="#4CAF50" />
    <stroke
      android:width="2dp"
      android:color="#388E3C" />
</shape>
外形类型:


[*]rectangle - 矩形(默认)
[*]oval - 椭圆
[*]line - 线
[*]ring - 环形
子元素:


[*]<corners> - 圆角
[*]<gradient> - 渐变
[*]<padding> - 内边距
[*]<size> - 尺寸
[*]<solid> - 纯色填充
[*]<stroke> - 描边
4. 图层 Drawable (LayerDrawable)

特点:


[*]将多个 Drawable 叠加在一起
[*]每个图层可以有自己的位置和大小
[*]适用于复杂组合图形
XML 定义:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shadow" />
    <item android:drawable="@drawable/icon"
          android:left="10dp"
          android:top="10dp" />
</layer-list>
属性:


[*]每个 <item> 可以设置:

[*]android:drawable - 引用的 Drawable
[*]android:left/right/top/bottom - 偏移量
[*]android:gravity - 对齐方式

5. 状态列表 Drawable (StateListDrawable)

特点:


[*]根据视图状态显示不同 Drawable
[*]常用于按钮的不同状态(按下、选中、禁用等)
XML 定义:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>
常用状态:


[*]state_pressed - 按下状态
[*]state_focused - 获得焦点
[*]state_selected - 选中状态
[*]state_enabled - 启用状态
[*]state_checked - 勾选状态
6. 级别列表 Drawable (LevelListDrawable)

特点:


[*]根据 Drawable 的 level 值显示不同 Drawable
[*]常用于电池电量、音量等指示器
XML 定义:
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/battery_low"
          android:maxLevel="20" />
    <item android:drawable="@drawable/battery_medium"
          android:maxLevel="70" />
    <item android:drawable="@drawable/battery_full"
          android:maxLevel="100" />
</level-list>
使用方法:
ImageView imageView = findViewById(R.id.image_view);
imageView.setImageLevel(50); // 显示 battery_medium
7. 过渡 Drawable (TransitionDrawable)

特点:


[*]在两个 Drawable 之间实现淡入淡出过渡结果
[*]必要代码控制过渡
XML 定义:
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/image1" />
    <item android:drawable="@drawable/image2" />
</transition>
使用方法:
TransitionDrawable transition = (TransitionDrawable) imageView.getDrawable();
transition.startTransition(1000); // 1秒过渡
8. 插入 Drawable (InsetDrawable)

特点:


[*]在另一个 Drawable 周围插入边距
[*]适用于必要内边距的场景
XML 定义:
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/background"
    android:insetLeft="16dp"
    android:insetTop="16dp"
    android:insetRight="16dp"
    android:insetBottom="16dp" />
9. 裁剪 Drawable (ClipDrawable)

特点:


[*]根据 level 值裁剪另一个 Drawable
[*]常用于进度条等
XML 定义:
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/progress"
    android:clipOrientation="horizontal"
    android:gravity="left" />
使用方法:
ClipDrawable clip = (ClipDrawable) imageView.getDrawable();
clip.setLevel(5000); // 50% 进度 (0-10000)
10. 缩放 Drawable (ScaleDrawable)

特点:


[*]根据 level 值缩放另一个 Drawable
[*]level 范围 0-10000
XML 定义:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/icon"
    android:scaleGravity="center"
    android:scaleHeight="80%"
    android:scaleWidth="80%" />
三、Drawable 使用技巧

1. 资源目次选择



[*]drawable/ - 默认目次
[*]drawable-mdpi/ - 中等密度
[*]drawable-hdpi/ - 高密度
[*]drawable-xhdpi/ - 超高密度
[*]drawable-xxhdpi/ - 超超高密度
[*]drawable-xxxhdpi/ - 超超超高密度
[*]drawable-night/ - 夜间模式资源
[*]drawable-v24/ - API 24+ 特定资源
2. 矢量图兼容性处理

// build.gradle
android {
    defaultConfig {
      vectorDrawables.useSupportLibrary = true
    }
}
// 代码中使用
AppCompatImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.vector_icon);
3. 9-Patch 图片



[*]可拉伸的 PNG 图片
[*]文件扩展名 .9.png
[*]使用 Android Studio 的 9-patch 编辑器
4. 主题属性引用

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/colorPrimary" />
</shape>
5. 动态修改 Drawable

// 获取并修改 GradientDrawable
GradientDrawable shape = (GradientDrawable) view.getBackground();
shape.setColor(ContextCompat.getColor(context, R.color.new_color));
shape.setCornerRadius(newRadius);
shape.setStroke(newWidth, newColor);
四、Drawable 与 View 的结合使用

1. 作为背景

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_bg" />
2. 作为远景

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@drawable/foreground_overlay" />
3. 作为 ImageView 源

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon" />
五、性能优化发起


[*] 优先使用矢量图:对于简单图标,VectorDrawable 是更好的选择
[*] 公道使用位图:复杂图像才使用位图,并注意提供多密度版本
[*] 复用 Drawable:克制重复创建相同的 Drawable 实例
[*] 考虑使用 tint:通过着色减少 Drawable 资源数量
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_arrow"
    android:tint="@color/primary_color" />

[*] 克制太过绘制:简化 Drawable 条理结构
六、常见问题解决


[*] 矢量图不显示:

[*]确保开启了支持库兼容
[*]查抄 SVG 转换为 VectorDrawable 的兼容性

[*] Drawable 内存泄漏:

[*]克制在静态变量中持有 Drawable
[*]注意 Activity 销毁时开释 Drawable 引用

[*] 状态列表不生效:

[*]查抄状态次序(最后一个 item 应该是默认状态)
[*]确保视图是可点击的(clickable=true)

[*] 9-patch 图片变形:

[*]使用 Android Studio 的 9-patch 工具重新编辑
[*]查抄拉伸地区定义是否精确

通过把握这些 Drawable 的知识和技巧,你可以更高效地创建和管理 Android 应用的视觉资源,提拔应用的用户体验和性能表现。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Android Studio 中 Drawable 具体全解