LVGL_基础控件进度条bar
1、创建进度条控件
lv_obj_t * bar = lv_bar_create ( lv_scr_act ( ) ) ;
LV_LOG_USER ( "lv_bar_get_value(bar) %d" , lv_bar_get_value ( bar) ) ;
lv_obj_center ( bar) ;
2、设置控件大小
3、设置进度条的值
lv_bar_set_value ( bar, 50 , LV_ANIM_ON) ;
4、设置进度条值的范围
lv_bar_set_range ( bar, 0 , 200 ) ;
5、设置进度条的模式
lv_bar_set_mode ( bar, LV_BAR_MODE_SYMMETRICAL) ;
lv_bar_set_range ( bar, - 100 , 100 ) ;
lv_bar_set_value ( bar, - 20 , LV_ANIM_ON) ;
lv_bar_set_mode ( bar, LV_BAR_MODE_RANGE) ;
lv_bar_set_range ( bar, - 100 , 100 ) ;
lv_bar_set_start_value ( bar, - 90 , LV_ANIM_ON) ;
lv_bar_set_value ( bar, - 20 , LV_ANIM_ON) ;
6、设置过度动画时间
lv_obj_set_style_anim_time ( bar, 1000 , LV_PART_MAIN) ;
7、当按下的时候变成绿色
lv_obj_set_style_bg_color ( bar, lv_color_hex ( 0xbdddba ) , LV_PART_INDICATOR | LV_STATE_PRESSED) ;
8、默认颜色变成黑色
lv_obj_set_style_bg_color ( bar, lv_color_hex ( 0x1e1e1e ) , LV_PART_INDICATOR | LV_STATE_DEFAULT) ;
9、改变指示器的延申方向
lv_obj_set_style_base_dir ( bar, LV_BASE_DIR_RTL, LV_PART_MAIN) ;
10、设置事件
lv_obj_t * label = lv_label_create ( lv_scr_act ( ) ) ;
lv_label_set_text_fmt ( label, "%d%%" , lv_bar_get_value ( bar) ) ;
lv_obj_align_to ( label, bar, LV_ALIGN_OUT_BOTTOM_MID, 0 , 0 ) ;
lv_obj_add_event_cb ( bar, bar_event_cb, LV_EVENT_ALL, label) ;
static void bar_event_cb ( lv_event_t * e)
{
lv_obj_t * obj = lv_event_get_target ( e) ;
lv_obj_t * label = lv_event_get_user_data ( e) ;
lv_event_code_t code = lv_event_get_code ( e) ;
switch ( code) {
case LV_EVENT_CLICKED:
LV_LOG_USER ( "LV_EVENT_CLICKED\n" ) ;
if ( lv_bar_get_value ( obj) == lv_bar_get_max_value ( obj) )
lv_bar_set_value ( obj, 0 , LV_ANIM_ON) ;
else
lv_bar_set_value ( obj, 30 , LV_ANIM_ON) ;
lv_label_set_text_fmt ( label, "%d%%" , lv_bar_get_value ( obj) ) ;
break ;
case LV_EVENT_PRESSING:
LV_LOG_USER ( "LV_EVENT_PRESSING\n" ) ;
lv_bar_set_value ( obj, lv_bar_get_value ( obj) + 1 , LV_ANIM_ON) ;
lv_label_set_text_fmt ( label, "%d%%" , lv_bar_get_value ( obj) ) ;
break ;
default :
break ;
}
}