Android Studio 中 Drawable 具体全解

打印 上一主题 下一主题

主题 1825|帖子 1825|积分 5475

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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 定义
  1. <bitmap
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:src="@drawable/my_image"
  4.     android:gravity="center"
  5.     android:tileMode="disabled" />
复制代码
属性


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

特点


  • 基于 XML 的矢量图形
  • 缩放不失真
  • 文件体积小
  • 支持 API 21+(通过支持库可向下兼容)
XML 定义
  1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:width="24dp"
  3.     android:height="24dp"
  4.     android:viewportWidth="24.0"
  5.     android:viewportHeight="24.0">
  6.     <path
  7.         android:fillColor="#FF000000"
  8.         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"/>
  9. </vector>
复制代码
常用元素


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

特点


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


  • rectangle - 矩形(默认)
  • oval - 椭圆
  • line - 线
  • ring - 环形
子元素


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

特点


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


  • 每个 <item> 可以设置:

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

5. 状态列表 Drawable (StateListDrawable)

特点


  • 根据视图状态显示不同 Drawable
  • 常用于按钮的不同状态(按下、选中、禁用等)
XML 定义
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2.     <item android:drawable="@drawable/button_pressed"
  3.           android:state_pressed="true" />
  4.     <item android:drawable="@drawable/button_focused"
  5.           android:state_focused="true" />
  6.     <item android:drawable="@drawable/button_default" />
  7. </selector>
复制代码
常用状态


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

特点


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

特点


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

特点


  • 在另一个 Drawable 周围插入边距
  • 适用于必要内边距的场景
XML 定义
  1. <inset xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:drawable="@drawable/background"
  3.     android:insetLeft="16dp"
  4.     android:insetTop="16dp"
  5.     android:insetRight="16dp"
  6.     android:insetBottom="16dp" />
复制代码
9. 裁剪 Drawable (ClipDrawable)

特点


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

特点


  • 根据 level 值缩放另一个 Drawable
  • level 范围 0-10000
XML 定义
  1. <scale xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:drawable="@drawable/icon"
  3.     android:scaleGravity="center"
  4.     android:scaleHeight="80%"
  5.     android:scaleWidth="80%" />
复制代码
三、Drawable 使用技巧

1. 资源目次选择



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

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



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

  1. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  2.     <solid android:color="?attr/colorPrimary" />
  3. </shape>
复制代码
5. 动态修改 Drawable

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

1. 作为背景

  1. <Button
  2.     android:layout_width="wrap_content"
  3.     android:layout_height="wrap_content"
  4.     android:background="@drawable/button_bg" />
复制代码
2. 作为远景

  1. <FrameLayout
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     android:foreground="@drawable/foreground_overlay" />
复制代码
3. 作为 ImageView 源

  1. <ImageView
  2.     android:layout_width="wrap_content"
  3.     android:layout_height="wrap_content"
  4.     android:src="@drawable/icon" />
复制代码
五、性能优化发起


  • 优先使用矢量图:对于简单图标,VectorDrawable 是更好的选择
  • 公道使用位图:复杂图像才使用位图,并注意提供多密度版本
  • 复用 Drawable:克制重复创建相同的 Drawable 实例
  • 考虑使用 tint:通过着色减少 Drawable 资源数量
    1. <ImageView
    2.     android:layout_width="wrap_content"
    3.     android:layout_height="wrap_content"
    4.     android:src="@drawable/ic_arrow"
    5.     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企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

愛在花開的季節

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表