Image button (lv_imgbtn)
Overview
The Image button is very similar to the simple 'Button' object. The only difference is that it displays user-defined images in each state instead of drawing a rectangle.
You can set a left, right and center image, and the center image will be repeated to match the width of the object.
Parts and Styles
LV_PART_MAINRefers to the image(s). If background style properties are used, a rectangle will be drawn behind the image button.
Usage
Image sources
To set the image in a state, use the lv_imgbtn_set_src(imgbtn, LV_IMGBTN_STATE_..., src_left, src_center, src_right).
The image sources work the same as described in the Image object
except that "Symbols" are not supported by the Image button. Any of the sources can NULL.
The possible states are:
If you set sources only in LV_IMGBTN_STATE_RELEASED, these sources
will be used in other states too. If you set e.g. LV_IMGBTN_STATE_PRESSED
they will be used in pressed state instead of the released images.
States
Instead of the regular lv_obj_add/remove_state() functions the
lv_imgbtn_set_state(imgbtn, LV_IMGBTN_STATE_...) functions should be
used to manually set a state.
Events
LV_EVENT_VALUE_CHANGEDSent when the button is toggled.
Learn more about Events.
Keys
LV_KEY_RIGHT/UPGo to toggled state ifLV_OBJ_FLAG_CHECKABLEis enabled.LV_KEY_LEFT/DOWNGo to non-toggled state ifLV_OBJ_FLAG_CHECKABLEis enabled.LV_KEY_ENTERClicks the button
Learn more about Keys.
Example
Simple Image button
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_IMGBTN && LV_BUILD_EXAMPLES
void lv_example_imgbtn_1(void)
{
LV_IMAGE_DECLARE(imgbtn_left);
LV_IMAGE_DECLARE(imgbtn_right);
LV_IMAGE_DECLARE(imgbtn_mid);
/*Create a transition animation on width transformation and recolor.*/
static lv_style_prop_t tr_prop[] = {LV_STYLE_TRANSFORM_WIDTH, LV_STYLE_IMAGE_RECOLOR_OPA, 0};
static lv_style_transition_dsc_t tr;
lv_style_transition_dsc_init(&tr, tr_prop, lv_anim_path_linear, 200, 0, NULL);
static lv_style_t style_def;
lv_style_init(&style_def);
lv_style_set_text_color(&style_def, lv_color_white());
lv_style_set_transition(&style_def, &tr);
/*Darken the button when pressed and make it wider*/
static lv_style_t style_pr;
lv_style_init(&style_pr);
lv_style_set_image_recolor_opa(&style_pr, LV_OPA_30);
lv_style_set_image_recolor(&style_pr, lv_color_black());
lv_style_set_transform_width(&style_pr, 20);
/*Create an image button*/
lv_obj_t * imgbtn1 = lv_imgbtn_create(lv_screen_active());
lv_imgbtn_set_src(imgbtn1, LV_IMGBTN_STATE_RELEASED, &imgbtn_left, &imgbtn_mid, &imgbtn_right);
lv_obj_add_style(imgbtn1, &style_def, 0);
lv_obj_add_style(imgbtn1, &style_pr, LV_STATE_PRESSED);
lv_obj_align(imgbtn1, LV_ALIGN_CENTER, 0, 0);
/*Create a label on the image button*/
lv_obj_t * label = lv_label_create(imgbtn1);
lv_label_set_text(label, "Button");
lv_obj_align(label, LV_ALIGN_CENTER, 0, -4);
}
#endif
# Create an image from the png file
try:
with open('../../assets/imgbtn_left.png','rb') as f:
imgbtn_left_data = f.read()
except:
print("Could not find imgbtn_left.png")
sys.exit()
imgbtn_left_dsc = lv.image_dsc_t({
'data_size': len(imgbtn_left_data),
'data': imgbtn_left_data
})
try:
with open('../../assets/imgbtn_mid.png','rb') as f:
imgbtn_mid_data = f.read()
except:
print("Could not find imgbtn_mid.png")
sys.exit()
imgbtn_mid_dsc = lv.image_dsc_t({
'data_size': len(imgbtn_mid_data),
'data': imgbtn_mid_data
})
try:
with open('../../assets/imgbtn_right.png','rb') as f:
imgbtn_right_data = f.read()
except:
print("Could not find imgtn_right.png")
sys.exit()
imgbtn_right_dsc = lv.image_dsc_t({
'data_size': len(imgbtn_right_data),
'data': imgbtn_right_data
})
# Create a transition animation on width transformation and recolor.
tr_prop = [lv.STYLE.TRANSFORM_WIDTH, lv.STYLE.IMAGE_RECOLOR_OPA, 0]
tr = lv.style_transition_dsc_t()
tr.init(tr_prop, lv.anim_t.path_linear, 200, 0, None)
style_def = lv.style_t()
style_def.init()
style_def.set_text_color(lv.color_white())
style_def.set_transition(tr)
# Darken the button when pressed and make it wider
style_pr = lv.style_t()
style_pr.init()
style_pr.set_image_recolor_opa(lv.OPA._30)
style_pr.set_image_recolor(lv.color_black())
style_pr.set_transform_width(20)
# Create an image button
imgbtn1 = lv.imgbtn(lv.screen_active())
imgbtn1.set_src(lv.imgbtn.STATE.RELEASED, imgbtn_left_dsc, imgbtn_mid_dsc, imgbtn_right_dsc)
imgbtn1.add_style(style_def, 0)
imgbtn1.add_style(style_pr, lv.STATE.PRESSED)
imgbtn1.align(lv.ALIGN.CENTER, 0, 0)
# Create a label on the image button
label = lv.label(imgbtn1)
label.set_text("Button")
label.align(lv.ALIGN.CENTER, 0, -4)