Animator Development
When to Use
A UI animator is implemented by calling the callback function you set for each tick using the task processing mechanism. The following classes are provided for you to implement an animator:
- AnimatorManager: Manages Animator instances. This is a singleton class, which is registered with the system task callback when the Init function is executed. The system task mechanism ensures that each tick invokes the callback function of AnimatorManager.
- Animator: Represents animator-related attributes, including the start and end time of an animator. It also provides animator-specific functions, for example, to start and stop an animator, to set the animator state, and to obtain the animator.
- AnimatorCallback: Implements the content of each tick. You need to implement your own logic in this callback class so that the desired operation is executed upon the corresponding callback is invoked.
Available APIs
Table 1 Available functions for an animator
Represents the animator callback. You can implement your own logic in this callback. |
||
Called after the animator stops. You can implement your own logic in this callback. |
||
How to Develop
-
Implement the callback in AnimatorCallback.
class AnimatorCallbackDemo : public OHOS::AnimatorCallback { public: AnimatorCallbackDemo(int16_t startPos, int16_t endPos, uint16_t time) : start_(startPos), end_(endPos), time_(time), curTime_(0) {} virtual void Callback(OHOS::UIView* view) { curTime_++; int16_t pos = EasingEquation::CubicEaseIn(start_, end_, curTime_, time_); view->Invalidate(); view->SetPosition(pos, view->GetY()); view->Invalidate(); } protected: int16_t start_; int16_t end_; uint16_t time_; uint16_t curTime_; };
-
Register AnimatorCallback to the animator.
UIImageView* image = new UIImageView(); image->SetSrc("..\\config\\images\\A021_001.bin"); image->SetPosition(0, 50); AnimatorCallbackDemo* callback = new AnimatorCallbackDemo(0, 338, 60); Animator* animator = new Animator(callback, image, 0, true);
-
Add the animator to AnimatorManager.
AnimatorManager::GetInstance()->Add(animator);
-
Click the buttons in the lower part of the following figure to verify that the animation effects are as expected.