GIF Decoder¶
GIF Decoder is an LVGL extension that enables you to use GIF images in your LVGL UI.
The implementation uses the AnimatedGIF library.
Usage¶
Once enabled in lv_conf.h by setting LV_USE_GIF to 1,
lv_gif_create(parent) can be used to create a gif widget.
Set the color format of the gif framebuffer with lv_gif_set_color_format(widget, color_format).
Use LV_COLOR_FORMAT_ARGB8888 for gifs with transparency.
It is set to LV_COLOR_FORMAT_ARGB8888 by default.
Significant RAM can be saved by using a smaller color format.
A color format conversion can be saved during rendering if the color format matches the display
color format.
This function can be called before lv_gif_set_src() to prevent the initial default ARGB8888
framebuffer from being allocated.
The supported color formats are:
lv_gif_set_src(widget, src) works very similarly to lv_image_set_src().
As source, it also accepts images as variables (lv_image_dsc_t) or files.
Converting GIF Files to C Arrays¶
Converting GIF files to an array of bytes is not supported in the online image
converter since LVGL v9.0. However, there is still a way to do it through the
./scripts/LVGLImage.py Python script. This command line:
python ./scripts/LVGLImage.py --cf RAW --ofmt C -o . --name my_gif_image_array input.gif
will produce all the bytes from the input.gif in unaltered form in a C array,
ready to use in this code:
LV_IMAGE_DECLARE(my_gif_image_array);
lv_obj_t * img;
img = lv_gif_create(lv_screen_active());
lv_gif_set_color_format(img, LV_COLOR_FORMAT_ARGB8888);
lv_gif_set_src(img, &my_gif_image_array);
Using GIF Images from Files¶
Example:
lv_gif_set_src(widget, "S:path/to/example.gif");
Note that, a file system driver needs to be registered to open images from files. To do so, follow the instructions in File System (lv_fs_drv).
Memory Requirements¶
To decode and display a GIF animation ~25 kB of RAM is needed plus (color format pixel size + 1) × image width × image height. RGB565 has a pixel size of 2, RGB888 has a pixel size of 3, and ARGB8888 has a pixel size of 4.
Example¶
Open a GIF image from file and variable¶
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_GIF && LV_BUILD_EXAMPLES
/**
* Open a GIF image from a file and a variable
*/
void lv_example_gif_1(void)
{
LV_IMAGE_DECLARE(img_bulb_gif);
lv_obj_t * img;
img = lv_gif_create(lv_screen_active());
lv_gif_set_color_format(img, LV_COLOR_FORMAT_ARGB8888);
lv_gif_set_src(img, &img_bulb_gif);
lv_obj_align(img, LV_ALIGN_LEFT_MID, 20, 0);
img = lv_gif_create(lv_screen_active());
lv_gif_set_color_format(img, LV_COLOR_FORMAT_ARGB8888);
/* Assuming a File system is attached to letter 'A'
* E.g. set LV_USE_FS_STDIO 'A' in lv_conf.h */
lv_gif_set_src(img, "A:lvgl/examples/libs/gif/bulb.gif");
lv_obj_align(img, LV_ALIGN_RIGHT_MID, -20, 0);
}
#endif