1. 引言
开机动画是 Android 系统启动时展示给用户的第一印象,也是厂商定制系统时最常调整的部分之一。无论是修改开机动画的图片、文字,还是调整动画的播放节奏,核心都围绕一个文件------bootanimation.zip。本文将带你从零开始,掌握 Android 开机动画的完整定制流程,包括格式规范、制作方法、常见问题排查,以及进阶的源码级定制方案。
2. 开机动画的基本原理
Android 系统的开机过程分为两个阶段,对应两段不同的动画展示:
- 第一阶段(Linux 内核启动) :此时 Android 系统尚未完全启动,屏幕显示的是内核启动阶段的静态图片或 Logo,通常由内核配置或
init进程早期阶段控制。 - 第二阶段(Android 系统启动) :系统服务(如
SurfaceFlinger)启动后,会读取并播放bootanimation.zip中的动画,这是用户通常所说的"开机动画"。
bootanimation 是一个独立的可执行程序,由 init 进程在系统启动后期启动,它负责解析并播放 bootanimation.zip 中的帧序列。
3. bootanimation.zip 格式规范
先看一下打包规范,源码文件已经把这部分规范写出来了目录:frameworks/base/cmds/bootanimation
FORMAT.md
# bootanimation format
## zipfile paths
The system selects a boot animation zipfile from the following locations, in order:
/system/media/bootanimation-encrypted.zip (if getprop("vold.decrypt") = '1')
/system/media/bootanimation.zip
/oem/media/bootanimation.zip
## zipfile layout
The `bootanimation.zip` archive file includes:
desc.txt - a text file
part0 \
part1 \ directories full of PNG frames
... /
partN /
## desc.txt format
The first line defines the general parameters of the animation:
WIDTH HEIGHT FPS
* **WIDTH:** animation width (pixels)
* **HEIGHT:** animation height (pixels)
* **FPS:** frames per second, e.g. 60
It is followed by a number of rows of the form:
TYPE COUNT PAUSE PATH [#RGBHEX CLOCK]
* **TYPE:** a single char indicating what type of animation segment this is:
+ `p` -- this part will play unless interrupted by the end of the boot
+ `c` -- this part will play to completion, no matter what
* **COUNT:** how many times to play the animation, or 0 to loop forever until boot is complete
* **PAUSE:** number of FRAMES to delay after this part ends
* **PATH:** directory in which to find the frames for this part (e.g. `part0`)
* **RGBHEX:** _(OPTIONAL)_ a background color, specified as `#RRGGBB`
* **CLOCK:** _(OPTIONAL)_ the y-coordinate at which to draw the current time (for watches)
There is also a special TYPE, `$SYSTEM`, that loads `/system/media/bootanimation.zip`
and plays that.
## loading and playing frames
Each part is scanned and loaded directly from the zip archive. Within a part directory, every file
(except `trim.txt` and `audio.wav`; see next sections) is expected to be a PNG file that represents
one frame in that part (at the specified resolution). For this reason it is important that frames be
named sequentially (e.g. `part000.png`, `part001.png`, ...) and added to the zip archive in that
order.
## trim.txt
To save on memory, textures may be trimmed by their background color. trim.txt sequentially lists
the trim output for each frame in its directory, so the frames may be properly positioned.
Output should be of the form: `WxH+X+Y`. Example:
713x165+388+914
708x152+388+912
707x139+388+911
649x92+388+910
If the file is not present, each frame is assumed to be the same size as the animation.
## audio.wav
Each part may optionally play a `wav` sample when it starts. To enable this, add a file
with the name `audio.wav` in the part directory.
## exiting
The system will end the boot animation (first completing any incomplete or even entirely unplayed
parts that are of type `c`) when the system is finished booting. (This is accomplished by setting
the system property `service.bootanim.exit` to a nonzero string.)
## protips
### PNG compression
Use `zopflipng` if you have it, otherwise `pngcrush` will do. e.g.:
for fn in *.png ; do
zopflipng -m ${fn}s ${fn}s.new && mv -f ${fn}s.new ${fn}
# or: pngcrush -q ....
done
Some animations benefit from being reduced to 256 colors:
pngquant --force --ext .png *.png
# alternatively: mogrify -colors 256 anim-tmp/*/*.png
### creating the ZIP archive
cd <path-to-pieces>
zip -0qry -i \*.txt \*.png \*.wav @ ../bootanimation.zip *.txt part*
Note that the ZIP archive is not actually compressed! The PNG files are already as compressed
as they can reasonably get, and there is unlikely to be any redundancy between files.
bootanimation.zip 是一个标准的 ZIP 压缩包,内部结构如下:
python
bootanimation.zip
├── desc.txt # 动画描述文件(必需)
├── part0/ # 第一部分动画帧(通常是循环播放)
│ ├── 00001.png
│ ├── 00002.png
│ └── ...
├── part1/ # 第二部分动画帧(通常是收尾,播放一次)
│ ├── 00001.png
│ └── ...
└── part2/ # 可选的第三部分
3.1 desc.txt 描述文件
desc.txt 是动画的核心配置文件,控制播放顺序、循环次数和帧率。其格式如下:
css
480 800 30
p 1 0 part0
p 0 0 part1
- 第一行 :
宽度 高度 帧率,例如480 800 30表示动画帧图片尺寸为 480×800 像素,每秒播放 30 帧。 - 后续行 :
p 循环次数 阶段切换延迟 part目录名p:固定标识,表示这是一个播放阶段(part)。- 循环次数:
0表示无限循环,1表示播放一次,其他数字表示具体循环次数。 - 阶段切换延迟:切换到下一阶段前等待的帧数,通常为
0。 - part 目录名:对应 ZIP 内的帧图片目录。
3.2 帧图片要求
- 图片格式:推荐使用 PNG 格式,支持透明通道。
- 图片尺寸:必须与
desc.txt第一行声明的宽高一致。 - 命名规则:目录内的图片按播放顺序命名,通常为
00001.png、00002.png等,按文件名排序播放。
4. 制作 bootanimation.zip
4.1 准备帧图片
使用图像处理工具(如 Photoshop、GIMP)或脚本生成一系列连续帧图片。例如,一个简单的"渐入"动画可以生成 30 张透明度逐渐增大的 PNG 图片。
4.2 编写 desc.txt
根据动画设计编写描述文件。例如,一个先循环播放、最后定格收尾的动画:
css
1080 1920 30
p 1 0 part0
p 0 0 part1
4.3 打包为 ZIP
打包时需要注意:ZIP 内的文件不能有额外的顶层目录,且建议使用**存储(不压缩)**模式,以避免部分设备解压失败。
bash
zip -r -X -Z store bootanimation part*/* desc.txt
关键点:
- 不要将
bootanimation.zip本身再套一层文件夹。
5. 替换开机动画
5.1 有 Root 权限的设备
bash
# 备份原动画
adb root
adb remount
adb pull /system/media/bootanimation.zip /backup/bootanimation.zip.bak
# 推送新动画
adb push bootanimation.zip /system/media/bootanimation.zip
# 设置权限并重启
adb shell chmod 644 /system/media/bootanimation.zip
adb reboot
5.2 无 Root 权限的设备
无 Root 设备通常无法直接修改 /system 分区。此时可以考虑:
- 使用厂商提供的主题/换机工具(部分 ROM 支持)。
- 刷入第三方 Recovery 后通过刷机包替换。
- 使用 Magisk 模块方式挂载替换(需设备已解锁并安装 Magisk)。
6. 常见问题排查
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
| 开机动画不显示,黑屏 | ZIP 内目录结构错误 | 检查 desc.txt 与目录名是否匹配 |
| 动画播放卡顿 | 帧率过高或图片过大 | 降低帧率或压缩图片尺寸 |
| 动画只播放一次就停止 | desc.txt 循环次数配置错误 |
检查 p 行的循环次数参数 |
| 部分设备无法解压 | ZIP 使用了压缩模式 | 使用 zip -0 重新打包 |
| 动画显示比例变形 | 图片尺寸与 desc.txt 不一致 |
统一图片尺寸与声明宽高 |
7. 进阶:源码级定制
对于系统开发者,可以在 Android 源码中直接修改开机动画相关配置。
7.1 修改默认动画路径
bootanimation 的默认查找路径定义在源码中,通常为 /system/media/bootanimation.zip。部分设备还支持 /oem/media/bootanimation.zip 作为备选路径。
7.2 修改默认动画
在源码编译环境中,将制作好的 bootanimation.zip 放入设备专属的 device/<厂商>/<型号>/ 目录下,并在 device.mk 中添加:
makefile
PRODUCT_COPY_FILES += \
device/<厂商>/<型号>/bootanimation.zip:system/media/bootanimation.zip
重新编译系统镜像后,开机动画即被内置。
7.3 关闭开机动画
部分场景(如调试)需要快速启动,可以关闭开机动画。在 init.rc 中设置:
ini
setprop debug.sf.nobootanimation 1
8. 总结
Android 开机动画的定制核心在于 bootanimation.zip 的制作与替换。掌握 desc.txt 的格式规范、帧图片的命名规则以及 ZIP 打包的注意事项,即可完成绝大多数定制需求。对于更深度的系统级定制,则需要结合源码编译环境进行操作。希望本文能帮助你顺利实现自己的开机动画。