Image Button (lv_imagebutton)
A button that draws per-state images instead of a styled rectangle.
Overview
The Image Button behaves like a Button but draws user-supplied images per state instead of a styled rectangle. Separate left, middle and right images can be set; the middle image is tiled to fill the width.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_imagebutton.c
*/
#include "../lv_examples.h"
/**
* @title Image button
* @brief A button drawn from per-state images instead of a styled rectangle.
*
* `<lv_imagebutton-src_left>`, `-src_mid` and `-src_right` set the image for
* a given `state`. The middle image is tiled to fill the width, so left and
* right act as fixed end-caps. Here the released and pressed states use the
* same source to keep the example self-contained.
*/
void lv_example_imagebutton(void)
{
LV_IMAGE_DECLARE(imgbtn_left);
LV_IMAGE_DECLARE(imgbtn_mid);
LV_IMAGE_DECLARE(imgbtn_right);
static lv_style_t style_pressed;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_pressed);
lv_style_set_transform_width(&style_pressed, 10);
lv_style_set_image_recolor(&style_pressed, lv_color_hex3(0x000));
lv_style_set_image_recolor_opa(&style_pressed, (255 * 20 / 100));
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_t * lv_imagebutton_0 = lv_imagebutton_create(screen);
lv_obj_set_width(lv_imagebutton_0, 160);
lv_obj_set_align(lv_imagebutton_0, LV_ALIGN_CENTER);
lv_obj_add_style(lv_imagebutton_0, &style_pressed, LV_STATE_PRESSED);
lv_imagebutton_set_src_left(lv_imagebutton_0, LV_IMAGEBUTTON_STATE_RELEASED, &imgbtn_left);
lv_imagebutton_set_src_mid(lv_imagebutton_0, LV_IMAGEBUTTON_STATE_RELEASED, &imgbtn_mid);
lv_imagebutton_set_src_right(lv_imagebutton_0, LV_IMAGEBUTTON_STATE_RELEASED, &imgbtn_right);
lv_obj_t * lv_label_0 = lv_label_create(lv_imagebutton_0);
lv_obj_set_align(lv_label_0, LV_ALIGN_CENTER);
lv_label_set_text(lv_label_0, "Press");
lv_obj_set_style_text_color(lv_label_0, lv_color_hex(0xffffff), 0);
lv_obj_set_y(lv_label_0, -3);
}
<!--
@title Image button
@brief A button drawn from per-state images instead of a styled rectangle.
`<lv_imagebutton-src>` sets the `left`, `mid` and `right` images for
a given `state`. The middle image is tiled to fill the width, so left and
right act as fixed end-caps. Here the released and pressed states use the
same source to keep the example self-contained.
-->
<screen>
<styles>
<style name="style_pressed" transform_width="10" image_recolor="0x000" image_recolor_opa="20%" />
</styles>
<view>
<lv_imagebutton width="160" align="center">
<style name="style_pressed" selector="pressed" />
<lv_imagebutton-src state="released" left="imgbtn_left" mid="imgbtn_mid" right="imgbtn_right" />
<lv_label align="center" text="Press" style_text_color="0xffffff" y="-3" />
</lv_imagebutton>
</view>
</screen>
Enumerations
lv_imagebutton_state — Possible states of an image button
| Value | Description |
|---|---|
released | Released |
pressed | Pressed |
disabled | Disabled |
checked_released | Checked and released |
checked_pressed | Checked and pressed |
checked_disabled | Checked and disabled |
Properties
Parts
Style parts of the widget with local style (e.g. style_bg_color-knob="0xff0000") or with style sheets (e.g. <style name="style_knob" selector="knob">). See Styles to learn more.
| Part | Description |
|---|---|
main | Style the button: background, text and image_recolor/image_recolor_opa properties. The shown left/mid/right images change with the state (released, pressed, disabled, checked_*). |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
state | enum:lv_imagebutton_state | The current state of the imagebutton |
<lv_imagebutton-src>
Set the image sources in a given state
access set
| Name | Kind | Type | Description |
|---|---|---|---|
state | arg | enum:lv_imagebutton_state | Set the images in this state |
left | prop | image | The left image in the given state |
mid | prop | image | The middle image in the given state |
right | prop | image | The right image in the given state |
Last updated on