Skip to main content

Animation

Animation is a special API component in ElenixOS for creating and managing animation effects. This document will introduce the features, usage methods, and precautions of the animation component.

Feature Introduction

Animation component allows developers to create and manage various animation effects, such as position change, size change, transparency change, etc. Through animation components, developers can add vivid visual effects to applications and improve user experience.

Usage Methods

Create Animation

Use the new lv.anim() constructor to create an animation object:

// Create animation object
const anim = new lv.anim();

Configure Animation

Use the following methods to configure the animation:

// Set animation target object
anim.setVar(obj);

// Set animation value range
anim.setValues(startValue, endValue);

// Set animation duration (milliseconds)
anim.setDuration(1000);

// Set animation execution callback
anim.setExecCb((varObj, value) => {
// Execute animation logic, such as setting position, size, etc.
varObj.setX(value);
});

// Set animation completion callback
anim.setReadyCb(() => {
eos.console.log("Animation completed");
});

// Set animation path (easing function)
anim.setPathCb(lv.anim_path_ease_in_out);

Start Animation

Use the start() method to start the animation:

// Start animation
anim.start();

Example: Create Translation Animation

// Create an object
const obj = new lv.obj(eos.view.active());
obj.setSize(100, 100);
obj.setPos(50, 50);

// Create animation
const anim = new lv.anim();
anim.setVar(obj);
anim.setValues(50, 200);
anim.setDuration(1000);
anim.setExecCb((varObj, value) => {
varObj.setX(value);
});
anim.setPathCb(lv.anim_path_ease_in_out);
anim.start();

Example: Create Scale Animation

// Create an object
const obj = new lv.obj(eos.view.active());
obj.setSize(100, 100);
obj.setPos(100, 100);

// Create animation
const anim = new lv.anim();
anim.setVar(obj);
anim.setValues(100, 150);
anim.setDuration(1000);
anim.setExecCb((varObj, value) => {
varObj.setSize(value, value);
});
anim.setPathCb(lv.anim_path_ease_in_out);
anim.start();

Animation Paths (Easing Functions)

ElenixOS provides the following animation paths (easing functions):

Path FunctionDescription
lv.anim_path_linearLinear animation
lv.anim_path_ease_inEase-in animation
lv.anim_path_ease_outEase-out animation
lv.anim_path_ease_in_outEase-in-out animation
lv.anim_path_overshootOvershoot animation
lv.anim_path_bounceBounce animation

Notes

Performance Optimization

  1. Avoid running multiple complex animations at the same time: Running multiple complex animations at the same time may affect system performance
  2. Set reasonable animation duration: Animation duration should not be too long to avoid affecting user experience
  3. Use appropriate easing functions: Choose appropriate easing functions according to animation effects

Memory Management

  1. Release animation objects in time: Release animation objects in time after animation completion
  2. Avoid memory leaks: Ensure there are no circular references in animation callback functions

Error Handling

  1. Parameter verification: Verify animation parameters to avoid invalid parameters
  2. Exception capture: Use try-catch to catch possible errors

Best Practices

  1. Use animation reasonably: Use animation reasonably according to application scenarios, avoid overuse
  2. Performance testing: Conduct performance testing before using animation to ensure it does not affect system performance
  3. Code optimization: Optimize animation code to improve animation performance
  4. User experience: Consider user experience to ensure animation effects are natural and smooth

Summary

Animation component is a special API component in ElenixOS for creating and managing animation effects. Through animation components, developers can add vivid visual effects to applications and improve user experience.

When using animation components, developers should pay attention to performance optimization, memory management, error handling, and other issues to ensure animation smoothness and system stability.