1e41f4b71Sopenharmony_ci# Animation Effect
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciYou can set the interpolator to implement the animation effect.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Creating an Animation Object
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciUse createAnimator to create an animation object and set the animation attributes by using the options parameter.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```html
12e41f4b71Sopenharmony_ci<!-- xxx.hml -->
13e41f4b71Sopenharmony_ci<div class="container">
14e41f4b71Sopenharmony_ci  <div style="width: 300px;height: 300px;margin-top: 100px;background: linear-gradient(pink, purple);transform: translate({{translateVal}});">
15e41f4b71Sopenharmony_ci  </div>
16e41f4b71Sopenharmony_ci  <div class="row">
17e41f4b71Sopenharmony_ci    <button type="capsule" value="play" onclick="playAnimation"></button>
18e41f4b71Sopenharmony_ci  </div>
19e41f4b71Sopenharmony_ci</div>
20e41f4b71Sopenharmony_ci```
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci```css
23e41f4b71Sopenharmony_ci/* xxx.css */
24e41f4b71Sopenharmony_ci.container {
25e41f4b71Sopenharmony_ci  width:100%;
26e41f4b71Sopenharmony_ci  height:100%;
27e41f4b71Sopenharmony_ci  flex-direction: column;
28e41f4b71Sopenharmony_ci  align-items: center;
29e41f4b71Sopenharmony_ci  justify-content: center;
30e41f4b71Sopenharmony_ci}
31e41f4b71Sopenharmony_cibutton{
32e41f4b71Sopenharmony_ci  width: 200px;
33e41f4b71Sopenharmony_ci}
34e41f4b71Sopenharmony_ci.row{
35e41f4b71Sopenharmony_ci  width: 65%;
36e41f4b71Sopenharmony_ci  height: 100px;
37e41f4b71Sopenharmony_ci  align-items: center;
38e41f4b71Sopenharmony_ci  justify-content: space-between;
39e41f4b71Sopenharmony_ci  margin-top: 50px;
40e41f4b71Sopenharmony_ci  margin-left: 260px;
41e41f4b71Sopenharmony_ci}
42e41f4b71Sopenharmony_ci```
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci```js
45e41f4b71Sopenharmony_ci// xxx.js
46e41f4b71Sopenharmony_ciimport animator from '@ohos.animator';
47e41f4b71Sopenharmony_ciexport default {
48e41f4b71Sopenharmony_ci  data: {
49e41f4b71Sopenharmony_ci    translateVal: 0,
50e41f4b71Sopenharmony_ci    animation: null
51e41f4b71Sopenharmony_ci  },
52e41f4b71Sopenharmony_ci  onInit() {},
53e41f4b71Sopenharmony_ci  onShow(){
54e41f4b71Sopenharmony_ci    var options = {
55e41f4b71Sopenharmony_ci      duration: 3000,
56e41f4b71Sopenharmony_ci      easing:"friction",
57e41f4b71Sopenharmony_ci      delay:"1000",
58e41f4b71Sopenharmony_ci      fill: 'forwards',
59e41f4b71Sopenharmony_ci      direction:'alternate',
60e41f4b71Sopenharmony_ci      iterations: 2,
61e41f4b71Sopenharmony_ci      begin: 0,
62e41f4b71Sopenharmony_ci      end: 180
63e41f4b71Sopenharmony_ci    };// Set attributes.
64e41f4b71Sopenharmony_ci    this.animation = animator.createAnimator(options)// Create an animation.
65e41f4b71Sopenharmony_ci  },
66e41f4b71Sopenharmony_ci  playAnimation() {
67e41f4b71Sopenharmony_ci    var _this = this;
68e41f4b71Sopenharmony_ci    this.animation.onframe = function(value) {
69e41f4b71Sopenharmony_ci      _this.translateVal= value
70e41f4b71Sopenharmony_ci    };
71e41f4b71Sopenharmony_ci    this.animation.play();
72e41f4b71Sopenharmony_ci  }
73e41f4b71Sopenharmony_ci}
74e41f4b71Sopenharmony_ci```
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci![en-us_image_0000001267887885](figures/en-us_image_0000001267887885.gif)
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci> **NOTE**
79e41f4b71Sopenharmony_ci>
80e41f4b71Sopenharmony_ci> - When you use createAnimator to create an animation object, you must pass the options parameter.
81e41f4b71Sopenharmony_ci>
82e41f4b71Sopenharmony_ci> - begin indicates the start point of the animation interpolation. If it is not set, the default value 0 is used.
83e41f4b71Sopenharmony_ci>
84e41f4b71Sopenharmony_ci> - end indicates the end point of the animation interpolation. If it is not set, the default value 1 is used.
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci## Adding Animation Events and Calling Methods
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ciThe animator supports events and methods, which you can use to customize the animation effect. Events include frame, cancel, repeat, and finish. Methods include update, play, pause, cancel, reverse, and finish. For details about the supported events and methods, see [animator supported events and animator supported APIs](../reference/apis-arkui/js-apis-animator.md).
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci```html
92e41f4b71Sopenharmony_ci<!-- xxx.hml -->
93e41f4b71Sopenharmony_ci<div style="flex-direction: column;align-items: center;width: 100%;height: 100%;">
94e41f4b71Sopenharmony_ci  <div style="width:200px;height: 200px;margin-top: 100px;background: linear-gradient(#b30d29, #dcac1b);
95e41f4b71Sopenharmony_ci  transform: scale({{scaleVal}});"></div>
96e41f4b71Sopenharmony_ci  <div style="width: {{DivWidth}};height: {{DivHeight}};margin-top: 200px;
97e41f4b71Sopenharmony_ci  background: linear-gradient(pink, purple);margin-top: 200px;transform:translateY({{translateVal}});">
98e41f4b71Sopenharmony_ci  </div>
99e41f4b71Sopenharmony_ci  <div class="row">
100e41f4b71Sopenharmony_ci    <button type="capsule" value="play" onclick="playAnimation"></button>
101e41f4b71Sopenharmony_ci    <button type="capsule" value="update" onclick="updateAnimation"></button>
102e41f4b71Sopenharmony_ci  </div>
103e41f4b71Sopenharmony_ci  <div class="row1">
104e41f4b71Sopenharmony_ci    <button type="capsule" value="pause" onclick="pauseAnimation"></button>
105e41f4b71Sopenharmony_ci    <button type="capsule" value="finish" onclick="finishAnimation"></button>
106e41f4b71Sopenharmony_ci  </div>
107e41f4b71Sopenharmony_ci  <div class="row2">
108e41f4b71Sopenharmony_ci    <button type="capsule" value="cancel" onclick="cancelAnimation"></button>
109e41f4b71Sopenharmony_ci    <button type="capsule" value="reverse" onclick="reverseAnimation"></button>
110e41f4b71Sopenharmony_ci  </div>
111e41f4b71Sopenharmony_ci</div>
112e41f4b71Sopenharmony_ci```
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci```css
115e41f4b71Sopenharmony_ci/* xxx.css */
116e41f4b71Sopenharmony_cibutton{
117e41f4b71Sopenharmony_ci  width: 200px;
118e41f4b71Sopenharmony_ci}
119e41f4b71Sopenharmony_ci.row{
120e41f4b71Sopenharmony_ci  width: 65%;
121e41f4b71Sopenharmony_ci  height: 100px;
122e41f4b71Sopenharmony_ci  align-items: center;
123e41f4b71Sopenharmony_ci  justify-content: space-between;
124e41f4b71Sopenharmony_ci  margin-top: 150px;
125e41f4b71Sopenharmony_ci  position: fixed;
126e41f4b71Sopenharmony_ci  top: 52%;
127e41f4b71Sopenharmony_ci  left: 120px;
128e41f4b71Sopenharmony_ci}
129e41f4b71Sopenharmony_ci.row1{
130e41f4b71Sopenharmony_ci  width: 65%;
131e41f4b71Sopenharmony_ci  height: 100px;
132e41f4b71Sopenharmony_ci  align-items: center;
133e41f4b71Sopenharmony_ci  justify-content: space-between;
134e41f4b71Sopenharmony_ci  margin-top: 120px;
135e41f4b71Sopenharmony_ci  position: fixed;
136e41f4b71Sopenharmony_ci  top: 65%;
137e41f4b71Sopenharmony_ci  left: 120px;
138e41f4b71Sopenharmony_ci}
139e41f4b71Sopenharmony_ci.row2{
140e41f4b71Sopenharmony_ci  width: 65%;
141e41f4b71Sopenharmony_ci  height: 100px;
142e41f4b71Sopenharmony_ci  align-items: center;
143e41f4b71Sopenharmony_ci  justify-content: space-between;
144e41f4b71Sopenharmony_ci  margin-top: 100px;
145e41f4b71Sopenharmony_ci  position: fixed;
146e41f4b71Sopenharmony_ci  top: 75%;
147e41f4b71Sopenharmony_ci  left: 120px;
148e41f4b71Sopenharmony_ci}
149e41f4b71Sopenharmony_ci```
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci```js
152e41f4b71Sopenharmony_ci// xxx.js
153e41f4b71Sopenharmony_ciimport animator from '@ohos.animator';
154e41f4b71Sopenharmony_ciimport promptAction from '@ohos.promptAction';
155e41f4b71Sopenharmony_ciexport default {
156e41f4b71Sopenharmony_ci  data: {
157e41f4b71Sopenharmony_ci    scaleVal:1,
158e41f4b71Sopenharmony_ci    DivWidth:200,
159e41f4b71Sopenharmony_ci    DivHeight:200,
160e41f4b71Sopenharmony_ci    translateVal:0,
161e41f4b71Sopenharmony_ci    animation: null
162e41f4b71Sopenharmony_ci  },
163e41f4b71Sopenharmony_ci  onInit() {
164e41f4b71Sopenharmony_ci    var options = {
165e41f4b71Sopenharmony_ci      duration: 3000,
166e41f4b71Sopenharmony_ci      fill: 'forwards',
167e41f4b71Sopenharmony_ci      begin: 200,
168e41f4b71Sopenharmony_ci      end: 270
169e41f4b71Sopenharmony_ci    };
170e41f4b71Sopenharmony_ci    this.animation = animator.createAnimator(options);
171e41f4b71Sopenharmony_ci  },
172e41f4b71Sopenharmony_ci  onShow() {
173e41f4b71Sopenharmony_ci    var _this= this;
174e41f4b71Sopenharmony_ci    // Add an animation repeat event.
175e41f4b71Sopenharmony_ci    this.animation.onrepeat = function() {
176e41f4b71Sopenharmony_ci      promptAction.showToast({
177e41f4b71Sopenharmony_ci        message: 'repeat'
178e41f4b71Sopenharmony_ci      });
179e41f4b71Sopenharmony_ci      var repeatoptions = {
180e41f4b71Sopenharmony_ci        duration: 2000,
181e41f4b71Sopenharmony_ci        iterations: 1,
182e41f4b71Sopenharmony_ci         direction: 'alternate',
183e41f4b71Sopenharmony_ci         begin: 180,
184e41f4b71Sopenharmony_ci         end: 240
185e41f4b71Sopenharmony_ci       };
186e41f4b71Sopenharmony_ci        _this.animation.update(repeatoptions);
187e41f4b71Sopenharmony_ci        _this.animation.play();
188e41f4b71Sopenharmony_ci      };
189e41f4b71Sopenharmony_ci  },
190e41f4b71Sopenharmony_ci  playAnimation() {
191e41f4b71Sopenharmony_ci    var _this= this;
192e41f4b71Sopenharmony_ci    // Add the frame-by-frame interpolation callback event for the animation.
193e41f4b71Sopenharmony_ci    this.animation.onframe = function(value) {
194e41f4b71Sopenharmony_ci      _this. scaleVal= value/150,
195e41f4b71Sopenharmony_ci      _this.DivWidth = value,
196e41f4b71Sopenharmony_ci      _this.DivHeight = value,
197e41f4b71Sopenharmony_ci      _this.translateVal = value-180
198e41f4b71Sopenharmony_ci    };
199e41f4b71Sopenharmony_ci    this.animation.play();
200e41f4b71Sopenharmony_ci  },
201e41f4b71Sopenharmony_ci  updateAnimation() {
202e41f4b71Sopenharmony_ci    var newoptions = {
203e41f4b71Sopenharmony_ci      duration: 5000,
204e41f4b71Sopenharmony_ci      iterations: 2,
205e41f4b71Sopenharmony_ci      begin: 120,
206e41f4b71Sopenharmony_ci      end: 180
207e41f4b71Sopenharmony_ci    };
208e41f4b71Sopenharmony_ci    this.animation.update(newoptions);
209e41f4b71Sopenharmony_ci    this.animation.play();// Play this animation.
210e41f4b71Sopenharmony_ci  },
211e41f4b71Sopenharmony_ci  pauseAnimation() {
212e41f4b71Sopenharmony_ci    this.animation.pause();// Pause this animation.
213e41f4b71Sopenharmony_ci  },
214e41f4b71Sopenharmony_ci  finishAnimation() {
215e41f4b71Sopenharmony_ci    var _this= this;
216e41f4b71Sopenharmony_ci   // Add an animation completion event.
217e41f4b71Sopenharmony_ci    this.animation.onfinish = function() {
218e41f4b71Sopenharmony_ci      promptAction.showToast({
219e41f4b71Sopenharmony_ci        message: 'finish'
220e41f4b71Sopenharmony_ci      })
221e41f4b71Sopenharmony_ci    };
222e41f4b71Sopenharmony_ci    this.animation.finish(); // Finish this animation.
223e41f4b71Sopenharmony_ci  },
224e41f4b71Sopenharmony_ci  cancelAnimation() {
225e41f4b71Sopenharmony_ci    this.animation.cancel(); // Cancel this animation.
226e41f4b71Sopenharmony_ci  },
227e41f4b71Sopenharmony_ci  reverseAnimation() {
228e41f4b71Sopenharmony_ci    this.animation.reverse(); // Reverse this animation.
229e41f4b71Sopenharmony_ci  }
230e41f4b71Sopenharmony_ci}
231e41f4b71Sopenharmony_ci```
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci![en-us_image_0000001223287724](figures/en-us_image_0000001223287724.gif)
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ci> **NOTE**
236e41f4b71Sopenharmony_ci>
237e41f4b71Sopenharmony_ci> When calling the update method, you can use it to update the animation parameters. The input parameters are the same as those of createAnimator.
238