博主头像
LT技术站

行动起来,活在当下

  • 累计撰写 255 篇文章
  • 累计创建 131 个标签
  • 累计收到 0 条评论

目录

Android高级UI

Android drawable和mipmap有啥区别?

Invariant resource types in Android design serve distinct purposes:Drawable contains UI elements like backgrounds, icons, and buttons while Mipmap exclusively holds application icons for Launcher display. Key differences include: 1. Purpose - Mipmap ensures system-provided density-independent scaling for app icons across devices, maintaining quality through multiple resource Dirkets(mdpi-xhdpi-xxhdpi). Drawable handles dynamic UI assets needing precise layout control. 2. Android 8+ Recommendations - System mandates mipmap placement for adaptive icon support since Android 8.0's Vector Drawables and Adaptive Icons introduced. Incorrect placement causes resource indexing errors and display artifacts. 3. Common Mistakes - Placing app icons in drawable prevents adaptive rendering. Conversely, placing UI elements in mipmap creates unnecessary resource confusion. Files in wrong directories lead to runtime crashes or unintended Liberation gradients. 4. Legacy Projects - Existing apps using.drawableLaunchers remain functional but risk violates modern guidelines without migration. New projects strictly follow mipmap for icons and drawable for graphical UI components. Developers should implement:){ √ radical anydpi-v26目录规范新应用的图标资源 √ drawable目录持续承载按钮、界面对象 √ 避免跨目录调用导致资源错位 }通过 проводится соответствие Android Team guidelines for optimal deliver experienced both system and user end.

在 Android 中,drawable 和 mipmap 都可以存放图片资源,但它们的设计目的不同。

简单理解:

drawable:普通图片资源,用于界面显示
mipmap:应用启动图标资源,用于 Launcher 显示


1. drawable 是干什么的?

drawable 是 Android 最常用的图片资源目录。

例如:

res/
 ├── drawable/
 │    ├── icon.png
 │    ├── background.png
 │    └── button.xml

里面可以放:

  • PNG/JPEG/WebP 图片

  • Vector Drawable(XML矢量图)

  • Shape Drawable

  • Selector

  • Layer-list

例如:

<ImageView
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/avatar"/>

典型用途:

  • 图片展示

  • 背景

  • 按钮状态

  • 图标

  • UI 装饰


2. mipmap 是干什么的?

mipmap 最初是专门给 应用图标(Launcher Icon) 使用的。

例如:

res/
 ├── mipmap-hdpi/
 │    └── ic_launcher.png
 ├── mipmap-xhdpi/
 │    └── ic_launcher.png
 ├── mipmap-xxhdpi/
 │    └── ic_launcher.png

AndroidManifest:

<application
    android:icon="@mipmap/ic_launcher">
</application>

这里使用的是:

@mipmap/ic_launcher

而不是:

@drawable/ic_launcher

3. 为什么 Android 要设计 mipmap?

核心原因:

Launcher 会根据设备情况动态缩放应用图标。

例如:

  • 手机桌面

  • 应用抽屉

  • 最近任务列表

  • 系统设置里的应用列表

这些地方显示图标时:

原始图标
    |
    |
系统缩放
    |
    ↓
不同大小显示

如果图标资源放在 drawable:

drawable-xxhdpi

系统认为:

这是普通 UI 图片

可能会按照普通资源策略处理。

而 mipmap 告诉系统:

这是一个需要被频繁缩放的应用图标

因此系统会保留更高质量资源。


4. mipmap 和 drawable 的资源匹配机制类似

例如:

drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi

和:

mipmap-mdpi
mipmap-hdpi
mipmap-xhdpi
mipmap-xxhdpi
mipmap-xxxhdpi

规则一样。

Android 根据:

设备dpi

自动选择:

drawable-xxhdpi/icon.png

或者:

mipmap-xxhdpi/ic_launcher.png

5. Android Studio 为什么默认生成 mipmap?

创建 Android 项目:

res/
├── mipmap-anydpi-v26/
│    └── ic_launcher.xml
├── mipmap-hdpi/
├── mipmap-mdpi/
├── mipmap-xhdpi/
├── mipmap-xxhdpi/
└── mipmap-xxxhdpi/

原因:

Android 8.0 引入了:

Adaptive Icon(自适应图标)

例如:

ic_launcher.xml

内容:

<adaptive-icon>
    <background>
        ...
    </background>

    <foreground>
        ...
    </foreground>
</adaptive-icon>

它由:

背景层
+
前景层

组成。

系统可以根据 Launcher 风格:

  • 圆形

  • 方形

  • 圆角矩形

自动裁剪。

这就是为什么现在 App 图标基本都放:

mipmap-anydpi-v26

6. 实际开发应该怎么用?

现在 Android 官方推荐:

资源

目录

App 图标

mipmap

普通图片

drawable

矢量图

drawable

按钮背景

drawable

启动页图片

drawable

头像

drawable

例如:

res

├── drawable
│   ├── avatar.webp
│   ├── bg_login.xml
│   └── ic_close.xml
│
└── mipmap
    ├── ic_launcher.xml
    └── ic_launcher_round.xml

7. 那如果我把图片放错会怎样?

情况1:

把 App 图标放 drawable:

drawable/ic_launcher.png

然后:

android:icon="@drawable/ic_launcher"

可以运行。

但是:

缺点:

  • 不支持 Android 8 自适应图标

  • Launcher 缩放质量可能不好

  • 不符合 Android 规范


情况2:

把普通图片放 mipmap:

例如:

mipmap/avatar.png

然后:

android:src="@mipmap/avatar"

也能运行。

但是:

没有意义。

因为:

  • 增加资源混乱

  • 误导维护者


8. 为什么很多老项目全部用 drawable?

因为早期 Android:

Android 1.x ~ 7.x:

没有 Adaptive Icon。

很多项目:

res/drawable/ic_launcher.png

也能正常工作。

后来 Android Studio 才逐渐规范:

App Icon → mipmap
其他图片 → drawable

总结

drawable

mipmap

用途

普通图片资源

应用图标

主要用户

App UI

系统 Launcher

支持 Adaptive Icon

❌

✅

推荐放图片

✅

❌

推荐放 App Logo

❌

✅

实际 Android 项目中:

除了 ic_launcher、ic_launcher_round 等应用图标,其它图片基本全部放 drawable。

这也是现在 Jetpack Compose / Material Design 项目里的主流做法。

那年今日11 月 29 日

    评论区