在lvgl使用的过程中,最初的时候,肯定都会遇到这样的问题,页面之间的切换以及空间的释放。如果不合理的设计的话,一上来就将所有的页面进行初始化,那将会占用许多不必要的内存空间,硬写之间的切换的话,界面一旦多起来,那切换逻辑将会绕来绕去,最终会写不下去。
所以合理设计一个页面管理函数或者说是对象将会非常重要。
欢迎关注 !!!!!!!
基于上面三点就可以完成一个基本的页面管理对象了,是不是非常简单。
typedef struct
{
unsigned char pageID; //pageID 每个页面有唯一的ID
lv_obj_t* page; //界面
lv_scr_load_anim_t load_anim;
void (*init)(lv_obj_t* page); //页面初始化
void (*exit)(lv_obj_t* page); //退出动画
}PAGE;
class PageManage
{
public:
PageManage(PAGE *home_page, void (*init)(lv_obj_t* page)); //初始化,并且设置home界面
~PageManage();
void page_manage_add_page(PAGE* page,unsigned char id, lv_scr_load_anim_t anim, void (*init)(lv_obj_t* page));
void page_manage_switch_page(unsigned char id);
private:
PAGE* curr_page;
vector<PAGE *> pagelist;
};
void PageManage::page_manage_add_page(PAGE* page, unsigned char id, lv_scr_load_anim_t anim, void (*init)(lv_obj_t* page))
{
page->pageID = id;
page->page = lv_obj_create(NULL);
page->load_anim = anim;
page->init = init;
pagelist.push_back(page);
}
void PageManage::page_manage_switch_page(unsigned char id)
{
unsigned char i;
for (i = 0; i < pagelist.size(); i++)
{
PAGE* page = pagelist[i];
if (page->pageID == id) //根据ID查找页面
{
page->init(page->page); //初始化界面
lv_scr_load_anim(page->page, page->load_anim, 800, 0, false); //加载界面
if(curr_page != NULL)
lv_obj_clean(curr_page->page); //清除界面上的内容,释放空间
curr_page = page; //更新当前页
break;
}
}
}
至此一个简单的页面管理就这样完成了,可以满足大部分要求了。在main函数中添加页类似于,可以在此基础上进行进一步的完善。
PAGE home_page;
PAGE ble_page;
PAGE clock_page;
PAGE game_page;
PAGE photo_page;
PageManage manage(&home_page, ui_wifiset_init); //默认ID为0
manage.page_manage_add_page(&ble_page, 4, LV_SCR_LOAD_ANIM_MOVE_LEFT,BLE_HID_Page_Init);
manage.page_manage_add_page(&clock_page, 2, LV_SCR_LOAD_ANIM_FADE_ON, Clock_Page_Init);
manage.page_manage_add_page(&game_page,1, LV_SCR_LOAD_ANIM_FADE_ON, _2048_Page_Init);
manage.page_manage_add_page(&photo_page,3, LV_SCR_LOAD_ANIM_OVER_TOP, Photo_Page_Init);
manage.page_manage_switch_page(0); //添加页面0
完整代码如下
#ifndef __PAGE_MANAGE_
#define __PAGE_MANAGE_
#include "../lvgl/lvgl.h"
#include
#include
using namespace std;
typedef struct
{
unsigned char pageID; //pageID 每个页面有唯一的ID
lv_obj_t* page; //界面
lv_scr_load_anim_t load_anim;
void (*init)(lv_obj_t* page); //页面初始化
void (*exit)(lv_obj_t* page); //退出动画
}PAGE;
class PageManage
{
public:
PageManage(PAGE *home_page, void (*init)(lv_obj_t* page)); //初始化,并且设置home界面
~PageManage();
void page_manage_add_page(PAGE* page,unsigned char id, lv_scr_load_anim_t anim, void (*init)(lv_obj_t* page));
void page_manage_switch_page(unsigned char id);
private:
PAGE* curr_page;
vector<PAGE *> pagelist;
};
#endif
--------------------------------------------------------------------
#include "Page_Manage.h"
PageManage::PageManage(PAGE* home_page, void (*init)(lv_obj_t* page))
{
home_page->page = lv_obj_create(NULL);
home_page->load_anim = LV_SCR_LOAD_ANIM_MOVE_LEFT;
home_page->pageID = 0;
home_page->init = init;
//curr_page = home_page;
curr_page = NULL;
//init(home_page->page); //初始化home_page
pagelist.push_back(home_page);
}
PageManage::~PageManage()
{
}
void PageManage::page_manage_add_page(PAGE* page, unsigned char id, lv_scr_load_anim_t anim, void (*init)(lv_obj_t* page))
{
page->pageID = id;
page->page = lv_obj_create(NULL);
page->load_anim = anim;
page->init = init;
//init(page->page);
pagelist.push_back(page);
}
void PageManage::page_manage_switch_page(unsigned char id)
{
unsigned char i;
for (i = 0; i < pagelist.size(); i++)
{
PAGE* page = pagelist[i];
if (page->pageID == id)
{
page->init(page->page);
lv_scr_load_anim(page->page, page->load_anim, 800, 0, false);
if(curr_page != NULL)
lv_obj_clean(curr_page->page);
curr_page = page;
break;
}
}
}
回复 LVGL页面管理 获取完成代码
关注↓↓↓