145 lines
5.9 KiB
C
Executable File
145 lines
5.9 KiB
C
Executable File
#include "lvgl.h"
|
|
|
|
|
|
static const char * kb_map[] =
|
|
{
|
|
"1","2", "3","\n",
|
|
"4", "5", "6", "\n",
|
|
"7", "8", "9" ,"\n",
|
|
".","0",LV_SYMBOL_BACKSPACE,""
|
|
};
|
|
void lv_btnmatrix_draw_test(void)
|
|
{
|
|
// 当前页面的画布容器
|
|
lv_obj_t *main_container = lv_obj_create(lv_scr_act()); // 基于屏幕创建了一个容器
|
|
if (main_container == NULL)
|
|
{
|
|
printf("[%s:%d] create main_container failed\n", __FUNCTION__, __LINE__);
|
|
return;
|
|
}
|
|
|
|
// 画布样式
|
|
static lv_style_t main_cont_style;
|
|
lv_style_reset(&main_cont_style);
|
|
lv_style_init(&main_cont_style); // 初始化样式
|
|
lv_style_set_radius(&main_cont_style, 0); // 设置样式的圆角弧度
|
|
lv_style_set_border_width(&main_cont_style, 0); // 设置边框宽度
|
|
lv_style_set_bg_opa(&main_cont_style, LV_OPA_50); // 设置样式背景的透明度
|
|
lv_style_set_bg_color(&main_cont_style, lv_color_hex(0xFFFFFF));
|
|
lv_style_set_pad_all(&main_cont_style, 0); // 设置样式内部填充
|
|
lv_obj_add_style(main_container, &main_cont_style, 0); // 给对象添加样式
|
|
lv_obj_set_size(main_container, LV_PCT(100), LV_PCT(100)); //设置大小
|
|
lv_obj_center(main_container); // 对象居屏幕中间显示
|
|
|
|
lv_obj_t * textarea = lv_textarea_create(main_container);
|
|
if (textarea == NULL)
|
|
{
|
|
printf("[%s:%d] obj textarea create failed\n", __FUNCTION__, __LINE__);
|
|
return;
|
|
}
|
|
|
|
static lv_style_t text_area_style;
|
|
lv_style_reset(&text_area_style); // 重置样式
|
|
lv_style_init(&text_area_style); // 初始化样式
|
|
lv_style_set_border_width(&text_area_style, 1); // 设置边框宽度
|
|
lv_style_set_radius(&text_area_style, 0); // 设置样式的圆角弧度
|
|
lv_style_set_pad_all(&text_area_style, 8); // 设置样式底部padding填充宽度
|
|
lv_style_set_text_font(&text_area_style, &lv_font_montserrat_20); //设置字体
|
|
lv_obj_add_style(textarea, &text_area_style, 0); // 给对象添加样式
|
|
|
|
lv_textarea_set_max_length(textarea, 6); // 设置最大输入长度
|
|
lv_textarea_set_one_line(textarea, true); // 只显示一行
|
|
lv_textarea_set_password_mode(textarea, false); // 关闭密码模式
|
|
lv_obj_set_size(textarea, 255, 40); // 设置对象大小
|
|
lv_obj_align(textarea, LV_ALIGN_TOP_MID, 0, 50); // 设置对齐
|
|
//lv_obj_add_event_cb(textarea, textarea_event_callback, LV_EVENT_CLICKED, NULL);
|
|
|
|
|
|
// 基于屏幕创建了一个容器
|
|
lv_obj_t *keyboard_bg_cont = lv_obj_create(main_container);
|
|
if (keyboard_bg_cont == NULL)
|
|
{
|
|
printf("[%s:%d] create keyboard_bg_cont failed\n", __FUNCTION__, __LINE__);
|
|
return;
|
|
}
|
|
|
|
static lv_style_t bg_style;
|
|
lv_style_reset(&bg_style); // 重置样式
|
|
lv_style_init(&bg_style); // 初始化样式
|
|
lv_style_set_border_width(&bg_style, 0); // 设置边框宽度
|
|
lv_style_set_radius(&bg_style, 5); // 设置样式的圆角弧度
|
|
lv_style_set_pad_all(&bg_style, 0); // 设置样式底部padding填充宽度
|
|
lv_style_set_bg_opa(&bg_style, LV_OPA_50); // 设置样式背景的透明度
|
|
lv_style_set_bg_color(&bg_style, lv_color_hex(0x000000));
|
|
|
|
lv_obj_add_style(keyboard_bg_cont, &bg_style, 0); // 给对象添加样式
|
|
lv_obj_set_size(keyboard_bg_cont, 260, 245); //设置对象大小
|
|
lv_obj_center(keyboard_bg_cont); // 居中显示
|
|
|
|
// 基于背景容器创建按键矩阵
|
|
lv_obj_t * btnmatrix = lv_btnmatrix_create(keyboard_bg_cont);
|
|
if (btnmatrix == NULL)
|
|
{
|
|
printf("[%s:%d] create btnmatrix obj failed\n", __FUNCTION__, __LINE__);
|
|
return ;
|
|
}
|
|
|
|
lv_obj_clear_flag(btnmatrix, LV_OBJ_FLAG_CLICK_FOCUSABLE);
|
|
lv_btnmatrix_set_map(btnmatrix, kb_map);
|
|
|
|
// 给键盘矩阵添加样式
|
|
static lv_style_t button_matrix_style;
|
|
lv_style_reset(&button_matrix_style); // 重置样式
|
|
lv_style_init(&button_matrix_style); // 初始化样式
|
|
lv_style_set_border_width(&button_matrix_style, 0); // 设置边框宽度
|
|
lv_style_set_radius(&button_matrix_style, 5); // 设置样式的圆角弧度
|
|
lv_style_set_pad_all(&button_matrix_style, 0); // 设置样式底部padding填充宽度
|
|
lv_style_set_text_opa(&button_matrix_style, LV_OPA_COVER);
|
|
lv_style_set_text_font(&button_matrix_style, &lv_font_montserrat_20); //设置字体
|
|
|
|
lv_obj_add_style(btnmatrix, &button_matrix_style, 0); // 给对象添加样式
|
|
//lv_obj_add_event_cb(btnmatrix, btnmatrix_event_cb, LV_EVENT_ALL, NULL);
|
|
//lv_obj_add_event_cb(btnmatrix, btnmatrix_event_cb, LV_EVENT_ALL, (void *)(textarea));
|
|
lv_obj_set_size(btnmatrix, 260, 245);
|
|
lv_obj_center(btnmatrix);
|
|
}
|
|
|
|
|
|
lv_obj_t* container;
|
|
|
|
static void button_event_cb(lv_event_t* event);
|
|
|
|
void desktop_init(void)
|
|
{
|
|
container = lv_obj_create(lv_scr_act());
|
|
lv_obj_set_size(container, LV_PCT(100), LV_PCT(100));
|
|
lv_obj_set_scrollbar_mode(container, LV_SCROLLBAR_MODE_OFF);
|
|
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_ROW);
|
|
lv_obj_set_flex_align(container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
|
|
lv_obj_set_scroll_snap_x(container, LV_SCROLL_SNAP_CENTER);
|
|
lv_obj_set_style_bg_opa(container, LV_OPA_0, LV_PART_MAIN);
|
|
lv_obj_set_style_bg_color(container, lv_color_black(), LV_PART_MAIN);
|
|
lv_obj_set_style_border_width(container, 0, LV_PART_MAIN);
|
|
lv_obj_set_style_pad_column(container, 60, LV_PART_MAIN); //图标之间的间隙
|
|
lv_obj_center(container);
|
|
|
|
//生成演示按钮
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
lv_obj_t* btn = lv_btn_create(container);
|
|
lv_obj_set_size(btn, 80, 80);
|
|
lv_obj_add_event_cb(btn, button_event_cb, LV_EVENT_ALL, NULL);
|
|
|
|
lv_obj_t* label = lv_label_create(btn);
|
|
lv_label_set_text_fmt(label, "%d", i);
|
|
lv_obj_center(label);
|
|
}
|
|
|
|
uint32_t mid_btn_index = (lv_obj_get_child_cnt(container) - 1) / 2;
|
|
for (uint32_t i = 0; i < mid_btn_index; i++)
|
|
{
|
|
lv_obj_move_to_index(lv_obj_get_child(container, -1), 0);
|
|
}
|
|
/*当按钮数为偶数时,确保按钮居中*/
|
|
lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index), LV_ANIM_OFF);
|
|
} |