您现在的位置是:首页 > 文章详情

Android5.0 Recovery源代码分析与定制---recovery UI相关(二)

日期:2017-06-09点击:540

       http://blog.csdn.net/morixinguan/article/details/72858346引用我的代码片

在上一篇文章中,我们大致的介绍了recovery的启动流程,那么,recovery升级或者做双清的时候,那些图形动画又是如何实现的呢?我们来看看代码:

     以下这段代码位于recovery/screen_ui.cpp

void ScreenRecoveryUI::Init() { gr_init(); gr_font_size(&char_width, &char_height); text_col = text_row = 0; text_rows = gr_fb_height() / char_height; if (text_rows > kMaxRows) text_rows = kMaxRows; text_top = 1; text_cols = gr_fb_width() / char_width; if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; backgroundIcon[NONE] = NULL; LoadBitmapArray("icon_installing", &installing_frames, &installation); backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL; backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; LoadBitmap("icon_error", &backgroundIcon[ERROR]); backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; LoadBitmap("progress_empty", &progressBarEmpty); LoadBitmap("progress_fill", &progressBarFill); LoadBitmap("stage_empty", &stageMarkerEmpty); LoadBitmap("stage_fill", &stageMarkerFill); LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]); LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]); LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); pthread_create(&progress_t, NULL, progress_thread, NULL); RecoveryUI::Init(); }
这段代码都做了哪些事情呢?这些recovery初始化图形显示最开始的部分,

(1)调用了miniui中的gr_init初始化显示图形相关的步骤,因为recovery是基于framebuffer机制显示的。

(2)调用gr_font_size设置字体显示的大小,然后计算文本显示行列。

(3)接下来就是装载图片了,会调用到LoadBitmapArray和LoadBitmap这两个函数。其中,我们会看到这些函数里图片的名称:

"icon_installing"、"icon_error"、"progress_empty"、"progress_fill"、

        "stage_empty"、"stage_fill"、"installing_text"、"erasing_text"、

        "no_command_text"、"error_text"

将上面的字符串与下面的图片一一对应:

那么这些分别是怎么显示的?其中erasing_text是用来显示做清除的时候显示的文字,放大后如下:

上面有许许多多的语言版本,我们可以根据需要来选择,这些主要要看接下来初始化文字的代码逻辑。

其余的图片中,后缀带text的,也和这些是类似的,有出现错误显示的字体error_text,更新系统显示的字体installing_text,没有命令的时候显示的字体no_command_text。

除了文字显示,我们最关心的就是icon_installing这张图片了,在做系统更新的时候,这个机器人会转动。这不是动画吗?怎么只有一张图片呢?我们找到Android官方网站看看是为什么?
原因如下:

Recovery UI images
Android 5.x
The recovery user interface consists images. Ideally, users never interact with the UI: During a normal update, 
the phone boots into recovery, fills the installation progress bar, and boots back into the new system without 
input from the user. In the event of a system update problem, the only user action that can be taken is to 
call customer care.An image-only interface obviates the need for localization. 
However, as of Android 5.x the update can display a string of text (e.g. "Installing system update...") 
along with the image. For details, see Localized recovery text.


efault images are available in different densities and are located inbootable/recovery/res$DENSITY/images 
(e.g., bootable/recovery/res-hdpi/images). To use a static image during installation, 
you need only provide the icon_installing.png image and set the number of frames in the animation to 0 
(the error icon is not animated; it is always a static image).
// 以上文档意思就是说,Android5.x以上的版本,机器人的动画是PNG图片和帧动画组成的,
//我们可以使用recovery目录下的interlace-frames.py这个python脚本来进行合成,
//具体的合成方法需要使用recovery代码中的一个python合成工具。
//我就可以把android原生态的动画给换了,因为这个机器人实在是丑。
源码如下:
# Copyright (C) 2014 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Script to take a set of frames (PNG files) for a recovery animation and turn it into a single output image which contains the input frames interlaced by row. Run with the names of all the input frames on the command line, in order, followed by the name of the output file.""" import sys try: import Image import PngImagePlugin except ImportError: print "This script requires the Python Imaging Library to be installed." sys.exit(1) frames = [Image.open(fn).convert("RGB") for fn in sys.argv[1:-1]] assert len(frames) > 0, "Must have at least one input frame." sizes = set() for fr in frames: sizes.add(fr.size) assert len(sizes) == 1, "All input images must have the same size." w, h = sizes.pop() N = len(frames) out = Image.new("RGB", (w, h*N)) for j in range(h): for i in range(w): for fn, f in enumerate(frames): out.putpixel((i, j*N+fn), f.getpixel((i, j))) # When loading this image, the graphics library expects to find a text # chunk that specifies how many frames this animation represents. If # you post-process the output of this script with some kind of # optimizer tool (eg pngcrush or zopflipng) make sure that your # optimizer preserves this text chunk. meta = PngImagePlugin.PngInfo() meta.add_text("Frames", str(N)) out.save(sys.argv[-1], pnginfo=meta) 
这也就是为什么,调用这张图片需要用到LoadBitmapArray这个函数的原因。
void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) { int result = res_create_multi_display_surface(filename, frames, surface); if (result < 0) { LOGE("missing bitmap %s\n(Code %d)\n", filename, result); } }
调完这个函数后会调用resources.cpp中的res_create_multi_display_surface函数用于显示,源码如下:
int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) { GRSurface** surface = NULL; int result = 0; png_structp png_ptr = NULL; png_infop info_ptr = NULL; png_uint_32 width, height; png_byte channels; int i; png_textp text; int num_text; unsigned char* p_row; unsigned int y; *pSurface = NULL; *frames = -1; result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); if (result < 0) return result; *frames = 1; if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { for (i = 0; i < num_text; ++i) { if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { *frames = atoi(text[i].text); break; } } printf(" found frames = %d\n", *frames); } if (height % *frames != 0) { printf("bad height (%d) for frame count (%d)\n", height, *frames); result = -9; goto exit; } surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*))); if (surface == NULL) { result = -8; goto exit; } for (i = 0; i < *frames; ++i) { surface[i] = init_display_surface(width, height / *frames); if (surface[i] == NULL) { result = -8; goto exit; } } #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) png_set_bgr(png_ptr); #endif p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); for (y = 0; y < height; ++y) { png_read_row(png_ptr, p_row, NULL); int frame = y % *frames; unsigned char* out_row = surface[frame]->data + (y / *frames) * surface[frame]->row_bytes; transform_rgb_to_draw(p_row, out_row, channels, width); } free(p_row); *pSurface = reinterpret_cast<GRSurface**>(surface); exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); if (result < 0) { if (surface) { for (i = 0; i < *frames; ++i) { if (surface[i]) free(surface[i]); } free(surface); } } return result; }
其余的和text无关图片,会用到LoadBitmap这个函数:
void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) { int result = res_create_multi_display_surface(filename, frames, surface); if (result < 0) { LOGE("missing bitmap %s\n(Code %d)\n", filename, result); } }
同样调用到以下函数:
int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) { GRSurface** surface = NULL; int result = 0; png_structp png_ptr = NULL; png_infop info_ptr = NULL; png_uint_32 width, height; png_byte channels; int i; png_textp text; int num_text; unsigned char* p_row; unsigned int y; *pSurface = NULL; *frames = -1; result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); if (result < 0) return result; *frames = 1; if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { for (i = 0; i < num_text; ++i) { if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { *frames = atoi(text[i].text); break; } } printf(" found frames = %d\n", *frames); } if (height % *frames != 0) { printf("bad height (%d) for frame count (%d)\n", height, *frames); result = -9; goto exit; } surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*))); if (surface == NULL) { result = -8; goto exit; } for (i = 0; i < *frames; ++i) { surface[i] = init_display_surface(width, height / *frames); if (surface[i] == NULL) { result = -8; goto exit; } } #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) png_set_bgr(png_ptr); #endif p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); for (y = 0; y < height; ++y) { png_read_row(png_ptr, p_row, NULL); int frame = y % *frames; unsigned char* out_row = surface[frame]->data + (y / *frames) * surface[frame]->row_bytes; transform_rgb_to_draw(p_row, out_row, channels, width); } free(p_row); *pSurface = reinterpret_cast<GRSurface**>(surface); exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); if (result < 0) { if (surface) { for (i = 0; i < *frames; ++i) { if (surface[i]) free(surface[i]); } free(surface); } } return result; }
关于图片我们大概都知道怎么来显示的了,所以,现在我们可以替换Android原生态中的图片,换成我们自己的图片,
当然,也不是什么图都可以的,在recovery中,所有的png图片必须是RGB且不带且不能带alhpa通道信息。
关于这一点,我们可以看open_png这个函数:
static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, png_uint_32* width, png_uint_32* height, png_byte* channels) { char resPath[256]; unsigned char header[8]; int result = 0; int color_type, bit_depth; size_t bytesRead; snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); resPath[sizeof(resPath)-1] = '\0'; FILE* fp = fopen(resPath, "rb"); if (fp == NULL) { result = -1; goto exit; } bytesRead = fread(header, 1, sizeof(header), fp); if (bytesRead != sizeof(header)) { result = -2; goto exit; } if (png_sig_cmp(header, 0, sizeof(header))) { result = -3; goto exit; } *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!*png_ptr) { result = -4; goto exit; } *info_ptr = png_create_info_struct(*png_ptr); if (!*info_ptr) { result = -5; goto exit; } if (setjmp(png_jmpbuf(*png_ptr))) { result = -6; goto exit; } png_init_io(*png_ptr, fp); 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, NULL, NULL, NULL); *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); result = -7; goto exit; } return result; exit: if (result < 0) { png_destroy_read_struct(png_ptr, info_ptr, NULL); } if (fp != NULL) { fclose(fp); } return result; }
在代码中,我们可以看到如下:
 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); result = -7; goto exit; }

以下参考一位网友给出的答案。
这个函数将图片文件的数据读取到内存,我在其中输出了一些调试信息,输出图片的 color_type, channels 等信息。
查看LOG发现,android原生的图片 channels == 3,channels 即色彩通道个数,等于 3 的话,意味着只有 R,G,B 三个通道的信息,
没有 ALPHA 通道信息!这段代码的逻辑是如果channels 不等于3, 则按channels = 1 来处理,即灰度图。
美工给的图片是带 alpha通道信息的,即channels = 4,被当成灰度图像来处理了,怪不得显示的效果是灰度图像。
我一直以为 png 图像就只有一种格式,都是带有 alpha通道的。。。
使用图像处理工具(photoshop 或者 gimp),将美工给的图片去掉 alpha 通道信息,再替换recovery 的图片,编译,替换recovery.img ,
reboot -r 。图片终于正常显示啦。

在下一节里,我们将继续分析UI显示....









原文链接:https://yq.aliyun.com/articles/236889
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章