• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <image-animator> Development
2
3
4The **<image-animator>** component applies an animation to images. For details, see [image-animator](../reference/arkui-js/js-components-basic-image-animator.md).
5
6
7## Creating an <image-animator> Component
8
9In the **pages/index** directory, create an **<image-animator>** component in the .hml file, define the component style in the .css file, and reference an image in the .js file.
10
11
12```
13<!-- xxx.hml -->
14<div class="container">
15  <image-animator class="animator" images="{{frames}}" duration="3s"/>
16</div>
17```
18
19
20```
21/* xxx.css */
22.container {
23  width: 100%;
24  height: 100%;
25  flex-direction: column;
26  justify-content: center;
27  align-items: center;
28  background-color: #F1F3F5;
29}
30.animator {
31  width: 500px;
32  height: 500px;
33}
34```
35
36
37```
38/* index.js */
39export default {
40  data: {
41    frames: [
42      {
43        src: "/common/landscape1.jpg",
44      },
45      {
46        src: "/common/landscape2.jpg",
47      }
48    ],
49  },
50};
51```
52
53![en-us_image_0000001275922969](figures/en-us_image_0000001275922969.gif)
54
55
56## Setting the &lt;image-animator&gt; Attributes
57
58Add the **iteration** (number of times the animation is played), **reverse** (whether the animation plays backward), **fixedsize** (whether the image size is fixed to the component size), **duration** (duration of the animation), and **fillmode** (style of the target when the animation is not playing) attributes.
59
60
61```
62<!-- xxx.hml -->
63<div class="container">
64  <image-animator class="animator" fixedsize="false" iteration='2' reverse="false" ref="animator" fillmode="none" images="{{frames}}"duration="5s" />
65</div>
66```
67
68
69```
70/* xxx.css */
71.container {
72  width: 100%;
73  height: 100%;
74  flex-direction: column;
75  background-color: #F1F3F5;
76}
77.animator {
78  width: 500px;
79  height: 500px;
80}
81```
82
83
84```
85/* index.js */
86export default {
87  data: {
88    frames: [
89      {
90        src: 'common/landscape1.jpg',
91        width: '250px',
92        height: '250px',
93        left: '150px',
94        top: '50px',
95      },
96      {
97        src: 'common/landscape2.jpg',
98        width: '300px',
99        height: '300px',
100        left: '150px',
101        top: '100px',
102      },
103      {
104        src: 'common/landscape1.jpg',
105        width: '350px',
106        height: '350px',
107        left: '150px',
108        top: '150px',
109      },
110      {
111        src: 'common/landscape2.jpg',
112        width: '400px',
113        height: '400px',
114        left: '150px',
115        top: '200px',
116      },
117      {
118        src: 'common/landscape3.jpg',
119        width: '450px',
120        height: '450px',
121        left: '150px',
122        top: '250px',
123      },
124      {
125        src: 'common/landscape4.jpg',
126        width: '500px',
127        height: '500px',
128        left: '150px',
129        top: '300px',
130      },
131    ],
132  },
133};
134```
135
136![en-us_image_0000001276003481](figures/en-us_image_0000001276003481.gif)
137
138> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
139> - If the **duration** attribute is set in the **images** attribute, the **duration** attribute set in the **&lt;image-animator&gt;** component is invalid.
140>
141> - If **fixedsize** is set to **true**, the **width**, **height**, **top**, and **left** settings in **images** will not take effect.
142>
143> - Setting **reverse** to **false** indicates that images are played from the first one to the last one. Setting **reverse** to **true** indicates that images are played from the last one to the first one.
144
145
146## Binding Events
147
148Add the start, pause, stop, and resume events to the **&lt;image-animator&gt;** component. Specifically, the start event is triggered when the image animator starts playing; the pause event is triggered when the image animator is clicked; the resume event is triggered when the image animator is pressed and held; the stop event is triggered when the image animator stops playing.
149
150
151```
152<!-- xxx.hml -->
153<div class="doc-page">
154  <image-animator class="img" id="img" images="{{imginfo}}" iteration="1" duration="10s" onstart="popstart" onpause="poppause"
155  onstop="popstop" onresume="popresume" onlongpress="setresume" onclick="setpause">
156  </image-animator>
157</div>
158```
159
160
161```
162/* xxx.css */
163.doc-page {
164  width: 100%;
165  height: 100%;
166  flex-direction: column;
167  align-items: center;
168  justify-content: center;
169   background-color: #F1F3F5;
170}
171.img {
172  width: 600px;
173  height: 600px;
174  border: 3px solid orange;
175}
176```
177
178
179```
180/* index.js */
181import prompt from '@system.prompt';
182export default {
183  data: {
184    imginfo: [
185      {
186        src: 'common/landscape1.jpg',
187      },{
188        src: 'common/landscape2.jpg',
189      },{
190        src: 'common/landscape3.jpg',
191      },{
192        src: 'common/landscape4.jpg',
193      }
194    ],
195  },
196  onInit() {
197  },
198  setpause(e) {
199    this.$element('img').pause()
200  },
201  setresume(e) {
202    this.$element('img').resume()
203  },
204  popstart(e) {
205    prompt.showToast({
206      message: 'Started.'
207    })
208  },
209  poppause(e) {
210    prompt.showToast({
211      message: 'Paused.'
212    })
213  },
214  popstop(e) {
215    prompt.showToast({
216      message: 'Stopped.'
217    })
218  },
219  popresume(e) {
220    prompt.showToast({
221      message: 'Resumed.'
222    })
223  }
224}
225```
226
227![en-us_image_0000001231843076](figures/en-us_image_0000001231843076.gif)
228
229
230## Example Scenario
231
232You can click the start or stop button to change the image animation status.
233
234Call the start, pause, stop, and resume methods to start, pause, stop, and resume the image animation, and call the **getState** method to check the image animation status.
235
236
237```
238<!-- xxx.hml -->
239<div class="doc-page">
240  <image-animator class="img" id="img" images="{{imginfo}}" iteration="2" reverse="{{rev}}" duration="10s">
241  </image-animator>
242  <div style="width: 700px;height:450px;margin-top: 40px;flex-direction:column;justify-content:space-around;">
243    <div class="container">
244      <button type="capsule" value="Start" onclick="startimg"></button>
245      <button type="capsule" value="Pause" onclick="pauseimg"></button>
246    </div>
247    <div class="container">
248      <button type="capsule" value="Stop" onclick="stopimg"></button>
249      <button type="capsule" value="Resume" onclick="resumeimg"></button>
250    </div>
251    <div class="container">
252      <button type="capsule" value="Get Status" onclick="getimgstate"></button>
253      <button type="capsule" value="{{revVal}}" onclick="revimg"></button>
254    </div>
255  </div>
256</div>
257```
258
259
260```
261/* xxx.css */
262.doc-page {
263  width: 100%;
264  height: 100%;
265  flex-direction: column;
266  align-items: center;
267  justify-content: center;
268  background-color: #F1F3F5;
269}
270.img {
271  width: 600px;
272  height: 600px;
273  border: 3px solid orange;
274}
275button{
276  width: 260px
277}
278.container {
279  width: 100%;
280  height: 120px;
281  align-items: center;
282  justify-content: space-around;
283}
284```
285
286
287```
288/* index.js */
289import prompt from '@system.prompt';
290export default {
291  data: {
292    rev:false,
293    imginfo: [
294      {
295        src: 'common/landscape1.jpg',
296      },{
297        src: 'common/landscape2.jpg',
298      },{
299        src: 'common/landscape3.jpg',
300      },{
301        src: 'common/landscape4.jpg',
302      }
303    ],
304    revVal:'Reverse'
305  },
306  onInit() {
307  },
308  startimg(e) {
309    this.$element('img').start()
310  },
311  pauseimg(e) {
312    this.$element('img').pause()
313  },
314  stopimg(e) {
315    this.$element('img').stop()
316  },
317  resumeimg(e) {
318    this.$element('img').resume()
319  },
320  getimgstate(e) {
321    prompt.showToast({
322      message: 'Current state:' + this.$element('img').getState()
323    })
324  },
325  revimg(e) {
326    this.rev = !this.rev
327    if (this.rev) {
328      this.revVal ='Play Forward'
329    } else {
330      this.revVal ='Reverse'
331    }
332  }
333}
334```
335
336![en-us_image_0000001276162717](figures/en-us_image_0000001276162717.gif)
337