1 #include "../../lv_examples.h"
2 #if LV_USE_SLIDER && LV_BUILD_EXAMPLES
3
4 static void slider_event_cb(lv_event_t * e);
5
6 /**
7 * Show the current value when the slider is pressed by extending the drawer
8 *
9 */
lv_example_slider_3(void)10 void lv_example_slider_3(void)
11 {
12 /*Create a slider in the center of the display*/
13 lv_obj_t * slider;
14 slider = lv_slider_create(lv_scr_act());
15 lv_obj_center(slider);
16
17 lv_slider_set_mode(slider, LV_SLIDER_MODE_RANGE);
18 lv_slider_set_value(slider, 70, LV_ANIM_OFF);
19 lv_slider_set_left_value(slider, 20, LV_ANIM_OFF);
20
21 lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_ALL, NULL);
22 lv_obj_refresh_ext_draw_size(slider);
23 }
24
slider_event_cb(lv_event_t * e)25 static void slider_event_cb(lv_event_t * e)
26 {
27 lv_event_code_t code = lv_event_get_code(e);
28 lv_obj_t * obj = lv_event_get_target(e);
29
30 /*Provide some extra space for the value*/
31 if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
32 lv_event_set_ext_draw_size(e, 50);
33 }
34 else if(code == LV_EVENT_DRAW_PART_END) {
35 lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
36 if(dsc->part == LV_PART_INDICATOR) {
37 char buf[16];
38 lv_snprintf(buf, sizeof(buf), "%d - %d", (int)lv_slider_get_left_value(obj), (int)lv_slider_get_value(obj));
39
40 lv_point_t label_size;
41 lv_txt_get_size(&label_size, buf, LV_FONT_DEFAULT, 0, 0, LV_COORD_MAX, 0);
42 lv_area_t label_area;
43 label_area.x1 = dsc->draw_area->x1 + lv_area_get_width(dsc->draw_area) / 2 - label_size.x / 2;
44 label_area.x2 = label_area.x1 + label_size.x;
45 label_area.y2 = dsc->draw_area->y1 - 10;
46 label_area.y1 = label_area.y2 - label_size.y;
47
48 lv_draw_label_dsc_t label_draw_dsc;
49 lv_draw_label_dsc_init(&label_draw_dsc);
50 label_draw_dsc.color = lv_color_hex3(0x888);
51 lv_draw_label(dsc->draw_ctx, &label_draw_dsc, &label_area, buf, NULL);
52 }
53 }
54 }
55
56 #endif
57