1e41f4b71Sopenharmony_ci# Animator Development<a name="EN-US_TOPIC_0000001051451654"></a> 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use<a name="section726685714018"></a> 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciA 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: 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- **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**. 8e41f4b71Sopenharmony_ci- **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. 9e41f4b71Sopenharmony_ci- **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. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Available APIs<a name="section85794718418"></a> 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci**Table 1** Available functions for an animator 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci<a name="table15207105417246"></a> 16e41f4b71Sopenharmony_ci<table><thead align="left"><tr id="row1389130182514"><th class="cellrowborder" valign="top" width="17.349999999999998%" id="mcps1.2.4.1.1"><p id="p16390130172517"><a name="p16390130172517"></a><a name="p16390130172517"></a>Module</p> 17e41f4b71Sopenharmony_ci</th> 18e41f4b71Sopenharmony_ci<th class="cellrowborder" valign="top" width="54.13%" id="mcps1.2.4.1.2"><p id="p239060112519"><a name="p239060112519"></a><a name="p239060112519"></a>Function</p> 19e41f4b71Sopenharmony_ci</th> 20e41f4b71Sopenharmony_ci<th class="cellrowborder" valign="top" width="28.52%" id="mcps1.2.4.1.3"><p id="p1839012019257"><a name="p1839012019257"></a><a name="p1839012019257"></a>Description</p> 21e41f4b71Sopenharmony_ci</th> 22e41f4b71Sopenharmony_ci</tr> 23e41f4b71Sopenharmony_ci</thead> 24e41f4b71Sopenharmony_ci<tbody><tr id="row1533075412415"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p633015547249"><a name="p633015547249"></a><a name="p633015547249"></a>Animator</p> 25e41f4b71Sopenharmony_ci</td> 26e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p6330554152411"><a name="p6330554152411"></a><a name="p6330554152411"></a>void Start ()</p> 27e41f4b71Sopenharmony_ci</td> 28e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p3330155472412"><a name="p3330155472412"></a><a name="p3330155472412"></a>Starts an animator.</p> 29e41f4b71Sopenharmony_ci</td> 30e41f4b71Sopenharmony_ci</tr> 31e41f4b71Sopenharmony_ci<tr id="row18330175410241"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p33301454172415"><a name="p33301454172415"></a><a name="p33301454172415"></a>Animator</p> 32e41f4b71Sopenharmony_ci</td> 33e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p12330195419248"><a name="p12330195419248"></a><a name="p12330195419248"></a>void Stop ()</p> 34e41f4b71Sopenharmony_ci</td> 35e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p23301854162416"><a name="p23301854162416"></a><a name="p23301854162416"></a>Stops the animator.</p> 36e41f4b71Sopenharmony_ci</td> 37e41f4b71Sopenharmony_ci</tr> 38e41f4b71Sopenharmony_ci<tr id="row433045420244"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p183301054182410"><a name="p183301054182410"></a><a name="p183301054182410"></a>Animator</p> 39e41f4b71Sopenharmony_ci</td> 40e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p43302054172415"><a name="p43302054172415"></a><a name="p43302054172415"></a>void Pause ()</p> 41e41f4b71Sopenharmony_ci</td> 42e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p15330854182413"><a name="p15330854182413"></a><a name="p15330854182413"></a>Pauses the animator.</p> 43e41f4b71Sopenharmony_ci</td> 44e41f4b71Sopenharmony_ci</tr> 45e41f4b71Sopenharmony_ci<tr id="row1033085492417"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p12331135413244"><a name="p12331135413244"></a><a name="p12331135413244"></a>Animator</p> 46e41f4b71Sopenharmony_ci</td> 47e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p5331165472412"><a name="p5331165472412"></a><a name="p5331165472412"></a>void Resume ()</p> 48e41f4b71Sopenharmony_ci</td> 49e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p63314543246"><a name="p63314543246"></a><a name="p63314543246"></a>Resumes the animator.</p> 50e41f4b71Sopenharmony_ci</td> 51e41f4b71Sopenharmony_ci</tr> 52e41f4b71Sopenharmony_ci<tr id="row1331175413240"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p18331454152418"><a name="p18331454152418"></a><a name="p18331454152418"></a>Animator</p> 53e41f4b71Sopenharmony_ci</td> 54e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p15331155472414"><a name="p15331155472414"></a><a name="p15331155472414"></a>uint8_t GetState () const</p> 55e41f4b71Sopenharmony_ci</td> 56e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p2033125414243"><a name="p2033125414243"></a><a name="p2033125414243"></a>Obtains the current state of the animator.</p> 57e41f4b71Sopenharmony_ci</td> 58e41f4b71Sopenharmony_ci</tr> 59e41f4b71Sopenharmony_ci<tr id="row43311554182415"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p33311854172420"><a name="p33311854172420"></a><a name="p33311854172420"></a>Animator</p> 60e41f4b71Sopenharmony_ci</td> 61e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p433165462418"><a name="p433165462418"></a><a name="p433165462418"></a>void SetState (uint8_t state)</p> 62e41f4b71Sopenharmony_ci</td> 63e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p23316546242"><a name="p23316546242"></a><a name="p23316546242"></a>Sets the current state for the animator.</p> 64e41f4b71Sopenharmony_ci</td> 65e41f4b71Sopenharmony_ci</tr> 66e41f4b71Sopenharmony_ci<tr id="row17331254192419"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p173319547243"><a name="p173319547243"></a><a name="p173319547243"></a>Animator</p> 67e41f4b71Sopenharmony_ci</td> 68e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p18332125416243"><a name="p18332125416243"></a><a name="p18332125416243"></a>uint32_t GetTime () const</p> 69e41f4b71Sopenharmony_ci</td> 70e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p1633295412414"><a name="p1633295412414"></a><a name="p1633295412414"></a>Obtains the total duration of the animator.</p> 71e41f4b71Sopenharmony_ci</td> 72e41f4b71Sopenharmony_ci</tr> 73e41f4b71Sopenharmony_ci<tr id="row8332195419241"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p2033211545243"><a name="p2033211545243"></a><a name="p2033211545243"></a>Animator</p> 74e41f4b71Sopenharmony_ci</td> 75e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p123321054172415"><a name="p123321054172415"></a><a name="p123321054172415"></a>void SetTime (uint32_t time)</p> 76e41f4b71Sopenharmony_ci</td> 77e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p4332105472411"><a name="p4332105472411"></a><a name="p4332105472411"></a>Sets the total duration for the animator.</p> 78e41f4b71Sopenharmony_ci</td> 79e41f4b71Sopenharmony_ci</tr> 80e41f4b71Sopenharmony_ci<tr id="row13332125412420"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p17332165482417"><a name="p17332165482417"></a><a name="p17332165482417"></a>Animator</p> 81e41f4b71Sopenharmony_ci</td> 82e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p3332115417243"><a name="p3332115417243"></a><a name="p3332115417243"></a>uint32_t GetRunTime () const</p> 83e41f4b71Sopenharmony_ci</td> 84e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p733275442419"><a name="p733275442419"></a><a name="p733275442419"></a>Obtains the running time of the animator.</p> 85e41f4b71Sopenharmony_ci</td> 86e41f4b71Sopenharmony_ci</tr> 87e41f4b71Sopenharmony_ci<tr id="row2033215419249"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p113327549245"><a name="p113327549245"></a><a name="p113327549245"></a>Animator</p> 88e41f4b71Sopenharmony_ci</td> 89e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p43321154172417"><a name="p43321154172417"></a><a name="p43321154172417"></a>void SetRunTime (uint32_t runTime)</p> 90e41f4b71Sopenharmony_ci</td> 91e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p173331354182416"><a name="p173331354182416"></a><a name="p173331354182416"></a>Sets the running time for the animator.</p> 92e41f4b71Sopenharmony_ci</td> 93e41f4b71Sopenharmony_ci</tr> 94e41f4b71Sopenharmony_ci<tr id="row20333115417249"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p2333155412240"><a name="p2333155412240"></a><a name="p2333155412240"></a>Animator</p> 95e41f4b71Sopenharmony_ci</td> 96e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p143335549246"><a name="p143335549246"></a><a name="p143335549246"></a>bool IsRepeat () const</p> 97e41f4b71Sopenharmony_ci</td> 98e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p23336548244"><a name="p23336548244"></a><a name="p23336548244"></a>Checks whether the animator is repeated.</p> 99e41f4b71Sopenharmony_ci</td> 100e41f4b71Sopenharmony_ci</tr> 101e41f4b71Sopenharmony_ci<tr id="row19333754202418"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p1833319543247"><a name="p1833319543247"></a><a name="p1833319543247"></a>AnimatorCallback</p> 102e41f4b71Sopenharmony_ci</td> 103e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p6333135402416"><a name="p6333135402416"></a><a name="p6333135402416"></a>virtual void Callback (UIView *view)=0</p> 104e41f4b71Sopenharmony_ci</td> 105e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p19333854172417"><a name="p19333854172417"></a><a name="p19333854172417"></a>Represents the animator callback. You can implement your own logic in this callback.</p> 106e41f4b71Sopenharmony_ci</td> 107e41f4b71Sopenharmony_ci</tr> 108e41f4b71Sopenharmony_ci<tr id="row193331854112415"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p1133325482420"><a name="p1133325482420"></a><a name="p1133325482420"></a>AnimatorCallback</p> 109e41f4b71Sopenharmony_ci</td> 110e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p1433585412411"><a name="p1433585412411"></a><a name="p1433585412411"></a>virtual void OnStop(UIView& view) {}</p> 111e41f4b71Sopenharmony_ci</td> 112e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p933595412249"><a name="p933595412249"></a><a name="p933595412249"></a>Called after the animator stops. You can implement your own logic in this callback.</p> 113e41f4b71Sopenharmony_ci</td> 114e41f4b71Sopenharmony_ci</tr> 115e41f4b71Sopenharmony_ci<tr id="row83351654192415"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p633525419244"><a name="p633525419244"></a><a name="p633525419244"></a>AnimatorManager</p> 116e41f4b71Sopenharmony_ci</td> 117e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p203351547242"><a name="p203351547242"></a><a name="p203351547242"></a>static AnimatorManager* GetInstance()</p> 118e41f4b71Sopenharmony_ci</td> 119e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p333545412419"><a name="p333545412419"></a><a name="p333545412419"></a>Obtains an <strong id="b1489919291979"><a name="b1489919291979"></a><a name="b1489919291979"></a>AnimatorManager</strong> instance.</p> 120e41f4b71Sopenharmony_ci</td> 121e41f4b71Sopenharmony_ci</tr> 122e41f4b71Sopenharmony_ci<tr id="row3335954202412"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p5335185413247"><a name="p5335185413247"></a><a name="p5335185413247"></a>AnimatorManager</p> 123e41f4b71Sopenharmony_ci</td> 124e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p11336145442417"><a name="p11336145442417"></a><a name="p11336145442417"></a>void Add (Animator *animator)</p> 125e41f4b71Sopenharmony_ci</td> 126e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p10336175492416"><a name="p10336175492416"></a><a name="p10336175492416"></a>Adds an animator.</p> 127e41f4b71Sopenharmony_ci</td> 128e41f4b71Sopenharmony_ci</tr> 129e41f4b71Sopenharmony_ci<tr id="row18336185422417"><td class="cellrowborder" valign="top" width="17.349999999999998%" headers="mcps1.2.4.1.1 "><p id="p103361554192411"><a name="p103361554192411"></a><a name="p103361554192411"></a>AnimatorManager</p> 130e41f4b71Sopenharmony_ci</td> 131e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="54.13%" headers="mcps1.2.4.1.2 "><p id="p6336195442412"><a name="p6336195442412"></a><a name="p6336195442412"></a>void Remove(const Animator* animator);</p> 132e41f4b71Sopenharmony_ci</td> 133e41f4b71Sopenharmony_ci<td class="cellrowborder" valign="top" width="28.52%" headers="mcps1.2.4.1.3 "><p id="p233615442420"><a name="p233615442420"></a><a name="p233615442420"></a>Removes the animator.</p> 134e41f4b71Sopenharmony_ci</td> 135e41f4b71Sopenharmony_ci</tr> 136e41f4b71Sopenharmony_ci</tbody> 137e41f4b71Sopenharmony_ci</table> 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci## How to Develop<a name="section14101161317435"></a> 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci1. Implement the callback in **AnimatorCallback**. 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci ``` 144e41f4b71Sopenharmony_ci class AnimatorCallbackDemo : public OHOS::AnimatorCallback { 145e41f4b71Sopenharmony_ci public: 146e41f4b71Sopenharmony_ci AnimatorCallbackDemo(int16_t startPos, int16_t endPos, uint16_t time) 147e41f4b71Sopenharmony_ci : start_(startPos), end_(endPos), time_(time), curTime_(0) {} 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci virtual void Callback(OHOS::UIView* view) 150e41f4b71Sopenharmony_ci { 151e41f4b71Sopenharmony_ci curTime_++; 152e41f4b71Sopenharmony_ci int16_t pos = EasingEquation::CubicEaseIn(start_, end_, curTime_, time_); 153e41f4b71Sopenharmony_ci view->Invalidate(); 154e41f4b71Sopenharmony_ci view->SetPosition(pos, view->GetY()); 155e41f4b71Sopenharmony_ci view->Invalidate(); 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci protected: 158e41f4b71Sopenharmony_ci int16_t start_; 159e41f4b71Sopenharmony_ci int16_t end_; 160e41f4b71Sopenharmony_ci uint16_t time_; 161e41f4b71Sopenharmony_ci uint16_t curTime_; 162e41f4b71Sopenharmony_ci }; 163e41f4b71Sopenharmony_ci ``` 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci2. Register **AnimatorCallback** to the animator. 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci ``` 168e41f4b71Sopenharmony_ci UIImageView* image = new UIImageView(); 169e41f4b71Sopenharmony_ci image->SetSrc("..\\config\\images\\A021_001.bin"); 170e41f4b71Sopenharmony_ci image->SetPosition(0, 50); 171e41f4b71Sopenharmony_ci AnimatorCallbackDemo* callback = new AnimatorCallbackDemo(0, 338, 60); 172e41f4b71Sopenharmony_ci Animator* animator = new Animator(callback, image, 0, true); 173e41f4b71Sopenharmony_ci ``` 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci3. Add the animator to **AnimatorManager**. 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci ``` 178e41f4b71Sopenharmony_ci AnimatorManager::GetInstance()->Add(animator); 179e41f4b71Sopenharmony_ci ``` 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci4. Click the buttons in the lower part of the following figure to verify that the animation effects are as expected. 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci **Figure 1** Animator effect<a name="fig17833181682317"></a> 184e41f4b71Sopenharmony_ci  185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci 187