lv_demo_widgets函数样式部分代码
接着看第二部分,设置样式的代码,先看代码:
static lv_style_t style_text_muted;
static lv_style_t style_title;
static lv_style_t style_icon;
static lv_style_t style_bullet;
// lv_demo_widgets()函数代码
if LV_USE_THEME_DEFAULT
lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), LV_THEME_DEFAULT_DARK, font_normal);
#endif
lv_style_init(&style_text_muted);
lv_style_set_text_opa(&style_text_muted, LV_OPA_50);
lv_style_init(&style_title);
lv_style_set_text_font(&style_title, font_large);
lv_style_init(&style_icon);
lv_style_set_text_color(&style_icon, lv_theme_get_color_primary(NULL));
lv_style_set_text_font(&style_icon, font_large);
lv_style_init(&style_bullet);
lv_style_set_border_width(&style_bullet, 0);
lv_style_set_radius(&style_bullet, LV_RADIUS_CIRCLE);
这里看到先设置默认主题初始化,然后对4个lv_style_t 变量设置值,从字面理解,这是4个样式,文本、标题、图标、子弹这4个样式。我猜是这4个样式:
回到代码,一步步看这些函数的意义是啥?
lv_theme_default_init
这个函数是默认主题初始化,点进去看里面的代码,看看初始化了什么东西?如果不这样设置初始化,要设置特定的样式应该怎么办呢? lv_theme_default_init 在lv_theme_default.c里面的,代码如下:
lv_theme_t * lv_theme_default_init(lv_disp_t * disp, lv_color_t color_primary, lv_color_t color_secondary, bool dark, const lv_font_t * font)
{
/*This trick is required only to avoid the garbage collection of
*styles' data if LVGL is used in a binding (e.g. Micropython)
*In a general case styles could be in simple `static lv_style_t my_style...` variables*/
if(!inited) {
LV_GC_ROOT(_lv_theme_default_styles) = lv_mem_alloc(sizeof(my_theme_styles_t));
styles = (my_theme_styles_t *)LV_GC_ROOT(_lv_theme_default_styles);
}
if(LV_HOR_RES <= 320) disp_size = DISP_SMALL;
else if(LV_HOR_RES < 720) disp_size = DISP_MEDIUM;
else disp_size = DISP_LARGE;
theme.disp = disp;
theme.color_primary = color_primary;
theme.color_secondary = color_secondary;
theme.font_small = font;
theme.font_normal = font;
theme.font_large = font;
theme.apply_cb = theme_apply;
theme.flags = dark ? MODE_DARK : 0;
style_init();
inited = true;
if(disp == NULL || lv_disp_get_theme(disp) == &theme) lv_obj_report_style_change(NULL);
return (lv_theme_t *)&theme;
}
可以看到这个函数,我们可以设置的显示驱动(lv_disp_t)、两个颜色(color_primary、color_secondary),一个 dark 布尔值,最后一个字体。