lv_slider
lv_slider是由lv_bar进度条和外加旋钮的东西构成,当滑块被点击或拖拽导致进度值变化时会产生LV_EVENT_VALUE_CHANGED
事件,若被持续的拖拽,那么LV_EVENT_VALUE_CHANGED
也会持续产生,如果希望只获得最后进度值,则监听LV_EVENT_RELEASED
事件来获得,相关API在lv_slider.h
样式
/** Built-in styles of slider*/
enum {
LV_SLIDER_PART_BG, /** Slider background style. */
LV_SLIDER_PART_INDIC, /** Slider indicator (filled area) style. */
LV_SLIDER_PART_KNOB, /** Slider knob style. */
};
例子
static void slider_event_cb(lv_obj_t * slider, lv_event_t event);
static lv_obj_t * slider;
static lv_obj_t * slider_label;
static lv_obj_t * slider2;
static lv_obj_t * slider2_label;
void lv_ex_slider_2(void)
{
static lv_style_t slider_bg_style;//背景的样式
static lv_style_t slider_indic_style;//指示器的样式
static lv_style_t slider_knob_style;//旋钮的样式
lv_style_init(&slider_bg_style);
lv_style_set_outline_color(&slider_bg_style, LV_STATE_DEFAULT, LV_COLOR_RED);
lv_style_init(&slider_indic_style);
lv_style_set_bg_color(&slider_indic_style, LV_STATE_DEFAULT, LV_COLOR_RED);
lv_style_init(&slider_knob_style);
lv_style_set_bg_color(&slider_knob_style, LV_STATE_DEFAULT, LV_COLOR_RED);
/* Create a slider in the center of the display */
slider = lv_slider_create(lv_scr_act(), NULL);
lv_obj_add_style(slider, LV_SLIDER_PART_BG, &slider_bg_style);
lv_obj_add_style(slider, LV_SLIDER_PART_INDIC, &slider_indic_style);
lv_obj_add_style(slider, LV_SLIDER_PART_KNOB, &slider_knob_style);
lv_obj_set_width(slider, LV_DPI *2);//宽度
lv_obj_set_height(slider, 8);//高度
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_OUT_BOTTOM_MID, 0, 10);
/* 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_IN_TOP_LEFT, 10, 10);
//slider2//
slider2 = lv_slider_create(lv_scr_act(), slider);
lv_slider_set_type(slider2, LV_SLIDER_TYPE_RANGE);
lv_obj_align(slider2, slider, LV_ALIGN_CENTER, 0, 50);
lv_slider_set_value(slider2, 100, LV_ANIM_OFF);
lv_slider_set_left_value(slider2, 0, LV_ANIM_OFF);
slider2_label = lv_label_create(lv_scr_act(), slider_label);
lv_label_set_text(slider2_label, "0-100");
lv_obj_set_auto_realign(slider2_label, true);//文本自动对齐
lv_obj_align(slider2_label, slider2, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
}
static void slider_event_cb(lv_obj_t * obj, lv_event_t event)
{
if (obj == slider)
{
if (event == LV_EVENT_VALUE_CHANGED) {
static char buf[4]; /* max 3 bytes for number plus 1 null terminating byte */
printf("slider temp value :%d\n", lv_slider_get_value(obj));
snprintf(buf, 4, "%u", lv_slider_get_value(obj));
lv_label_set_text(slider_label, buf);
}
if (event == LV_EVENT_RELEASED)
{
printf("slider last value :%d\n", lv_slider_get_value(obj));
}
}
if (obj == slider2)
{
if (event == LV_EVENT_VALUE_CHANGED) {
static char buf2[8]; /* max 3 bytes for number plus 1 null terminating byte */
printf("slider2 temp value :%d-%d\n", lv_slider_get_left_value(obj),lv_slider_get_value(obj));
snprintf(buf2, 8, "%u-%u", lv_slider_get_left_value(obj), lv_slider_get_value(obj));
lv_label_set_text(slider2_label, buf2);
}
if (event == LV_EVENT_RELEASED)
{
printf("slider2 last value :%d-%d\n", lv_slider_get_left_value(obj), lv_slider_get_value(obj));
}
}
}