lvgl按键抬起完成切屏显示例程
时间: 2023-07-23 08:35:45 浏览: 257
以下是一个基于LVGL的按键抬起完成切屏显示的例程:
```c
#include <lvgl.h>
#define LV_EXAMPLE_KEYPAD_BTN_CNT (5)
lv_obj_t * screen1;
lv_obj_t * screen2;
lv_obj_t * keypad;
lv_obj_t * btns[LV_EXAMPLE_KEYPAD_BTN_CNT];
static void btn_event_cb(lv_obj_t * obj, lv_event_t event)
{
if(event == LV_EVENT_RELEASED) {
if(obj == btns[0]) {
lv_scr_load(screen1);
} else if(obj == btns[1]) {
lv_scr_load(screen2);
}
}
}
void lv_example_keypad_1(void)
{
/* Create screen1 */
screen1 = lv_obj_create(NULL, NULL);
lv_scr_load(screen1);
/* Create a label on screen1 */
lv_obj_t * label1 = lv_label_create(screen1, NULL);
lv_label_set_text(label1, "Screen 1");
/* Create screen2 */
screen2 = lv_obj_create(NULL, NULL);
/* Create a label on screen2 */
lv_obj_t * label2 = lv_label_create(screen2, NULL);
lv_label_set_text(label2, "Screen 2");
/* Create a keypad */
keypad = lv_obj_create(NULL, NULL);
lv_obj_set_size(keypad, 200, 150);
lv_obj_center(keypad);
/* Create buttons on the keypad */
btns[0] = lv_btn_create(keypad, NULL);
lv_obj_set_size(btns[0], 60, 60);
lv_btn_set_fit2(btns[0], LV_FIT_NONE, LV_FIT_TIGHT);
lv_obj_align(btns[0], NULL, LV_ALIGN_IN_TOP_LEFT, 20, 20);
lv_obj_t * label3 = lv_label_create(btns[0], NULL);
lv_label_set_text(label3, "Screen 1");
btns[1] = lv_btn_create(keypad, btns[0]);
lv_obj_align(btns[1], btns[0], LV_ALIGN_OUT_RIGHT_MID, 20, 0);
lv_obj_t * label4 = lv_label_create(btns[1], NULL);
lv_label_set_text(label4, "Screen 2");
/* Set button event callbacks */
lv_obj_set_event_cb(btns[0], btn_event_cb);
lv_obj_set_event_cb(btns[1], btn_event_cb);
}
```
该示例中创建了两个屏幕(screen1和screen2),以及一个包含两个按钮的键盘(keypad)。按下按钮时,会切换到相应的屏幕。
要实现按键抬起完成切屏显示,需要在按钮的回调函数中判断事件类型是否为LV_EVENT_RELEASED,如果是则执行相应的屏幕加载操作。在该示例中,使用lv_scr_load()函数来加载屏幕。
阅读全文