LVGL8的窗口切换 (转)
-
-
转载: LVGL8的窗口切换方式
LVGL8实现两个窗口的切换方式:
方法一:切换到窗口2时,删除窗口1的容器,这样可以删除窗口1下的所有子对象,这种方式适用于运存较小的设备方法二:显示窗口1之前给窗口2的容器设置LV_OBJ_FLAG_HIDDEN隐藏属性,显示窗口2之前给窗口1的容器设置LV_OBJ_FLAG_HIDDEN隐藏属性
还是通过codeblock来模拟代码的运行,代码如下:
#define HIDDEN_WIN (0) static lv_obj_t *win1_contanier = NULL; static lv_obj_t *win2_contanier = NULL; static void win_btn_event_callback1(lv_event_t* event); static void win_btn_event_callback2(lv_event_t* event); static void show_button_win1() { static lv_style_t obj_style; lv_style_reset(&obj_style); lv_style_init(&obj_style); // 初始化按钮样式 lv_style_set_radius(&obj_style, 0); // 设置样式圆角弧度 lv_obj_t * btn =NULL; win1_contanier = lv_obj_create(lv_scr_act()); // 基于屏幕创建一个和屏幕大小一样的容器win1_contanier if (win1_contanier != NULL) { lv_obj_set_style_bg_color(win1_contanier, lv_palette_main(LV_PALETTE_RED), 0); lv_obj_set_size(win1_contanier, 800, 480); lv_obj_add_style(win1_contanier, &obj_style, 0); btn = lv_btn_create(win1_contanier); if (btn != NULL) { lv_obj_set_size(btn, 100, 50); lv_obj_add_event_cb(btn, win_btn_event_callback1, LV_EVENT_ALL, NULL); // 给对象添加CLICK事件和事件处理回调函数 lv_obj_center(btn); lv_obj_t *label = lv_label_create(btn); // 给按钮添加标签 if (label != NULL) { lv_label_set_text(label, "Button1"); // 设置标签文本 lv_obj_center(label); // 标签居中显示 } } } } static void show_button_win2() { static lv_style_t obj_style; lv_style_reset(&obj_style); lv_style_init(&obj_style); // 初始化按钮样式 lv_style_set_radius(&obj_style, 0); // 设置样式圆角弧度 lv_obj_t * btn =NULL; win2_contanier = lv_obj_create(lv_scr_act()); // 基于屏幕创建一个和屏幕大小一样的容器win2_contanier if (win2_contanier != NULL) { lv_obj_set_style_bg_color(win2_contanier, lv_palette_main(LV_PALETTE_BLUE), 0); lv_obj_set_size(win2_contanier, 800, 480); lv_obj_add_style(win2_contanier, &obj_style, 0); btn = lv_btn_create(win2_contanier); if (btn != NULL) { lv_obj_set_size(btn, 100, 50); lv_obj_add_event_cb(btn, win_btn_event_callback2, LV_EVENT_ALL, NULL); // 给对象添加CLICK事件和事件处理回调函数 lv_obj_center(btn); lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_RED), 0); lv_obj_t *label = lv_label_create(btn); // 给按钮添加标签 if (label != NULL) { lv_label_set_text(label, "Button2"); // 设置标签文本 lv_obj_center(label); // 标签居中显示 } } } } static void win_btn_event_callback1(lv_event_t* event) { lv_event_code_t code = lv_event_get_code(event); if (code == LV_EVENT_CLICKED) { #if HIDDEN_WIN // 给窗口1的容器添加隐藏属性,清除窗口2的隐藏属性 lv_obj_add_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN); #else // 删除窗口1的win1_contanier容器,这样就可以把win1_contanier上的子对象全部删除,然后显示窗口2 lv_obj_del(win1_contanier); show_button_win2(); #endif } } static void win_btn_event_callback2(lv_event_t* event) { lv_event_code_t code = lv_event_get_code(event); if (code == LV_EVENT_CLICKED) { #if HIDDEN_WIN // 给窗口2的容器添加隐藏属性,清除窗口1的隐藏属性 lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN); #else // 删除窗口2的win2_contanier容器,这样就可以把win2_contanier上的子对象全部删除,然后显示窗口1 lv_obj_del(win2_contanier); show_button_win1(); #endif } } void lv_win_switch_main() { show_button_win1(); #if HIDDEN_WIN show_button_win2(); // 给窗口2的容器添加隐藏属性 lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN); #endif }
-
#include "lvgl/lvgl.h" #define HIDDEN_WIN (0) static lv_obj_t *win1_contanier = NULL; static lv_obj_t *win2_contanier = NULL; static void win_btn_event_callback1(lv_event_t* event); static void win_btn_event_callback2(lv_event_t* event); static void show_button_win1() { static lv_style_t obj_style; lv_style_reset(&obj_style); lv_style_init(&obj_style); // 初始化按钮样式 lv_style_set_radius(&obj_style, 0); // 设置样式圆角弧度 lv_obj_t * btn = NULL; lv_obj_t * label1 = NULL; win1_contanier = lv_obj_create(lv_scr_act()); // 基于屏幕创建一个和屏幕大小一样的容器win1_contanier if (win1_contanier != NULL) { lv_obj_set_style_bg_color(win1_contanier, lv_palette_main(LV_PALETTE_RED), 0); lv_obj_set_size(win1_contanier, 800, 480); lv_obj_add_style(win1_contanier, &obj_style, 0); btn = lv_btn_create(win1_contanier); label1 = lv_label_create(win1_contanier); if (btn != NULL) { lv_obj_set_size(btn, 100, 50); //lv_obj_set_pos(btn, 100, 100); lv_obj_add_event_cb(btn, win_btn_event_callback1, LV_EVENT_ALL, NULL); // 给对象添加CLICK事件和事件处理回调函数 lv_obj_center(btn); lv_obj_align_to(label1, btn, LV_ALIGN_OUT_TOP_LEFT, 0, -10); lv_label_set_text(label1, "This is window1"); lv_obj_t *label = lv_label_create(btn); // 给按钮添加标签 if (label != NULL) { lv_label_set_text(label, "Button1"); // 设置标签文本 lv_obj_center(label); // 标签居中显示 } } } } static void show_button_win2() { static lv_style_t obj_style; lv_style_reset(&obj_style); lv_style_init(&obj_style); // 初始化按钮样式 lv_style_set_radius(&obj_style, 0); // 设置样式圆角弧度 lv_obj_t * btn =NULL; lv_obj_t * label2 = NULL; win2_contanier = lv_obj_create(lv_scr_act()); // 基于屏幕创建一个和屏幕大小一样的容器win2_contanier if (win2_contanier != NULL) { lv_obj_set_style_bg_color(win2_contanier, lv_palette_main(LV_PALETTE_BLUE), 0); lv_obj_set_size(win2_contanier, 800, 480); lv_obj_add_style(win2_contanier, &obj_style, 0); btn = lv_btn_create(win2_contanier); label2 = lv_label_create(win1_contanier); if (btn != NULL) { lv_obj_set_size(btn, 100, 50); lv_obj_add_event_cb(btn, win_btn_event_callback2, LV_EVENT_ALL, NULL); // 给对象添加CLICK事件和事件处理回调函数 lv_obj_center(btn); lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_RED), 0); lv_obj_t *label = lv_label_create(btn); // 给按钮添加标签 lv_obj_align_to(label2, btn, LV_ALIGN_OUT_TOP_LEFT, 0, -20); lv_label_set_text(label2, "This is window2"); if (label != NULL) { lv_label_set_text(label, "Button2"); // 设置标签文本 lv_obj_center(label); // 标签居中显示 } } } } static void win_btn_event_callback1(lv_event_t* event) { lv_event_code_t code = lv_event_get_code(event); if (code == LV_EVENT_CLICKED) { #if HIDDEN_WIN // 给窗口1的容器添加隐藏属性,清除窗口2的隐藏属性 lv_obj_add_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN); #else // 删除窗口1的win1_contanier容器,这样就可以把win1_contanier上的子对象全部删除,然后显示窗口2 lv_obj_del(win1_contanier); show_button_win2(); #endif } } static void win_btn_event_callback2(lv_event_t* event) { lv_event_code_t code = lv_event_get_code(event); if (code == LV_EVENT_CLICKED) { #if HIDDEN_WIN // 给窗口2的容器添加隐藏属性,清除窗口1的隐藏属性 lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(win1_contanier, LV_OBJ_FLAG_HIDDEN); #else // 删除窗口2的win2_contanier容器,这样就可以把win2_contanier上的子对象全部删除,然后显示窗口1 lv_obj_del(win2_contanier); show_button_win1(); #endif } } void lv_win_switch_main() { show_button_win1(); #if HIDDEN_WIN show_button_win2(); // 给窗口2的容器添加隐藏属性 lv_obj_add_flag(win2_contanier, LV_OBJ_FLAG_HIDDEN); #endif }
添加 label 后的代码
-
@whycan 如果窗口顶层对象是lv_obj_create(NULL)创建的对象,直接用lv_scr_load_anim和lv_scr_load加载,lv_scr_load_anim可以指定是否删除原来的窗口对象,lv_scr_load不删除原来的。
-
Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号