Android 10 关机充电字体 设置范例
BGM
Android 10 以来 关机充电在AOSP中只显示图片 无法显示百分比
探讨
Android 9及 之前的解决方案 一般是讲字体转换为二进制头文件来加载 ,
Android 10中资源加载的逻辑改变 二进制文件的解析 移植困难
思路
AOSP中 Recovery mode中使用的miui的字体。 可以在系统不启动的时候加载字体
使用此种字体解析方案 可以更快的实现关机充电字体
参考源码
bootable/recovery/minui/resources.cpp
PngHandler::PngHandler(const std::string& name) {
std::string res_path = g_resource_dir + "/" + name + ".png";
png_fp_.reset(fopen(res_path.c_str(), "rbe"));
// Try to read from |name| if the resource path does not work.
if (!png_fp_) {
png_fp_.reset(fopen(name.c_str(), "rbe"));
}
if (!png_fp_) {
error_code_ = -1;
return;
}
uint8_t header[8];
size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get());
if (bytesRead != sizeof(header)) {
error_code_ = -2;
return;
}
if (png_sig_cmp(header, 0, sizeof(header))) {
error_code_ = -3;
return;
}
png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr_) {
error_code_ = -4;
return;
}
info_ptr_ = png_create_info_struct(png_ptr_);
if (!info_ptr_) {
error_code_ = -5;
return;
}
if (setjmp(png_jmpbuf(png_ptr_))) {
error_code_ = -6;
return;
}
png_init_io(png_ptr_, png_fp_.get());
png_set_sig_bytes(png_ptr_, sizeof(header));
png_read_info(png_ptr_, info_ptr_);
png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr,
nullptr);
channels_ = png_get_channels(png_ptr_, info_ptr_);
if (bit_depth_ == 8 && channels_ == 3 && color_type_ == PNG_COLOR_TYPE_RGB) {
// 8-bit RGB images: great, nothing to do.
} else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_GRAY) {
// 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
png_set_expand_gray_1_2_4_to_8(png_ptr_);
} else if (bit_depth_ <= 8 && channels_ == 1 && color_type_ == PNG_COLOR_TYPE_PALETTE) {
// paletted images: expand to 8-bit RGB. Note that we DON'T
// currently expand the tRNS chunk (if any) to an alpha
// channel, because minui doesn't support alpha channels in
// general.
png_set_palette_to_rgb(png_ptr_);
channels_ = 3;
} else {
fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_,
channels_, color_type_);
error_code_ = -7;
}
}
总结
要熟悉关机充电的流程。合理利用已有机制