我是靠谱客的博主 无私滑板,最近开发中收集的这篇文章主要介绍LVGL 8.2 菜单,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

定义及声明

enum {
    LV_MENU_ITEM_BUILDER_VARIANT_1,
    LV_MENU_ITEM_BUILDER_VARIANT_2
};   
typedef uint8_t lv_menu_builder_variant_t;
static void back_event_handler(lv_event_t* e);
static void switch_handler(lv_event_t* e);
lv_obj_t* root_page;
static lv_obj_t* create_text(lv_obj_t* parent, const char* icon, const char* txt,
    lv_menu_builder_variant_t builder_variant);
static lv_obj_t* create_slider(lv_obj_t* parent,
    const char* icon, const char* txt, int32_t min,int32_t max, int32_t val);
static lv_obj_t* create_switch(lv_obj_t* parent,
    const char* icon, const char* txt, bool chk);

返回键事件处理

static void back_event_handler(lv_event_t* e)
{
    lv_obj_t* obj = lv_event_get_target(e);  // 获取产生事件的对象
    lv_obj_t* menu = lv_event_get_user_data(e);  // 获取用户数据
    if (lv_menu_back_btn_is_root(menu, obj)) { // 根菜单的返回键
        lv_obj_t* mbox1 = lv_msgbox_create(NULL, "Hello", "Root back btn click.", NULL, true);  //创建消息对话框
        lv_obj_center(mbox1);  // 居中显示
    }
}

switch键事件处理

static void switch_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);  //获取对象产生的事件码
    lv_obj_t* menu = lv_event_get_user_data(e); // 获取用户数据
    lv_obj_t* obj = lv_event_get_target(e); // 获取产生事件的对象
    if (code == LV_EVENT_VALUE_CHANGED) {  // LV_EVENT_VALUE_CHANGED事件处理
        if (lv_obj_has_state(obj, LV_STATE_CHECKED)) { // 选中处理
            lv_menu_set_page(menu, NULL);   // 清除主菜单界面显示
            lv_menu_set_sidebar_page(menu, root_page);// Set menu page to display in sidebar
            lv_event_send(lv_obj_get_child(lv_obj_get_child(lv_menu_get_cur_sidebar_page(menu), 0), 0), LV_EVENT_CLICKED, NULL);
        }
        else {  // 未选中处理
            lv_menu_set_sidebar_page(menu, NULL); //取消菜单sidebar显示
            lv_menu_clear_history(menu); /* Clear history because we will be showing␣
            ,→the root page later */
            lv_menu_set_page(menu, root_page); // 设置菜单主界面
        }
    }
}

创建文本

static lv_obj_t* create_text(lv_obj_t* parent, const char* icon, const char* txt,
    lv_menu_builder_variant_t builder_variant)
{
    lv_obj_t* obj = lv_menu_cont_create(parent);  // 创建菜单cont容器对象
    lv_obj_t* img = NULL;
    lv_obj_t* label = NULL;
    if (icon) { 
        img = lv_img_create(obj);  // 创建图片对象
        lv_img_set_src(img, icon);  // 设置图片显示资源
    }
    if (txt) {
        label = lv_label_create(obj);   //创建label
        lv_label_set_text(label, txt);  //设置label显示内容
        lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR); //循环滚动模式
        lv_obj_set_flex_grow(label, 1);
    }
    if (builder_variant == LV_MENU_ITEM_BUILDER_VARIANT_2 && icon && txt) {
        lv_obj_add_flag(img, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK); // 添加LV_OBJ_FLAG_FLEX_IN_NEW_TRACK标志
        lv_obj_swap(img, label); // Swap the positions of two objects
    }
    return obj;
}

创建slider

static lv_obj_t* create_slider(lv_obj_t* parent, const char* icon, const char* txt, int32_t min, int32_t max,
    int32_t val)
{
    lv_obj_t* obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_2); // 创建自定义text
    lv_obj_t* slider = lv_slider_create(obj);  // 创建slider对象
    lv_obj_set_flex_grow(slider, 1);
    lv_slider_set_range(slider, min, max); // Sets the width or height (on main axis) to grow the object in order fill the free space
    lv_slider_set_value(slider, val, LV_ANIM_OFF); // 设置slider的值
    if (icon == NULL) {
        lv_obj_add_flag(slider, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK); // 添加LV_OBJ_FLAG_FLEX_IN_NEW_TRACK标志
    }
    return obj;
}

创建switch

static lv_obj_t* create_switch(lv_obj_t* parent, const char* icon, const char* txt, bool chk)
{
    lv_obj_t* obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_1); // 创建自定义text
    lv_obj_t* sw = lv_switch_create(obj);  // 创建switch开关
    lv_obj_add_state(sw, chk ? LV_STATE_CHECKED : 0); // 添加事件
    return obj;
}

Complex Menu

static void lv_example_menu_5(void)
{
    lv_obj_t* menu = lv_menu_create(lv_scr_act());  // 创建菜单对象
    lv_color_t bg_color = lv_obj_get_style_bg_color(menu, 0);  //获取菜单背景色
    if (lv_color_brightness(bg_color) > 127) {  // 背景颜色亮度大于127
        lv_obj_set_style_bg_color(menu, lv_color_darken(lv_obj_get_style_bg_color(menu, 0), 10), 0); //设置背景颜色
    }
    else {
        lv_obj_set_style_bg_color(menu, lv_color_darken(lv_obj_get_style_bg_color(menu, 0), 50), 0);//设置背景颜色
    }
    lv_menu_set_mode_root_back_btn(menu, LV_MENU_ROOT_BACK_BTN_ENABLED);// 使能根菜单返回键
    lv_obj_add_event_cb(menu, back_event_handler, LV_EVENT_CLICKED, menu);//添加返回键点击事件
    lv_obj_set_size(menu, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));// 设置大小
    lv_obj_center(menu); // 居中显示
    lv_obj_t* cont;
    lv_obj_t* section;
    /*Create sub pages*/
    lv_obj_t* sub_mechanics_page = lv_menu_page_create(menu, NULL); //创建菜单界面
    lv_obj_set_style_pad_hor(sub_mechanics_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
    lv_menu_separator_create(sub_mechanics_page);  //menu separator
    section = lv_menu_section_create(sub_mechanics_page); // Create a menu section object
    create_slider(section, LV_SYMBOL_SETTINGS, "Velocity", 0, 150, 120); // 创建Velocity slider
    create_slider(section, LV_SYMBOL_SETTINGS, "Acceleration", 0, 150, 50);// 创建Acceleration slider
    create_slider(section, LV_SYMBOL_SETTINGS, "Weight limit", 0, 150, 80); // 创建Weight limit slider
    lv_obj_t* sub_sound_page = lv_menu_page_create(menu, NULL); // 创建sub_sound_page菜单界面
    lv_obj_set_style_pad_hor(sub_sound_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);//设置水平PAD间距
    lv_menu_separator_create(sub_sound_page);   //menu separator
    section = lv_menu_section_create(sub_sound_page);   // Create a menu section object
    create_switch(section, LV_SYMBOL_AUDIO, "Sound", false); // 创建Sound switch开关
    lv_obj_t* sub_display_page = lv_menu_page_create(menu, NULL);  // 创建sub_display_page 菜单界面
    lv_obj_set_style_pad_hor(sub_display_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
    lv_menu_separator_create(sub_display_page); //menu separator
    section = lv_menu_section_create(sub_display_page); // Create a menu section object
    create_slider(section, LV_SYMBOL_SETTINGS, "Brightness", 0, 150, 100);  // 创建 Brightness slider

    lv_obj_t* sub_software_info_page = lv_menu_page_create(menu, NULL); // 创建sub_software_info_page 菜单界面
    lv_obj_set_style_pad_hor(sub_software_info_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
    section = lv_menu_section_create(sub_software_info_page); // Create a menu section object
    create_text(section, NULL, "Version 1.0", LV_MENU_ITEM_BUILDER_VARIANT_1);// 创建显示版本文本"Version 1.0"
    lv_obj_t* sub_legal_info_page = lv_menu_page_create(menu, NULL); // 创建sub_legal_info_page 菜单界面
    lv_obj_set_style_pad_hor(sub_legal_info_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);  //设置水平PAD间距
    section = lv_menu_section_create(sub_legal_info_page);// Create a menu section object
    for (uint32_t i = 0; i < 15; i++) {
        create_text(section, NULL,
            "This is a long long long long long long long long long text, if it is long enough it may scroll.",
            LV_MENU_ITEM_BUILDER_VARIANT_1);
    }  //创建15行文本
    lv_obj_t* sub_about_page = lv_menu_page_create(menu, NULL);// 创建sub_about_page 菜单界面
    lv_obj_set_style_pad_hor(sub_about_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
    lv_menu_separator_create(sub_about_page);  //menu separator
    section = lv_menu_section_create(sub_about_page); // Create a menu section object
    cont = create_text(section, NULL, "Software information", LV_MENU_ITEM_BUILDER_VARIANT_1); // Software information项
    lv_menu_set_load_page_event(menu, cont, sub_software_info_page);  //加载cont到menu,设置跳转界面sub_software_info_page

    cont = create_text(section, NULL, "Legal information", LV_MENU_ITEM_BUILDER_VARIANT_1);  // Legal information项
    lv_menu_set_load_page_event(menu, cont, sub_legal_info_page);  //加载cont到menu,设置跳转界面sub_legal_info_page
    lv_obj_t* sub_menu_mode_page = lv_menu_page_create(menu, NULL);  // 创建sub_menu_mode_page 菜单界面
    lv_obj_set_style_pad_hor(sub_menu_mode_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0);//设置水平PAD间距
    lv_menu_separator_create(sub_menu_mode_page);  //menu separator
    section = lv_menu_section_create(sub_menu_mode_page); // Create a menu section object
    cont = create_switch(section, LV_SYMBOL_AUDIO, "Sidebar enable", true); // 创建Sidebar enable开关
    lv_obj_add_event_cb(lv_obj_get_child(cont, 2), switch_handler, LV_EVENT_VALUE_CHANGED, menu); // 添加LV_EVENT_VALUE_CHANGED事件
    /*Create a root page*/
    root_page = lv_menu_page_create(menu, "Settings"); // 创建Settings 菜单界面
    lv_obj_set_style_pad_hor(root_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(menu), 0), 0); //设置水平PAD间距
    section = lv_menu_section_create(root_page); // Create a menu section object
    cont = create_text(section, LV_SYMBOL_SETTINGS, "Mechanics", LV_MENU_ITEM_BUILDER_VARIANT_1);  // 创建Mechanics菜单子项
    lv_menu_set_load_page_event(menu, cont, sub_mechanics_page);//加载cont到menu,设置跳转界面sub_mechanics_page
    cont = create_text(section, LV_SYMBOL_AUDIO, "Sound", LV_MENU_ITEM_BUILDER_VARIANT_1);  // 创建Sound菜单子项
    lv_menu_set_load_page_event(menu, cont, sub_sound_page); //加载cont到menu,设置跳转界面sub_sound_page
    cont = create_text(section, LV_SYMBOL_SETTINGS, "Display", LV_MENU_ITEM_BUILDER_VARIANT_1); // 创建Sound菜单子项
    lv_menu_set_load_page_event(menu, cont, sub_display_page);//加载cont到menu,设置跳转界面sub_display_page
    create_text(root_page, NULL, "Others", LV_MENU_ITEM_BUILDER_VARIANT_1); // 创建Others菜单子项
    section = lv_menu_section_create(root_page); // Create a menu section object
    cont = create_text(section, NULL, "About", LV_MENU_ITEM_BUILDER_VARIANT_1); // 创建About菜单子项
    lv_menu_set_load_page_event(menu, cont, sub_about_page);//加载cont到menu,设置跳转界面sub_about_page
    cont = create_text(section, LV_SYMBOL_SETTINGS, "Menu mode", LV_MENU_ITEM_BUILDER_VARIANT_1);// 创建Menu mode菜单子项
    lv_menu_set_load_page_event(menu, cont, sub_menu_mode_page);//加载cont到menu,设置跳转界面sub_menu_mode_page
    lv_menu_set_sidebar_page(menu, root_page);// 设置菜单sidebar风格显示
    lv_event_send(lv_obj_get_child(lv_obj_get_child(lv_menu_get_cur_sidebar_page(menu), 0), 0), LV_EVENT_CLICKED, NULL); //发送点击事件
}

运行效果

在这里插入图片描述

最后

以上就是无私滑板为你收集整理的LVGL 8.2 菜单的全部内容,希望文章能够帮你解决LVGL 8.2 菜单所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(30)

评论列表共有 0 条评论

立即
投稿
返回
顶部