当前位置:网站首页>lvgl 实现状态提示图标自动对齐补位显示
lvgl 实现状态提示图标自动对齐补位显示
2022-08-05 10:38:00 【*_仰望星空_*】
实现思路
- 通过数组索引表示状态图标or文字的位置
- 定时更新图标在数组的位置,然后再进行图标对齐,数组第n个元素向第n-1个元素对齐
具体实现
1、数据结构
struct state_prompts_pos {
lv_obj_t* prompts;
};
static struct state_prompts_pos _state_prompts_pos[5] = {
{
.prompts = NULL},
{
.prompts = NULL},
{
.prompts = NULL},
{
.prompts = NULL},
{
.prompts = NULL},
};
#define STATE_PROMPTS_SIZE(_state_prompts) (sizeof(_state_prompts)/sizeof(_state_prompts[0]))
- lvgl用lv_obj_t表示元素,所以数组元素类型定义为lv_obj_t
- 数组的每一位表示一个状态。
2、向数组插入状态图标
static void state_prompts_set_prompts(lv_obj_t* prompts)
{
/* There has been a prompts */
for (char i = 0; i < STATE_PROMPTS_SIZE(_state_prompts_pos); i++)
{
if (_state_prompts_pos[i].prompts == prompts)
{
return;
}
}
for (char i = 0; i < STATE_PROMPTS_SIZE(_state_prompts_pos); i++)
{
if (_state_prompts_pos[i].prompts == NULL) /* find a free place */
{
_state_prompts_pos[i].prompts = prompts;
lv_obj_set_hidden(prompts, false);
//printf("set %d\r\n", i);
break;
}
}
}
- 需要保持obj在数组中的唯一性,插入的obj前是否已经在数组中
lv_obj_set_hidden(prompts, false)
,图标已经提前创建好,所以直接取消隐藏显示出来
3、从数组中删除obj
static void state_prompts_reset_prompts(lv_obj_t* prompts)
{
for (char i = 0; i < STATE_PROMPTS_SIZE(_state_prompts_pos); i++)
{
if (_state_prompts_pos[i].prompts == prompts)
{
_state_prompts_pos[i].prompts = NULL;
lv_obj_set_hidden(prompts, true);
//printf("reset %d\r\n", i);
break;
}
}
}
if (_state_prompts_pos[i].prompts == prompts)
在数据找到了要删除的objlv_obj_set_hidden(prompts, true);
,将图标隐藏,不显示_state_prompts_pos[i].prompts = NULL
;,将当前数组元素指针置NULL表示当前位置没有图标
4、更新状态图标位置
static void state_prompts_align_not_0(lv_obj_t* obj, lv_obj_t *base, lv_coord_t x, lv_coord_t y)
{
lv_obj_align(obj, base, LV_ALIGN_OUT_LEFT_MID, x, y);
}
void lv_update_state_prompts_pos_change(void)
{
for (char i = 0; i < (STATE_PROMPTS_SIZE(_state_prompts_pos)); i++)
{
if (_state_prompts_pos[i].prompts == NULL) /* if current obj is NULL */
{
char y = 0;
for (y = i; y < STATE_PROMPTS_SIZE(_state_prompts_pos)-1; y++)
{
if (_state_prompts_pos[y].prompts) break;
}
_state_prompts_pos[i].prompts = _state_prompts_pos[y].prompts;
_state_prompts_pos[y].prompts = NULL;
}
}
if (_state_prompts_pos[0].prompts)
{
lv_obj_t* par = lv_scr_act();
lv_obj_align(_state_prompts_pos[0].prompts, par, LV_ALIGN_IN_TOP_RIGHT, -10, 30);
}
for (char i = 1; i < STATE_PROMPTS_SIZE(_state_prompts_pos); i++)
{
if (_state_prompts_pos[i].prompts == NULL) continue;
state_prompts_align_not_0(_state_prompts_pos[i].prompts, _state_prompts_pos[i-1].prompts, -10, 0);
}
}
- 先更新数组元素,在进行对齐;当数组当前元素为NULL,就向上查找不为NULL的,将其移到到当前位置,然后将从图标的位置置NULL
if (_state_prompts_pos[0].prompts)
,特殊处理第1个图标位置state_prompts_align_not_0(_state_prompts_pos[i].prompts, _state_prompts_pos[i-1].prompts, -10, 0);
其他图标向前一个图标对齐
5、使用
- 创建1个定时lvgl task,在task回调函数中调用
lv_update_state_prompts_pos_change
- 在状态的条件触发时,调用
state_prompts_set_prompts
插入obj - 在状态条件取消时,调用
state_prompts_reset_prompts
删除obj
实现效果
边栏推荐
- Oracle 19.3 restart 环境
- High-quality DeFi application building guide to help developers enjoy DeFi Summer
- 2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享
- [Translation] Chaos Net + SkyWalking: Better observability for chaos engineering
- Import Excel/CSV from Sub Grid within Dynamics 365
- The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
- 2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
- 【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
- 深入理解 Istio 流量管理的超时时间设置
- Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database
猜你喜欢
随机推荐
力扣(LeetCode)216. 组合总和 III(2022.08.04)
The query that the user's test score is greater than the average score of a single subject
第四章:redis 数组结构的set和一些通用命令「建议收藏」
产品太多了,如何实现一次登录多产品互通?
第八章:activiti多用户任务分配
企业的数字化转型到底是否可以买来?
拓朴排序例题
反射修改jsessionid实现Session共享
Getting started with Polkadot parachain development, this article is enough
Offensive World-PWN-new_easypwn
Leetcode刷题——623. 在二叉树中增加一行
第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]
静态链接和动态链接
Go编译原理系列6(类型检查)
你最隐秘的性格在哪?
基于MindSpore高效完成图像分割,实现Dice!
The fuse: OAuth 2.0 four authorized login methods must read
MySQL之数据视图
Voice-based social software development - making the most of its value
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes