Hello,
I have array of buttons and on click event I change button color by adding style.
static void event_handler(lv_obj_t* obj, lv_event_t event)
{
lv_obj_add_style(obj, LV_OBJ_PART_MAIN, &style_home);
...
}
It looks like this procedure takes some memory. Since I have many clicks and low memory it is critical for me. Why setting style decrease memory?
PGNet
October 25, 2021, 8:34am
2
Hello!
Instead of changing style (in callback function) every time the button is pressed, you should initialize the button ahead for the “default” and “pressed” states ONCE.
See documentation: Button (lv_btn) — LVGL documentation
(list of styles: Styles — LVGL documentation )
See C example:
lv_style_set_shadow_width(&style, 8);
lv_style_set_shadow_color(&style, lv_palette_main(LV_PALETTE_GREY));
lv_style_set_shadow_ofs_y(&style, 8);
lv_style_set_outline_opa(&style, LV_OPA_COVER);
lv_style_set_outline_color(&style, lv_palette_main(LV_PALETTE_BLUE));
lv_style_set_text_color(&style, lv_color_white());
lv_style_set_pad_all(&style, 10);
/*Init the pressed style*/
static lv_style_t style_pr;
lv_style_init(&style_pr);
/*Ad a large outline when pressed*/
lv_style_set_outline_width(&style_pr, 30);
lv_style_set_outline_opa(&style_pr, LV_OPA_TRANSP);
lv_style_set_translate_y(&style_pr, 5);
lv_style_set_shadow_ofs_y(&style_pr, 3);
lv_style_set_bg_color(&style_pr, lv_palette_darken(LV_PALETTE_BLUE, 2));
→ lv_obj_add_style(btn1, &style_pr, LV_STATE_PRESSED);
I hope this helps.
I need to change button color according to some external condition and leave button color till next press.
PGNet
October 25, 2021, 11:13am
4
Ok, then maybe using lv_obj_set_style_bg_color for both ‘default’ and ‘pressed’ states?
It overwrites existing style bg_color
instead of adding new style.
But in this case every button should have their own style objects (created before buttons are created). With this solution memory (for style objects) is allocated only once (when buttons are created), instead of every click event.
1 Like
It might work, but why lv_obj_add_style increases memory usage in general?
PGNet
October 25, 2021, 4:48pm
6
As I see in source code, it adds the new style object (pointer) to the object’s styles list, so the list (memory) size needs to be increased.
Source code: https://github.com/lvgl/lvgl/blob/master/src/core/lv_obj_style.c#L90