Animations
Guide to creating and controlling animations in LVGL Pro Editor using timeline-based animation system.
Create smooth, professional animations for your UI components using LVGL's timeline-based animation system, which allows you to define and control complex sequences with ease.
Overview
XML animations are used to animate one or more widgets and their properties. They are built from timeline animations that organize multiple animation steps into coordinated sequences.
Timelines are composed of simple animations. For example: "change the bg_opa of my_button_2 from 0 to 255 in 500 ms."
Each Component can define its own timeline animations, which can then be played by the Component itself or by any parent Components.
Style <transition>s can animate widgets too. The difference is what starts them: a timeline is played on demand, while a transition runs when the widget's state changes, for example from default to pressed.
Defining Timelines
Timelines can be defined inside <screen>s and <component>s.
Example:
<component>
<animations>
<!-- Show Component and its children -->
<timeline name="load">
<animation prop="translate_x" target="self" start="-30" end="0" duration="500"/>
<animation prop="opa" target="text" start="0" end="255" duration="500" delay="200"/>
<include_timeline target="icon" timeline="show_up" delay="300"/>
</timeline>
<!-- Shake horizontally -->
<timeline name="shake" repeat_count="infinite" repeat_delay="200">
<animation prop="translate_x" target="self" start="0" end="-30" duration="150" easing="ease_in_out"/>
<animation prop="translate_x" target="self" start="-30" end="30" duration="300" delay="150" easing="ease_in_out"/>
<animation prop="translate_x" target="self" start="30" end="0" duration="150" delay="450" easing="ease_in_out"/>
</timeline>
</animations>
<view>
<lv_button width="200">
<my_icon name="icon" src="image1"/>
<lv_label name="text" text="Click me"/>
</lv_button>
</view>
</component>Timeline Properties
Inside <animations>, you can define <timeline>s with unique names that you can reference later. A <timeline> supports these properties:
name- Unique name used to reference and play the timeline.repeat_count- How many times the whole timeline repeats. Use a number, orinfiniteto loop forever. Default is1.repeat_delay- Delay in milliseconds between repetitions. Default is0.
Simple Animations
Within each timeline, add individual <animation> elements to describe each step. The following properties are supported:
prop- Style property to animate. All integer, percentage, and color style properties are supported.selector- Style selector, e.g.knob|pressed. Default:main|default.target- Name of the UI element to animate.selfrefers to the root element of the Component (theview).start- Start value (integer only).end- End value (integer only).duration- Duration of the animation in milliseconds. Default is1000.delay- Delay before starting in milliseconds. Default is 0.early_apply- Iftrue, the start value is applied immediately, even during the delay. Default isfalse.easing- The animation path, e.g.ease_out. See below. Default islinear.
start, end, duration and delay accept constants and expressions of them:
<consts>
<int name="slide_ms" value="500"/>
<int name="slide_dist" value="30"/>
</consts>
<animation prop="translate_x" target="self" start="{-slide_dist}" end="0"
duration="{slide_ms}" delay="{slide_ms / 2}"/>Easing
The easing property tells how the value should progress from start to end during the duration. These built-in paths can be used:
linear- Constant speed. This is the default.ease_in- Slow start.ease_out- Slow end.ease_in_out- Slow start and end.overshoot- Goes above the end value and settles back.bounce- Bounces back a few times at the end.step- Stays at the start value and jumps to the end value at the very end.
<animation prop="translate_y" target="self" start="-30" end="0" duration="500" easing="ease_out"/>For full control, bezier(x1 y1 x2 y2) describes a cubic bezier curve with its two control points, just like cubic-bezier() in CSS:
<animation prop="translate_y" target="self" start="-30" end="0" duration="500"
easing="bezier(0.34 1.56 0.64 1)"/>x1 and x2 need to be in the 0..1 range, while y1 and y2 can be outside it to overshoot. At most four decimals are used from each value. Ready to use curves can be picked from e.g. easings.net.
Finally, an animation path implemented in C can be referenced by its function name:
<animation prop="translate_y" target="self" start="-30" end="0" duration="500" easing="my_easing"/>int32_t my_easing(const lv_anim_t * a)
{
/*Return the value to apply now, interpolating between start_value and end_value*/
return lv_map(a->act_time, 0, a->duration, a->start_value, a->end_value);
}The exported code calls this function directly, so it can't be static and it has to be visible where the generated file is compiled.
Like every value in <animations>, easing is resolved before any instance exists. A constant works there, an <api> <prop> does not.
Include External Timelines
The include_timeline element can be used in a <timeline> to reference animations from another timeline. For example, if my_icon defines a "show_up" timeline that fades in and enlarges the icon, you can include that timeline in other animations without duplicating the code.
To include a timeline, use the following properties:
target- Name of the target UI element whose timeline should be included.selfrefers to the root element of the Component (theview).timeline- Name of the timeline to include. Must be defined in thetarget's XML file.delay- Delay before starting in milliseconds. Default is 0.
Playing Timelines
Timelines can be triggered by events (such as clicks) using the <play_timeline_event> element as a child of any widget.
<lv_button>
<lv_label text="Open"/>
<play_timeline_event timeline="timeline_open" target="self" trigger="clicked"/>
</lv_button>Learn more about it in Events section.
Playing without a click
trigger also takes a { } condition, which fires on every false-to-true change. That covers the cases an input event cannot.
Condition triggers require LVGL Pro Editor v2.1 and LVGL v9.6.
{true} never changes, so it fires once, right after creation — which is how a timeline plays as soon as the element appears:
<!-- Runs on creation, every time -->
<play_timeline_event timeline="timeline_intro" target="self" trigger="{true}"/>
<!-- Runs on creation, but only where the instance asked for it
(`autoplay` is a <prop> of this Component) -->
<play_timeline_event timeline="timeline_intro" target="self" trigger="{autoplay}"/>A condition on a subject keeps watching. The same timeline reversed on the inverted condition gives a continuous in and out:
<play_timeline_event timeline="timeline_open" target="self" trigger="{subject_menu_open}"/>
<play_timeline_event timeline="timeline_open" target="self" trigger="{!subject_menu_open}" reverse="true"/>The condition can be any expression, so a threshold or a comparison against the Component's own property works just as well:
<!-- Shake the card when the reading goes over the limit -->
<play_timeline_event timeline="timeline_shake" target="self" trigger="{subject_temperature > 80}"/>
<!-- `index` is a plain <prop>, so each page reacts to its own number coming up -->
<play_timeline_event timeline="timeline_show" target="self" trigger="{subject_active_page == index}"/>Under the Hood
Understanding how timelines work internally helps you use them more effectively.
When an XML file is registered, the contents of the animations section are parsed, and the timeline data is stored as a "blueprint". The descriptors store target names as strings.
When a Component or Screen instance is created, lv_anim_timelines are created and initialized from the saved blueprints. If include_timelines are used, the requested timeline is included in the Component's timeline at this point. As all children are created at this point, the saved animation target names are resolved to pointers using lv_obj_find_by_name.
The created timeline instances and their names are saved in the Component's instance. Since each instance has its own timeline, you can have multiple Components (for example, 10 list_items) and play their load timelines independently with different delays.
When a play_timeline_event is added to a UI element, the target and timeline names are saved as strings. Pointers cannot be used because the event can reference UI elements that will be created only later in the view.
Finally, when the play timeline event is triggered, the selected timeline is retrieved by its name from the target and started according to the other parameters (reverse, delay, and so on).
Last updated on