LVGL_显示图片
此笔记记录了如何开启lvgl中加载图片的功能,开启后无需每次将图片转换为.C文件
一、配置
打开lvgl中的lv_conf.h配置文件
配置步骤如下:
- 启用对应格式图片的解码器库
//启用png格式的解码器库
#define LV_USE_PNG 1
//启用gif格式的解码器库
#define LV_USE_GIF 1
如需其他格式的图片,类似操作。
- 配置文件系统(linux)
/*File system interfaces for common APIs
*To enable set a driver letter for that API*/
#define LV_USE_FS_STDIO '/' /*Uses fopen, fread, etc*/
//#define LV_FS_STDIO_PATH "/home/john/" /*Set the working directory. If commented it will be "./" */
#if LV_USE_FS_STDIO
#define LV_FS_STDIO_LETTER '/' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
#define LV_FS_STDIO_PATH "/" /*Set the working directory. File/directory paths will be appended to it.*/
#define LV_FS_STDIO_CACHE_SIZE 0 /*>0 to cache this number of bytes in lv_fs_read()*/
#endif
- 适当调整分配的内存
/*Size of the memory available for `lv_mem_alloc()` in bytes (>= 2kB)*/
# define LV_MEM_SIZE (20480U * 1024U) /*[bytes]*/
注:若内存分配不够,大点的图标就会加载不出来。
二、使用方式
//显示png图片
lv_obj_t *img_logo = lv_img_create(lv_scr_act());
lv_img_set_src(img_logo, "/tmp/test.png");
//显示gif
lv_obj_t *gif = lv_gif_create(lv_scr_act());
lv_gif_set_src(gif, "/tmp/test2.gif");