当前位置:网站首页>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
实现效果

边栏推荐
- MySQL transactions
- 【温度预警程序de开发】事件驱动模型实例运用
- poj2935 Basic Wall Maze (2016xynu暑期集训检测 -----D题)
- 数据可视化(一)
- 一个栈的输入序列为1 2 3 4 5 的出站顺序的理解
- Is digital transformation a business buy-in?
- 19.3 restart the Oracle environment
- SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
- 阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
- Microcontroller: temperature control DS18B20
猜你喜欢

登录功能和退出功能(瑞吉外卖)

Ali's new launch: Microservices Assault Manual, all operations are written out in PDF

Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way

FPGA: Use of the development environment Vivado

技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离

FPGA:开发环境Vivado的使用

three物体围绕一周呈球形排列

JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number

什么是 DevOps?看这一篇就够了!

Opencv图像缩放和平移
随机推荐
牛刀小试基本语法,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
今天告诉你界面控件DevExpress WinForms为何弃用经典视觉样式
MySQL事务
负载均衡应用场景
trie树模板
一个栈的输入序列为1 2 3 4 5 的出站顺序的理解
Chapter 4: activiti RuntimeService settings get and get process variables, and the difference from taskService, set process variables when starting and completing tasks [easy to understand]
E-sports, convenience, efficiency, security, key words for OriginOS functions
导火索:OAuth 2.0四种授权登录方式必读
2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
MySQL 中 auto_increment 自动插入主键值
What are the standards for electrical engineering
Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
机器学习-基础知识 - Precision, Recall, Sensitivity, Specificity, Accuracy, FNR, FPR, TPR, TNR, F1 Score, Bal
数据可视化(二)
【MindSpore Easy-Diantong Robot-01】You may have seen many knowledge quiz robots, but this one is a bit different
Microcontroller: temperature control DS18B20
[Android]如何使用RecycleView in Kotlin project
基于MindSpore高效完成图像分割,实现Dice!