1 #include "../../lv_examples.h"
2 #if LV_USE_BAR && LV_BUILD_EXAMPLES
3 
set_value(void * bar,int32_t v)4 static void set_value(void * bar, int32_t v)
5 {
6     lv_bar_set_value(bar, v, LV_ANIM_OFF);
7 }
8 
event_cb(lv_event_t * e)9 static void event_cb(lv_event_t * e)
10 {
11     lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
12     if(dsc->part != LV_PART_INDICATOR) return;
13 
14     lv_obj_t * obj = lv_event_get_target(e);
15 
16     lv_draw_label_dsc_t label_dsc;
17     lv_draw_label_dsc_init(&label_dsc);
18     label_dsc.font = LV_FONT_DEFAULT;
19 
20     char buf[8];
21     lv_snprintf(buf, sizeof(buf), "%d", (int)lv_bar_get_value(obj));
22 
23     lv_point_t txt_size;
24     lv_txt_get_size(&txt_size, buf, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, LV_COORD_MAX,
25                     label_dsc.flag);
26 
27     lv_area_t txt_area;
28     /*If the indicator is long enough put the text inside on the right*/
29     if(lv_area_get_width(dsc->draw_area) > txt_size.x + 20) {
30         txt_area.x2 = dsc->draw_area->x2 - 5;
31         txt_area.x1 = txt_area.x2 - txt_size.x + 1;
32         label_dsc.color = lv_color_white();
33     }
34     /*If the indicator is still short put the text out of it on the right*/
35     else {
36         txt_area.x1 = dsc->draw_area->x2 + 5;
37         txt_area.x2 = txt_area.x1 + txt_size.x - 1;
38         label_dsc.color = lv_color_black();
39     }
40 
41     txt_area.y1 = dsc->draw_area->y1 + (lv_area_get_height(dsc->draw_area) - txt_size.y) / 2;
42     txt_area.y2 = txt_area.y1 + txt_size.y - 1;
43 
44     lv_draw_label(dsc->draw_ctx, &label_dsc, &txt_area, buf, NULL);
45 }
46 
47 /**
48  * Custom drawer on the bar to display the current value
49  */
lv_example_bar_6(void)50 void lv_example_bar_6(void)
51 {
52     lv_obj_t * bar = lv_bar_create(lv_scr_act());
53     lv_obj_add_event_cb(bar, event_cb, LV_EVENT_DRAW_PART_END, NULL);
54     lv_obj_set_size(bar, 200, 20);
55     lv_obj_center(bar);
56 
57     lv_anim_t a;
58     lv_anim_init(&a);
59     lv_anim_set_var(&a, bar);
60     lv_anim_set_values(&a, 0, 100);
61     lv_anim_set_exec_cb(&a, set_value);
62     lv_anim_set_time(&a, 2000);
63     lv_anim_set_playback_time(&a, 2000);
64     lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
65     lv_anim_start(&a);
66 
67 }
68 
69 #endif
70