Image (lv_image)
Displays images from arrays or files, with recolor, scale and rotation.
Overview
The Image widget displays images from compiled-in arrays or from files. It can recolor, scale, rotate and tile its source, and supports custom formats through the image decoder interface.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_image.c
*/
#include "../lv_examples.h"
/**
* @title Image
* @brief The same source shown plain, rotated + scaled, and recoloured.
*
* `src` points at a registered image. The second copy sets `rotation` (in
* 0.1° steps) with `scale_x`/`scale_y` (256 = 100 %) around a centred pivot.
* The third uses the `image_recolor` style properties to tint the pixels.
*/
void lv_example_image(void)
{
LV_IMAGE_DECLARE(img_logo);
static lv_style_t style_tint;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_tint);
lv_style_set_image_recolor(&style_tint, lv_color_hex(0x3b82f6));
lv_style_set_image_recolor_opa(&style_tint, (255 * 80 / 100));
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_set_style_pad_all(screen, 0, 0);
lv_obj_set_flex_flow(screen, LV_FLEX_FLOW_ROW);
lv_obj_set_style_flex_main_place(screen, LV_FLEX_ALIGN_SPACE_EVENLY, 0);
lv_obj_set_style_flex_cross_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_track_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_t * lv_image_0 = lv_image_create(screen);
lv_image_set_src(lv_image_0, &img_logo);
lv_obj_t * lv_image_1 = lv_image_create(screen);
lv_image_set_src(lv_image_1, &img_logo);
lv_image_set_rotation(lv_image_1, 300);
lv_image_set_scale_x(lv_image_1, 580);
lv_image_set_scale_y(lv_image_1, 180);
lv_obj_t * lv_image_2 = lv_image_create(screen);
lv_image_set_src(lv_image_2, &img_logo);
lv_obj_add_style(lv_image_2, &style_tint, 0);
}
<!--
@title Image
@brief The same source shown plain, rotated + scaled, and recoloured.
`src` points at a registered image. The second copy sets `rotation` (in
0.1° steps) with `scale_x`/`scale_y` (256 = 100 %) around a centred pivot.
The third uses the `image_recolor` style properties to tint the pixels.
-->
<screen>
<styles>
<style name="style_tint" image_recolor="0x3b82f6" image_recolor_opa="80%" />
</styles>
<view
style_pad_all="0"
flex_flow="row"
style_flex_main_place="space_evenly"
style_flex_cross_place="center"
style_flex_track_place="center"
>
<lv_image src="img_logo" />
<lv_image src="img_logo" rotation="300" scale_x="580" scale_y="180" />
<lv_image src="img_logo">
<style name="style_tint" />
</lv_image>
</view>
</screen>
Enumerations
lv_image_align — How the image is placed inside its container
| Value | Description |
|---|---|
default | Align top-left (same as top_left) |
top_left | Align to top-left |
top_mid | Align to top-center |
top_right | Align to top-right |
bottom_left | Align to bottom-left |
bottom_mid | Align to bottom-center |
bottom_right | Align to bottom-right |
left_mid | Align to left-center |
right_mid | Align to right-center |
center | Align to center |
stretch | Stretch to fill the widget |
tile | Tile to fill the widget |
contain | Scale to fit inside while keeping aspect ratio |
cover | Scale to fully cover while keeping aspect ratio |
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 image: image_recolor, image_recolor_opa, image_opa and the transform properties (scale/rotation). Background, border, etc can be added too. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
src | image | Set the image source (file, symbol, or variable) |
inner_align | enum:lv_image_align | Align and scale the image inside |
rotation | int | Rotate the image (degrees) |
scale_x | int | Scale horizontally (100 = original) |
scale_y | int | Scale vertically (100 = original) |
pivot_x | px|% | Set the pivot X for rotation/scaling |
pivot_y | px|% | Set the pivot Y for rotation/scaling |
offset_x | int | Shift the origin of the image source horizontally (px) |
offset_y | int | Shift the origin of the image source vertically (px) |
blend_mode | enum:lv_blend_mode | How the image's pixels mix with the background |
antialias | bool | Smooth the edges when the image is rotated or scaled |
Last updated on