• 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```html
13<!-- xxx.hml -->
14<div class="container">
15  <image-animator class="animator" images="{{frames}}" duration="3s"/>
16</div>
17```
18
19
20```css
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```js
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```html
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```css
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```js
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> **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```html
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"   onstop="popstop" onresume="popresume" onlongpress="setresume" onclick="setpause">
155  </image-animator>
156</div>
157```
158
159
160```css
161/* xxx.css */
162.doc-page {
163  width: 100%;
164  height: 100%;
165  flex-direction: column;
166  align-items: center;
167  justify-content: center;
168   background-color: #F1F3F5;
169}
170.img {
171  width: 600px;
172  height: 600px;
173  border: 3px solid orange;
174}
175```
176
177
178```js
179// index.js
180import promptAction from '@ohos.promptAction';
181export default {
182  data: {
183    imginfo: [
184      {
185        src: 'common/landscape1.jpg',
186      },{
187        src: 'common/landscape2.jpg',
188      },{
189        src: 'common/landscape3.jpg',
190      },{
191        src: 'common/landscape4.jpg',
192      }
193    ],
194  },
195  onInit() {
196  },
197  setpause(e) {
198    this.$element('img').pause()
199  },
200  setresume(e) {
201    this.$element('img').resume()
202  },
203  popstart(e) {
204    promptAction.showToast({
205      message: 'Started.'
206    })
207  },
208  poppause(e) {
209    promptAction.showToast({
210      message: 'Paused.'
211    })
212  },
213  popstop(e) {
214    promptAction.showToast({
215      message: 'Stopped.'
216    })
217  },
218  popresume(e) {
219    promptAction.showToast({
220      message: 'Resumed.'
221    })
222  }
223}
224```
225
226![en-us_image_0000001231843076](figures/en-us_image_0000001231843076.gif)
227
228
229## Example Scenario
230
231You can click the start or stop button to change the image animation status.
232
233Call 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.
234
235
236```html
237<!-- xxx.hml -->
238<div class="doc-page">
239  <image-animator class="img" id="img" images="{{imginfo}}" iteration="2" reverse="{{rev}}" duration="10s">
240  </image-animator>
241  <div style="width: 700px;height:450px;margin-top: 40px;flex-direction:column;justify-content:space-around;">
242    <div class="container">
243      <button type="capsule" value="Start" onclick="startimg"></button>
244      <button type="capsule" value="Pause" onclick="pauseimg"></button>
245    </div>
246    <div class="container">
247      <button type="capsule" value="Stop" onclick="stopimg"></button>
248      <button type="capsule" value="Resume" onclick="resumeimg"></button>
249    </div>
250    <div class="container">
251      <button type="capsule" value="Get Status" onclick="getimgstate"></button>
252      <button type="capsule" value="{{revVal}}" onclick="revimg"></button>
253    </div>
254  </div>
255</div>
256```
257
258
259```css
260/* xxx.css */
261.doc-page {
262  width: 100%;
263  height: 100%;
264  flex-direction: column;
265  align-items: center;
266  justify-content: center;
267  background-color: #F1F3F5;
268}
269.img {
270  width: 600px;
271  height: 600px;
272  border: 3px solid orange;
273}
274button{
275  width: 260px
276}
277.container {
278  width: 100%;
279  height: 120px;
280  align-items: center;
281  justify-content: space-around;
282}
283```
284
285
286```js
287// index.js
288import prompt from '@system.prompt';
289export default {
290  data: {
291    rev:false,
292    imginfo: [
293      {
294        src: 'common/landscape1.jpg',
295      },{
296        src: 'common/landscape2.jpg',
297      },{
298        src: 'common/landscape3.jpg',
299      },{
300        src: 'common/landscape4.jpg',
301      }
302    ],
303    revVal:'Reverse'
304  },
305  onInit() {
306  },
307  startimg(e) {
308    this.$element('img').start()
309  },
310  pauseimg(e) {
311    this.$element('img').pause()
312  },
313  stopimg(e) {
314    this.$element('img').stop()
315  },
316  resumeimg(e) {
317    this.$element('img').resume()
318  },
319  getimgstate(e) {
320    prompt.showToast({
321      message: 'Current state:' + this.$element('img').getState()
322    })
323  },
324  revimg(e) {
325    this.rev = !this.rev
326    if (this.rev) {
327      this.revVal ='Play Forward'
328    } else {
329      this.revVal ='Reverse'
330    }
331  }
332}
333```
334
335![en-us_image_0000001276162717](figures/en-us_image_0000001276162717.gif)
336