1
2
3# Lottie player
4Allows to use Lottie animations in LVGL. Taken from this [base repository](https://github.com/ValentiWorkLearning/lv_rlottie)
5
6LVGL provides the interface to [Samsung/rlottie](https://github.com/Samsung/rlottie) library's C API. That is the actual Lottie player is not part of LVGL, it needs to be built separately.
7
8## Build Rlottie
9To build Samsung's Rlottie C++14-compatible compiler and optionally CMake 3.14 or higher is required.
10
11To build on desktop you can follow the instructions from Rlottie's [README](https://github.com/Samsung/rlottie/blob/master/README.md). In the most basic case it looks like this:
12```
13mkdir rlottie_workdir
14cd rlottie_workdir
15git clone https://github.com/Samsung/rlottie.git
16mkdir build
17cd build
18cmake ../rlottie
19make -j
20sudo make install
21```
22
23And finally add the `-lrlottie` flag to your linker.
24
25On embedded systems you need to take care of integrating Rlottie to the given build system.
26
27
28## Usage
29
30You can use animation from files or raw data (text). In either case first you need to enable `LV_USE_RLOTTIE` in `lv_conf.h`.
31
32
33The `width` and `height` of the object be set in the *create* function and the animation will be scaled accordingly.
34
35### Use Rlottie from file
36
37To create a Lottie animation from file use:
38```c
39  lv_obj_t * lottie = lv_rlottie_create_from_file(parent, width, height, "path/to/lottie.json");
40```
41
42Note that, Rlottie uses the standard STDIO C file API, so you can use the path "normally" and no LVGL specific driver letter is required.
43
44
45### Use Rlottie from raw string data
46
47`lv_example_rlottie_approve.c` contains an example animation in raw format. Instead storing the JSON string a hex array is stored for the following reasons:
48- avoid escaping `"` in the JSON file
49- some compilers don't support very long strings
50
51`lvgl/scripts/filetohex.py` can be used to convert a Lottie file a hex array. E.g.:
52```
53./filetohex.py path/to/lottie.json > out.txt
54```
55
56To create an animation from raw data:
57
58```c
59extern const uint8_t lottie_data[];
60lv_obj_t* lottie = lv_rlottie_create_from_raw(parent, width, height, (const char *)lottie_data);
61```
62
63## Getting animations
64
65Lottie is standard and popular format so you can find many animation files on the web.
66For example: https://lottiefiles.com/
67
68You can also create your own animations with Adobe After Effects or similar software.
69
70## Controlling animations
71
72LVGL provides two functions to control the animation mode: `lv_rlottie_set_play_mode` and `lv_rlottie_set_current_frame`.
73You'll combine your intentions when calling the first method, like in these examples:
74```c
75lv_obj_t * lottie = lv_rlottie_create_from_file(scr, 128, 128, "test.json");
76lv_obj_center(lottie);
77// Pause to a specific frame
78lv_rlottie_set_current_frame(lottie, 50);
79lv_rlottie_set_play_mode(lottie, LV_RLOTTIE_CTRL_PAUSE); // The specified frame will be displayed and then the animation will pause
80
81// Play backward and loop
82lv_rlottie_set_play_mode(lottie, LV_RLOTTIE_CTRL_PLAY | LV_RLOTTIE_CTRL_BACKWARD | LV_RLOTTIE_CTRL_LOOP);
83
84// Play forward once (no looping)
85lv_rlottie_set_play_mode(lottie, LV_RLOTTIE_CTRL_PLAY | LV_RLOTTIE_CTRL_FORWARD);
86```
87
88The default animation mode is **play forward with loop**.
89
90If you don't enable looping, a `LV_EVENT_READY` is sent when the animation can not make more progress without looping.
91
92To get the number of frames in an animation or the current frame index, you can cast the `lv_obj_t` instance to a `lv_rlottie_t` instance and inspect the `current_frame` and `total_frames` members.
93
94## Example
95```eval_rst
96
97.. include:: ../../examples/libs/rlottie/index.rst
98
99```
100
101## API
102
103```eval_rst
104
105.. doxygenfile:: lv_rlottie.h
106  :project: lvgl
107