LVGL自定义组件__页面指示器
创始人
2024-03-21 13:38:18
0次

前言

LVGL对硬件的要求非常低,使用其自带的组件能够搭建出精美的界面,动效也很棒。但是如过移植到Linux平台下,开发稍微复杂的应用项目,那些组件就远远不够用了。为此需要自己自定义一些组件,以方便实用。

效果

为此,尝试开发了一个页面指示器,先看效果:
在这里插入图片描述

代码

Talk is cheap, show me your code.

circles.h

#include "lvgl.h"
#define MAX_INDEX 10
typedef struct circles_ {int max_index;int active_index;lv_style_t *active_style;lv_style_t *inactive_style;lv_obj_t *circle_list[MAX_INDEX];lv_obj_t *parent;int (*click_callback)(void);const char * name;void (*init)(void);
} circles_t;typedef struct callback_user_data_{circles_t *circles;int index;
}callback_user_data_t;// set the active index
int lv_circles_set_active_index(circles_t *circles, int index);
// draw the circles on the screen
int lv_draw_circles(circles_t *circles);//demo functionvoid custom_components_circles_demo();

circles.c

#include "circles.h"// usage:
// 1. define the callback function
void test_cb(lv_event_t *e){printf("hello world\n");
}// 2.init the circles like this: 
void custom_components_circles_demo(){static lv_style_t inactive_style;lv_style_init(&inactive_style);lv_style_set_radius(&inactive_style,5);lv_style_set_size(&inactive_style, 10);lv_style_set_bg_color(&inactive_style,lv_palette_main(LV_PALETTE_GREY));lv_style_set_bg_opa(&inactive_style, 255);lv_style_set_text_opa(&inactive_style, 0);static lv_style_t active_style;lv_style_init(&active_style);lv_style_set_radius(&active_style,5);// lv_style_set_size(&active_style, 10);lv_style_set_width(&active_style, 20);lv_style_set_height(&active_style, 10);lv_style_set_bg_color(&active_style,lv_palette_main(LV_PALETTE_BLUE));lv_style_set_bg_opa(&active_style, 255);lv_style_set_text_opa(&active_style, 0);circles_t *circles;circles = (circles_t *)malloc(sizeof(circles_t));circles->active_style = &active_style;circles->inactive_style = &inactive_style;circles->max_index = 3;circles->active_index = 0;circles->name = "circles";circles->parent = lv_obj_create(lv_scr_act());lv_obj_set_size(circles->parent, 300, 75);lv_obj_set_flex_flow(circles->parent, LV_FLEX_FLOW_ROW);lv_obj_align(circles->parent,  LV_ALIGN_TOP_MID, 0, 5);circles->click_callback = test_cb;lv_draw_circles(circles);}int lv_circles_set_active_index(circles_t *circles, int index){if(index < 0 || index > circles->max_index) return -1;lv_obj_remove_style_all(circles->circle_list[circles->active_index]);lv_obj_remove_style_all(circles->circle_list[index]);lv_obj_add_style(circles->circle_list[circles->active_index], circles->inactive_style,0);lv_obj_add_style(circles->circle_list[index], circles->active_style,0);circles->active_index = index;return 0;
}
void lv_circles_event_cb(lv_event_t *e){callback_user_data_t *user_data = lv_event_get_user_data(e);lv_obj_t * btn = lv_event_get_target(e);lv_event_code_t code = lv_event_get_code(e);if(code == LV_EVENT_CLICKED){lv_circles_set_active_index(user_data->circles, user_data->index);if(user_data->circles->click_callback != NULL){user_data->circles->click_callback();}}
}
int lv_draw_circles(circles_t *circles){int i = 0;for(i =0;imax_index;i++){circles->circle_list[i] = lv_btn_create(circles->parent);lv_obj_remove_style_all(circles->circle_list[i]);callback_user_data_t *data = malloc(sizeof(callback_user_data_t));data->circles = circles;data->index = i;lv_obj_add_event_cb(circles->circle_list[i], lv_circles_event_cb, LV_EVENT_ALL, data);        if(i==circles->active_index){lv_obj_add_style(circles->circle_list[i], circles->active_style,0);}else{lv_obj_add_style(circles->circle_list[i], circles->inactive_style,0);}}
}

使用方法

  1. 项目根目录下,新建文件夹components,把上面的两个文件放进去
  2. 修改makefile:
INC  := -I./ui/simulator/inc/ -I./ -I./lvgl/ -I ./components
  1. 要使用时引入头文件
#include "circles.h"
  1. 可以使用自带的Demo快速预览,具体使用方法可以参考Demo函数
custom_components_circles_demo();

配置

参考demo函数初始化配置即可:

  1. 建立点击回调函数,并注册到组件中,在指示器被点击后翻页使用
  2. 初始化active_style,inactive_style,设置自己需要的指示器样式
  3. 创建circles对象,初始化页面总个数和默认激活哪个页面
  4. 画到界面上

总结

由于刚刚上手lvgl,所以这个组件并没有完全基于lvgl中的基础对象lv_obj类型进行编写。感兴趣的同学可以参考官方组件的写法改写哦。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...