Lottie (lv_lottie)



The Lottie widget is capable of parsing, rasterizing, and playing Lottie animations.

The Lottie animations are vector based animation. Think of it as the modern combination of SVG and GIF.

The animations can be downloaded from various sources, such as https://lottiefiles.com/ or you can create your own animations using for example Adobe After Effects.

The Lottie widget is based on lv_canvas.h because in order to render the animation the user needs to provide a buffer where the current frame is stored.

Lottie 组件能够解析、光栅化以及播放 Lottie 动画

Lottie 动画是基于矢量的动画。可以将其视为 SVG 和 GIF 的现代组合。

动画可以从多种来源下载,比如 https://lottiefiles.com/ ,或者你可以使用例如 Adobe After Effects 来创建自己的动画。

Lottie 组件基于 lv_canvas.h 因为为了渲染动画,用户需要提供一个缓冲区来存储当前帧。

Parts and Styles(组件和样式)

  • LV_PART_MAIN The background of the Lottie animation. The typical background style properties apply but usually it is left transparent.

  • LV_PART_MAIN 动画的背景。通常背景样式属性适用,但通常保持透明。




The Lottie widget uses the ThorVG library which is integrated into LVGL. In order to use Lottie animations LV_USE_THORVG_INTERNAL (to use the built-in ThorVG) or LV_USE_THORVG_EXTERNAL (to link it externally) needs to enabled. For vector graphics in general LV_USE_VECTOR_GRAPHIC also needs to be enabled.

As ThorVG is written in C++, when using LV_USE_THORVG_INTERNAL be sure that you can compile the cpp files.

Lottie 组件使用了 ThorVG 库,该库已经 集成到 LVGL 中。

为了使用 Lottie 动画,需要启用 LV_USE_THORVG_INTERNAL (使用内置的 ThorVG)或者``LV_USE_THORVG_EXTERNAL`` (外部链接它)。对于一般的矢量图形,也需要启用 LV_USE_VECTOR_GRAPHIC

由于 ThorVG 用 C++ 编写,当使用 LV_USE_THORVG_INTERNAL 时,请确保你能编译 cpp 文件。

Set a buffer(设置缓冲区)


In order to render the animation a buffer needs to assign to the Lottie widget. The animations are rendered in ARGB8888 format, therefor the buffer's size should be equal to target_width x target_height x 4 bytes.

To keep the buffer size and the animation size consistent, the size of the widget (i.e. the size of the animation) is set to the dimensions of the buffer internally.

The buffer can be set with either void lv_lottie_set_buffer(lottie, w, h, buf); or lv_lottie_set_draw_buf(lottie, draw_buf).

When a draw buffer is used, it must be already initialized by the user with LV_COLOR_FORMAT_ARGB8888 color format.

为了渲染动画,需要给 Lottie 组件分配一个缓冲区。 动画以 ARGB8888 格式渲染,因此缓冲区的大小应该是 目标宽度 x 目标高度 x 4 字节

为了保持缓冲区大小和动画大小的一致性, 组件的大小(即动画的大小)在内部设置为缓冲区的尺寸。


void lv_lottie_set_buffer(lottie, w, h, buf);

或者 lv_lottie_set_draw_buf(lottie, draw_buf)

当使用绘制缓冲区时,它必须已经被用户使用 LV_COLOR_FORMAT_ARGB8888 颜色格式初始化。

Set a source(设置源)


lv_example_lottie_approve.c contains an example animation. Instead storing the JSON string, a hex array is stored for the following reasons: - avoid escaping " character in the JSON file - some compilers don't support very long strings

lvgl/scripts/filetohex.py can be used to convert a Lottie file a hex array. E.g.:

./filetohex.py path/to/lottie.json > out.txt

To create an animation from data use lv_lottie_set_src_data(lottie, data, sizeof(data))

Lottie animations can be opened from JSON files by using lv_lottie_set_src_file(lottie, "path/to/file.json"). Note that the Lottie loader doesn't support LVGL's File System interface but a "normal path" should be used without a driver letter.

lv_example_lottie_approve.c 包含一个示例动画。为了存储 JSON 字符串,存储了一个十六进制数组,原因如下:

  • 避免在 JSON 文件中转义 " 字符

  • 一些编译器不支持非常长的字符串

可以使用 lvgl/scripts/filetohex.py 将 Lottie 文件转换为十六进制数组。 例如:

./filetohex.py path/to/lottie.json > out.txt

要从头数据创建动画,使用 lv_lottie_set_src_data(lottie, data, sizeof(data))

Lottie 动画可以通过使用 lv_lottie_set_src_file(lottie, "path/to/file.json") 从 JSON 文件打开。 注意,Lottie 加载器不支持 LVGL 的文件系统界面,但应该使用没有驱动器字母的“普通路径”。

Get the animation(获取动画)


lv_anim_t * a = lv_lottie_get_anim(lottie) return the LVGL animation which controls the Lottie animation. By default it is running infinitely at 60FPS however the LVGL animation can be freely adjusted.

lv_anim_t * a = lv_lottie_get_anim(lottie) 返回控制 Lottie 动画的 LVGL 动画。默认情况下,它无限期地以 60FPS 运行,但是 LVGL 动画可以自由调整。



No special events are sent by the Lottie widget.

Learn more about Events(事件).

Lottie 组件没有发送特殊事件。

了解更多关于 Events(事件)



No keys are processed by the Lottie widget Learn more about Keys(按键).

Lottie 组件不处理任何键。 了解更多关于 Keys(按键)