Hi,大家好,欢迎来到STM32之littlevGL课堂,我们在上节课中学习了滑动列表控件(lv_roller),本节课带大家学习滑动条控件,那么什么是滑动条呢?举个最简单的例子——音量调节,这下大家应该都知道了吧~废话不多说! 一 有图有真相 像不像你调节音量的时候那个滑动条呢~ 二 代码分析 static lv_obj_t * slider_label; static void slider_event_cb(lv_obj_t * slider, lv_event_t event) { if(event == LV_EVENT_VALUE_CHANGED) { static char buf[4]; /* max 3 bytes for number plus 1 null terminating byte */ snprintf(buf, 4, "%u", lv_slider_get_value(slider)); lv_label_set_text(slider_label, buf); } } static void slider_test(void) { /* Create a slider in the center of the display */ lv_obj_t * slider = lv_slider_create(lv_scr_act(), NULL); lv_obj_set_width(slider, 200); lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0); lv_obj_set_event_cb(slider, slider_event_cb); lv_slider_set_range(slider, 0, 100); /* Create a label below the slider */ slider_label = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(slider_label, "0"); lv_obj_set_auto_realign(slider_label, true); lv_obj_align(slider_label, slider, LV_ALIGN_CENTER, 0, 30); /* Create an informative label */ lv_obj_t * info = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(info, "Welcome to the slider+label demo!\n" "Move the slider and see that the label\n" "updates to match it."); lv_obj_align(info, NULL, LV_ALIGN_CENTER, 0, -50); } lv_obj_t * slider = lv_slider_create(lv_scr_act(), NULL); lv_obj_set_width(slider, 200); lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0); lv_obj_set_event_cb(slider, slider_event_cb); lv_slider_set_range(slider, 0, 100); lv_slider_get_value(slider) OK,本节课就到这里,大家有没有学会呢?最重要的还是要多实践。下节课我们学习微调框控件(lv_spinbox),我们下节课再见! |