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

简介:        http://blog.csdn.net/morixinguan/article/details/72858346引用我的代码片 在上一篇文章中,我们大致的介绍了recovery的启动流程,那么,recovery升级或者做双清的时候,那些图形动画又是如何实现的呢?我们来看看代码:      以下这段代码位于recovery/screen_ui.

       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显示....









目录
相关文章
|
3天前
|
IDE 开发工具 Android开发
安卓与iOS开发环境对比分析
在移动应用开发的广阔舞台上,安卓与iOS这两大操作系统各占半壁江山。它们在开发环境上的差异,不仅影响了开发者的编码体验,也在一定程度上塑造了应用生态的多样性。本文将深入探讨两者在开发工具、编程语言、用户界面设计以及市场分布等方面的不同特点,为即将踏入这一领域的开发者提供一盏明灯。
|
4天前
|
IDE 开发工具 Android开发
安卓与iOS开发环境的差异性分析
在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各据一方,引领着市场潮流。它们各自拥有独特的开发环境和工具集,为开发者提供了不同的挑战与机遇。本文旨在深入剖析这两个平台的开发环境,通过比较它们的编程语言、集成开发环境(IDE)、用户界面设计、以及系统架构等方面,揭示各自的优势与局限。我们将探讨如何基于这些差异来优化开发策略,并预测未来可能的发展趋势,以期为开发者在选择平台时提供有价值的参考。
|
4天前
|
API 开发工具 Android开发
安卓与iOS开发环境对比分析
移动操作系统的两大巨头,安卓和iOS,各自拥有独特的开发环境和工具。本文将深入探讨两者的开发环境差异,从编程语言、开发工具、用户界面设计、API支持以及生态系统五个维度进行比较分析。通过数据支撑和案例研究,揭示各自的优势和局限性,为开发者选择适合自己项目需求的平台提供参考依据。
13 1
|
7天前
|
Java Android开发 iOS开发
探索安卓与iOS开发的差异:平台特性与用户体验的对比分析
【7月更文挑战第19天】在移动开发的广阔天地中,安卓与iOS两大阵营各据一方,它们在开发环境、用户界面设计、性能优化等方面展现出独特的魅力与挑战。本文旨在深入探讨这两个平台在技术开发和用户体验上的根本差异,并分析这些差异如何影响开发者的策略和最终用户的选择。通过比较两者的编程语言、工具、框架以及设计理念,我们将揭示各自平台的优势与局限,为开发者提供实用的参考,并为消费者呈现一个更加清晰的平台选择视角。
|
9天前
|
安全 开发工具 Android开发
安卓与iOS开发环境对比分析
本文通过深入探讨和比较安卓与iOS两大主流移动操作系统的开发环境,旨在为开发者提供一个全面的视角。我们将从开发工具、编程语言、用户界面设计、性能优化、安全性考量等多个维度进行细致分析,揭示各自平台的优势与挑战。通过统计数据支持的实证研究,本文将展示两个系统在实际应用中的技术差异及其对项目开发周期的影响,并基于市场数据评估各自的商业潜力。
15 1
|
1天前
|
IDE 开发工具 Android开发
安卓与iOS开发环境对比分析:选择适合自己的编程平台
移动应用开发的两大阵营——安卓和iOS,各自拥有不同的开发环境和工具集。本文通过深入比较这两个平台的编程语言、集成开发环境(IDE)、用户界面设计、测试框架以及部署流程,旨在为开发者提供一个全面的视角来选择最符合个人或项目需求的开发环境。
|
7天前
|
Java Android开发 iOS开发
探索安卓与iOS开发的差异:平台特性与用户体验的对比分析
在移动应用开发的广阔天地中,安卓与iOS两大阵营各自占据着半壁江山。本文将深入探讨这两个平台在开发环境、编程语言、用户界面设计以及性能优化方面的差异,并分析这些差异如何影响最终的用户体验。通过数据支持的案例分析和最新的市场研究,我们将揭示开发者如何在这两个不同的生态系统中做出战略决策,以及这些决策对应用成功的潜在影响。
|
2月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
|
2月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
|
2月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍