Android Studio 中 Drawable 详细全解
文章目录
一、Drawable 概述二、Drawable 类型详解1. 位图 Drawable (BitmapDrawable)2. 矢量 Drawable (VectorDrawable)3. 形状 Drawable (ShapeDrawable)4. 图层 Drawable (LayerDrawable)5. 状态列表 Drawable (StateListDrawable)6. 级别列表 Drawable (LevelListDrawable)7. 过渡 Drawable (TransitionDrawable)8. 插入 Drawable (InsetDrawable)9. 裁剪 Drawable (ClipDrawable)10. 缩放 Drawable (ScaleDrawable)
三、Drawable 使用技巧1. 资源目录选择2. 矢量图兼容性处理3. 9-Patch 图片4. 主题属性引用5. 动态修改 Drawable
四、Drawable 与 View 的结合使用1. 作为背景2. 作为前景3. 作为 ImageView 源
五、性能优化建议六、常见问题解决
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 定义:
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 定义: android:width="24dp" android:height="24dp" android:viewportWidth="24.0" android:viewportHeight="24.0"> 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"/> 常用元素: 3. 形状 Drawable (ShapeDrawable) 特点: 通过 XML 定义几何形状支持矩形、椭圆、线、环可定义渐变、圆角、描边等效果 XML 定义: android:shape="rectangle"> android:width="2dp" android:color="#388E3C" /> 形状类型: rectangle - 矩形(默认)oval - 椭圆line - 线ring - 环形 子元素: 4. 图层 Drawable (LayerDrawable) 特点: 将多个 Drawable 叠加在一起每个图层可以有自己的位置和大小适用于复杂组合图形 XML 定义: android:left="10dp" android:top="10dp" /> 属性: 每个 android:drawable - 引用的 Drawableandroid:left/right/top/bottom - 偏移量android:gravity - 对齐方式 5. 状态列表 Drawable (StateListDrawable) 特点: 根据视图状态显示不同 Drawable常用于按钮的不同状态(按下、选中、禁用等) XML 定义: android:state_pressed="true" /> android:state_focused="true" /> 常用状态: state_pressed - 按下状态state_focused - 获得焦点state_selected - 选中状态state_enabled - 启用状态state_checked - 勾选状态 6. 级别列表 Drawable (LevelListDrawable) 特点: 根据 Drawable 的 level 值显示不同 Drawable常用于电池电量、音量等指示器 XML 定义: android:maxLevel="20" /> android:maxLevel="70" /> android:maxLevel="100" /> 使用方法: ImageView imageView = findViewById(R.id.image_view); imageView.setImageLevel(50); // 显示 battery_medium 7. 过渡 Drawable (TransitionDrawable) 特点: 在两个 Drawable 之间实现淡入淡出过渡效果需要代码控制过渡 XML 定义: 使用方法: TransitionDrawable transition = (TransitionDrawable) imageView.getDrawable(); transition.startTransition(1000); // 1秒过渡 8. 插入 Drawable (InsetDrawable) 特点: 在另一个 Drawable 周围插入边距适用于需要内边距的场景 XML 定义: android:drawable="@drawable/background" android:insetLeft="16dp" android:insetTop="16dp" android:insetRight="16dp" android:insetBottom="16dp" /> 9. 裁剪 Drawable (ClipDrawable) 特点: 根据 level 值裁剪另一个 Drawable常用于进度条等 XML 定义: 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 值缩放另一个 Drawablelevel 范围 0-10000 XML 定义: 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. 主题属性引用 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. 作为背景