在做下面操作之前,需要先干掉SurfaceFlinger进程,参考:Android中怎么方便的调试关机充电
1.Android.mk文件如下:
# Copyright 2013 The Android Open Source Project
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
main.cpp \
LOCAL_MODULE := minui
LOCAL_MODULE_TAGS := optional
LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
LOCAL_CFLAGS := -D__STDC_LIMIT_MACROS -Werror
LOCAL_C_INCLUDES := bootable/recovery
LOCAL_STATIC_LIBRARIES := \
libbinder \
libminui \
libpng \
libz \
libutils \
libbase \
libcutils \
liblog \
libm \
libc
LOCAL_HAL_STATIC_LIBRARIES := libhealthd
include $(BUILD_EXECUTABLE)
2.源码(main.cpp)如下:
#include "minui/minui.h"
#include <string>
GRSurface* surf_unknown;
static int char_width;
static int char_height;
void healthd_board_mode_charger_set_backlight(bool en);
static int draw_text(const char *str, int x, int y)
{
int str_len_px = gr_measure(gr_sys_font(), str);
if (x < 0)
x = (gr_fb_width() - str_len_px) / 2;
if (y < 0)
y = (gr_fb_height() - char_height) / 2;
gr_text(gr_sys_font(), x, y, str, 0);
return y + char_height;
}
static void android_green(void)
{
gr_color(0xa4, 0xc6, 0x39, 255);
}
static void clear_screen(void)
{
gr_color(0, 0, 0, 255);
gr_clear();
}
/* returns the last y-offset of where the surface ends */
static int draw_surface_centered(GRSurface* surface)
{
int w;
int h;
int x;
int y;
w = gr_get_width(surface);
h = gr_get_height(surface);
x = (gr_fb_width() - w) / 2 ;
y = (gr_fb_height() - h) / 2 ;
printf("drawing surface %dx%d+%d+%d\n", w, h, x, y);
gr_blit(surface, 0, 0, w, h, x, y);
return y + h;
}
static void draw_picture(void)
{
int ret = -1;
std::string animation_file;
animation_file.assign("charger/battery_fail");
ret = res_create_display_surface(animation_file.c_str(), &surf_unknown);
if(ret < 0)
{
printf("surface %s create failed\n",animation_file.c_str());
}
draw_surface_centered(surf_unknown);
}
int main(void)
{
gr_init();
gr_font_size(gr_sys_font(), &char_width, &char_height);
clear_screen();
healthd_board_mode_charger_set_backlight(true);
android_green();
//draw_text("Hello World!", -1, -1);
draw_picture();
gr_flip();
printf("run anim\n");
while(1);
return 0;
}
这里需要指出的是res_create_display_surface寻找的资源文件的目录是/res/image
,我们这儿使用的图片完整路径如下:/res/images/charger/battery_fail
3.显示效果: