Android的bootanimation用于控制顯示開機的啟動動畫,但是由于Android源碼里只支持一幀一幀的顯示圖片,從而達到動畫的效果, bootanimation本身并不支持播放mp4. 那對于一些對于流暢度要求很高,或者想要做一些非常酷炫的動畫效果, bootanimation怕是不行了。
那如果把動畫效果做成mp4視頻,然后在bootanimation里播放,那會不會是一種不錯的解決方案呢?
本篇文章主要是對于bootanimation播放mp4的可行性進行分析。
PS: 筆者已經在7.0上試驗成功了
轉載請標注出處: http://www.lxweimin.com/p/d3e8f1378846
1. Android的啟動過程
安卓的啟動過程大致可以分為三個階段
- bootloader
- kernel
- init
bootloader啟動時會顯示一些文字
kernel啟動時會顯示一張靜態圖片
init啟動時會在bootanimation里顯示動畫
老羅的Android系統的開機畫面顯示過程分析對這三個階段講解得非常清楚,大家可以去拜讀一下。
2. bootanimation是什么
bootanimation是一個可執行進程,位于 /system/bin下
bootanimation的代碼位置: frameworks/base/cmds/bootanimation
在Android 7.0, bootanimation.rc并不是直接放到了init.rc里面,它放在/etc/init/下,作為一個單獨的rc配置文件存在。
那么bootanimation.rc什么時候被解析呢
在 init 進程里, 準確說是在builtins.cpp
文件里被解析的
static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Parser& parser = Parser::GetInstance();
if (end_index <= start_index) {
// Use the default set if no path is given
//init_directories是保存xxx.rc的地方,
static const std::vector<std::string> init_directories = {
"/system/etc/init",
"/vendor/etc/init",
"/odm/etc/init"
};
for (const auto& dir : init_directories) {
parser.ParseConfig(dir);
}
} else {
for (size_t i = start_index; i < end_index; ++i) {
parser.ParseConfig(args[i]);
}
}
}
3. bootanimation啟動
bootanimation.rc文件定義如下
service bootanim /system/bin/bootanimation
class core
user graphics
group graphics audio
disabled
oneshot
從定義可以看出, bootanimation被定義成了init進程的一個service.,它屬于 core類,只能啟動運行一次(oneshot), 是disabled的, 意思是 class_start core時,并不會啟動它。
那bootanimation是在什么時候被啟動的呢?
在 SurfaceFlinger初始化的最后
void SurfaceFlinger::init() {
// start boot animation
startBootAnim();
}
void SurfaceFlinger::startBootAnim() {
// start boot animation
property_set("service.bootanim.exit", "0");
property_set("ctl.start", "bootanim");
}
startBootAnim
設置了ctl.start=bootanim
這個property, 從而會觸發init中正在監聽property的handle_property_set_fd
函數
簡化后如下
static void handle_property_set_fd()
{
switch(msg.cmd) {
case PROP_MSG_SETPROP:
if (memcmp(msg.name,"ctl.",4) == 0) {
if (check_control_mac_perms(msg.value, source_ctx, &cr)) {
handle_control_message((char*) msg.name + 4, (char*) msg.value);
}
}
}
void handle_control_message(const std::string& msg, const std::string& name) {
Service* svc = ServiceManager::GetInstance().FindServiceByName(name);
//找到 bootanim 的service (由bootanim.rc定義), 然后start
if (msg == "start") {
svc->Start();
} else if (msg == "stop") {
svc->Stop();
} else if (msg == "restart") {
svc->Restart();
}
}
這里先通過name也就是bootanim去查找service, 然后start這個service, 這里bootanimation就開始運行起來了。
bootanimation的具體代碼的執行可以參考老羅的分析,在這里就不重復造輪子了。
4. bootanimation播放mp4分析
既然 bootanimation 要播放 mp4 文件,那么肯定得相關服務已經啟動了才行,如 MediaPlayer相關. 通過對init的log分析 (adb shell dmesg),可以發現 audioserver
media
相關的服務已經于bootanim先啟動起來,這樣就可以得到 Mediaplayer在bootanim里能正常的播放多媒體文件。