首先找一个git动画文件 【转成逐帧png】,或者连续几张图片,然后在Online image converter - BMP, JPG or PNG to C array or binary | LVGL 网页上将图片转成C文件数组数据。
Lvgl_image_convert_tool: 基于LVGl图片转换离线版封装的小工具,不仅有界面,还可以一键生成到项目里哦 (gitee.com)
这里用经典的太空人gif图。 我提取了其中的7张图片转成文件 taikong_a1.c~~taikong_a7.c
将这些文件导入工程。在初始化的文件中添加代码 lvgl_v8.0+
#define MAX_IMGS_NUM 7 //定义动画帧的图片量
static const lv_img_dsc_t* my_anim_imgs[MAX_IMGS_NUM] =
{
&taikong_a1,
&taikong_a2,
&taikong_a3,
&taikong_a4,
&taikong_a5,
&taikong_a6,
&taikong_a7
};
//方案1:初始化函数中调用 my_anim_imgs() 即可。
void astronauts_animing(void)
{
lv_obj_t* animimg1 = lv_animimg_create(lv_scr_act()); //动画1
lv_obj_center(animimg1); //居中
lv_animimg_set_src(animimg1, (lv_img_decoder_t**)my_anim_imgs,MAX_IMGS_NUM );
lv_animimg_set_duration(animimg1,500);
lv_animimg_set_repeat_count(animimg1,LV_ANIM_REPEAT_INFINITE);
lv_animimg_start( animimg1 );
}
//方案2:
void loop_display()
{
int index = 0;
lv_obj_t* parent_obj = lv_scr_act();
lv_obj_t* img_obj = lv_img_create(parent_obj);
while(1)
{
lv_img_set_src(img_obj,my_anim_imgs[index]);
lv_timer_handler();
if(++index>MAX_IMGS_NUM)
{
index = 0;
vTaskDelay(pdMS_TO_TICKS(1500));
}
else
vTaskDelay(pdMS_TO_TICKS(100));
}
}
//***************一些常用函数扩展:*****************//
//1) 弹出消息框,调用如lv_msgbox_show("TestMsg!")
static void msgbox_event_callback(lv_event_t* event)
{
lv_event_code_t code = lv_event_get_code(event);
lv_obj_t* msgbox = lv_event_get_current_target(event);
if ((code == LV_EVENT_VALUE_CHANGED) && (msgbox != NULL))
{
const char* txt = lv_msgbox_get_active_btn_text(msgbox);
if (strcmp(txt, "OK") == 0)
{
lv_msgbox_close(msgbox);
}
}
}
void lv_msgbox_show(char * msgStr )
{
static const char* button_array[] = { "OK" };
// 创建带按钮
lv_obj_t* msg_box = lv_msgbox_create(NULL, "---------", msgStr, button_array, false);
if (msg_box != NULL )
{
lv_obj_add_event_cb(msg_box, msgbox_event_callback, LV_EVENT_ALL, NULL);
lv_obj_align(msg_box, LV_ALIGN_CENTER, 0, 0);
}
}
2)在窗口顶部做Toast显示,调用 lv_toast_show("TestMsg!",true); 第二参数为是否需要长时间显示
void task_calcBox_cb(lv_timer_t* tmr)
{
lv_obj_t* msg_box = (lv_obj_t*)tmr->user_data;
if (msg_box == NULL)
{
return;
}
int leftCnt = tmr->repeat_count;
if ( leftCnt > 15)
{ //when value<=15 start,for stop it
return;
}
static int offsetPos = 10;
lv_obj_align(msg_box, LV_ALIGN_TOP_MID, 0, offsetPos);
offsetPos -= 5;
if (leftCnt == 0)
{
lv_msgbox_close(msg_box);
offsetPos = 10;//restore origin
}
}
void lv_toast_show(char* msgStr,bool bStayLong)
{
lv_obj_t* msg_box = lv_msgbox_create(NULL, "", msgStr, NULL, false);
int cnt_times = 15;
if (bStayLong)
cnt_times = 30;
if (msg_box != NULL)
{
lv_obj_align(msg_box, LV_ALIGN_TOP_MID, 0, 0);
lv_timer_t* taskTimer = lv_timer_create(task_calcBox_cb, 100, msg_box);
lv_timer_set_repeat_count(taskTimer, cnt_times);//count_time set
}
}
//---------------------------
void set_screen_bg_color(uint32_t bg_color)
{
static lv_style_t style ;
static bool bFistInit = false;
if( !bFistInit ){
bFistInit = true;
lv_style_init(&style);
lv_obj_add_style(lv_scr_act(),&style,0);
}
lv_style_set_bg_color(&style,lv_color_hex(bg_color) );
lv_obj_refresh_style(lv_scr_act(), &style, 0);
}