1e41f4b71Sopenharmony_ci# image-animator
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci图片帧动画播放器。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 该组件从从API version 4 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## 子组件
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci不支持。
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## 属性
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci| 名称                  | 类型                       | 默认值   | 必填 | 描述                                                         |
18e41f4b71Sopenharmony_ci| --------------------- | -------------------------- | -------- | ---- | ------------------------------------------------------------ |
19e41f4b71Sopenharmony_ci| images                | Array&lt;ImageFrame&gt;    | -        | 是   | 设置图片帧信息集合。每一帧的帧信息包含图片路径、图片大小和图片位置信息。目前支持以下图片格式:png、jpg和bmp。ImageFrame的详细说明请见表1。<br/>**说明:**<br/>1.使用时需要使用数据绑定的方式,如images&nbsp;=&nbsp;{{images}},js中声明相应变量:images:&nbsp;[{src:&nbsp;"/common/heart-rate01.png"},&nbsp;{src:&nbsp;"/common/heart-rate02.png"}]。<br/>2.部分轻量级设备不支持原图片格式的解析。所以,在应用编译阶段,图片会被直接编译为可解析的位图(总字节数约:图片长x宽x4),并打包在应用安装包里,这会增加安装包的大小。因此,在使用图片资源时,应尽量控制图片的分辨率,尺寸越大的图片对于空间的占用越明显。 |
20e41f4b71Sopenharmony_ci| iteration             | number&nbsp;\|&nbsp;string | infinite | 否   | 设置帧动画播放次数。number表示固定次数,infinite枚举表示无限次数播放。 |
21e41f4b71Sopenharmony_ci| reverse               | boolean                    | false    | 否   | 设置播放顺序。false表示从第1张图片播放到最后1张图片;&nbsp;true表示从最后1张图片播放到第1张图片。 |
22e41f4b71Sopenharmony_ci| fixedsize             | boolean                    | true     | 否   | 设置图片大小是否固定为组件大小。&nbsp;true表示图片大小与组件大小一致,此时设置图片的width&nbsp;、height&nbsp;、top&nbsp;和left属性是无效的。false表示每一张图片的&nbsp;width&nbsp;、height&nbsp;、top和left属性都要单独设置。 |
23e41f4b71Sopenharmony_ci| duration              | string                     | -        | 是   | 设置单次播放时长。单位支持[s(秒)\|ms(毫秒)],默认单位为ms。&nbsp;duration为0时,不播放图片。&nbsp;值改变只会在下一次循环开始时生效。 |
24e41f4b71Sopenharmony_ci| fillmode<sup>5+</sup> | string                     | forwards | 否   | 指定帧动画执行结束后的状态。可选项有:<br/>-&nbsp;none:恢复初始状态。<br/>-&nbsp;forwards:保持帧动画结束时的状态(在最后一个关键帧中定义)。 |
25e41f4b71Sopenharmony_ci| id                    | string                     | -        | 否   | 组件的唯一标识。                                             |
26e41f4b71Sopenharmony_ci| style                 | string                     | -        | 否   | 组件的样式声明。                                             |
27e41f4b71Sopenharmony_ci| class                 | string                     | -        | 否   | 组件的样式类,用于引用样式表。                               |
28e41f4b71Sopenharmony_ci| ref                   | string                     | -        | 否   | 用来指定指向子元素的引用信息,该引用将注册到父组件的$refs&nbsp;属性对象上。 |
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci  **表1** ImageFrame说明
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci| 名称     | 类型             | 默认值  | 必填   | 描述               |
33e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | ---- | ---------------- |
34e41f4b71Sopenharmony_ci| src    | &lt;uri&gt;    | -    | 是    | 图片路径。            |
35e41f4b71Sopenharmony_ci| width  | &lt;length&gt; | 0    | 否    | 图片宽度。            |
36e41f4b71Sopenharmony_ci| height | &lt;length&gt; | 0    | 否    | 图片高度。            |
37e41f4b71Sopenharmony_ci| top    | &lt;length&gt; | 0    | 否    | 图片相对于组件左上角的纵向坐标。 |
38e41f4b71Sopenharmony_ci| left   | &lt;length&gt; | 0    | 否    | 图片相对于组件左上角的横向坐标。 |
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci## 事件
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci| 名称                 | 参数                                | 描述          |
44e41f4b71Sopenharmony_ci| ------------------ | --------------------------------- | ----------- |
45e41f4b71Sopenharmony_ci| stop               | -                                 | 帧动画结束时触发。   |
46e41f4b71Sopenharmony_ci| click              | -                                 | 点击动作触发该事件。  |
47e41f4b71Sopenharmony_ci| longpress          | -                                 | 长按动作触发该事件。  |
48e41f4b71Sopenharmony_ci| swipe<sup>5+</sup> | [SwipeEvent](js-lite-common-events.md) | 组件上快速滑动后触发。 |
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci## 样式
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci| 名称                                 | 类型                                       | 默认值   | 必填   | 描述                                       |
54e41f4b71Sopenharmony_ci| ---------------------------------- | ---------------------------------------- | ----- | ---- | ---------------------------------------- |
55e41f4b71Sopenharmony_ci| width                              | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>5+</sup> | -     | 否    | 设置组件自身的宽度。<br/><br/>未设置时组件宽度默认为0。        |
56e41f4b71Sopenharmony_ci| height                             | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>5+</sup> | -     | 否    | 设置组件自身的高度。<br/><br/>未设置时组件高度默认为0。        |
57e41f4b71Sopenharmony_ci| padding                            | &lt;length&gt;                           | 0     | 否    | 使用简写属性设置所有的内边距属性。<br/>&nbsp;&nbsp;该属性可以有1到4个值:<br/>-&nbsp;指定一个值时,该值指定四个边的内边距。<br/>-&nbsp;指定两个值时,第一个值指定上下两边的内边距,第二个指定左右两边的内边距。<br/>-&nbsp;指定三个值时,第一个指定上边的内边距,第二个指定左右两边的内边距,第三个指定下边的内边距。<br/>-&nbsp;指定四个值时分别为上、右、下、左边的内边距(顺时针顺序)。 |
58e41f4b71Sopenharmony_ci| padding-[left\|top\|right\|bottom] | &lt;length&gt;                           | 0     | 否    | 设置左、上、右、下内边距属性。                          |
59e41f4b71Sopenharmony_ci| margin                             | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>5+</sup> | 0     | 否    | 使用简写属性设置所有的外边距属性,该属性可以有1到4个值。<br/>-&nbsp;只有一个值时,这个值会被指定给全部的四个边。<br/>-&nbsp;两个值时,第一个值被匹配给上和下,第二个值被匹配给左和右。<br/>-&nbsp;三个值时,第一个值被匹配给上,&nbsp;第二个值被匹配给左和右,第三个值被匹配给下。<br/>-&nbsp;四个值时,会依次按上、右、下、左的顺序匹配&nbsp;(即顺时针顺序)。 |
60e41f4b71Sopenharmony_ci| margin-[left\|top\|right\|bottom]  | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>5+</sup> | 0     | 否    | 设置左、上、右、下外边距属性。                          |
61e41f4b71Sopenharmony_ci| border-width                       | &lt;length&gt;                           | 0     | 否    | 使用简写属性设置元素的所有边框宽度。                       |
62e41f4b71Sopenharmony_ci| border-color                       | &lt;color&gt;                            | black | 否    | 使用简写属性设置元素的所有边框颜色。                       |
63e41f4b71Sopenharmony_ci| border-radius                      | &lt;length&gt;                           | -     | 否    | border-radius属性是设置元素的外边框圆角半径。            |
64e41f4b71Sopenharmony_ci| background-color                   | &lt;color&gt;                            | -     | 否    | 设置背景颜色。                                  |
65e41f4b71Sopenharmony_ci| opacity<sup>5+</sup>               | number                                   | 1     | 否    | 元素的透明度,取值范围为0到1,1表示为不透明,0表示为完全透明。        |
66e41f4b71Sopenharmony_ci| display                            | string                                   | flex  | 否    | 确定一个元素所产生的框的类型,可选值为:<br/>-&nbsp;flex:弹性布局。<br/>-&nbsp;none:不渲染此元素。 |
67e41f4b71Sopenharmony_ci| [left\|top]                        | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>6+</sup> | -     | 否    | left\|top确定元素的偏移位置。<br/>-&nbsp;left属性规定元素的左边缘。该属性定义了定位元素左外边距边界与其包含块左边界之间的偏移。<br/>-&nbsp;top属性规定元素的顶部边缘。该属性定义了一个定位元素的上外边距边界与其包含块上边界之间的偏移。 |
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci## 方法
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci| 名称       | 参数   | 描述                                       |
73e41f4b71Sopenharmony_ci| -------- | ---- | ---------------------------------------- |
74e41f4b71Sopenharmony_ci| start    | -    | 开始播放图片帧动画。再次调用,重新从第1帧开始播放。               |
75e41f4b71Sopenharmony_ci| pause    | -    | 暂停播放图片帧动画。                               |
76e41f4b71Sopenharmony_ci| stop     | -    | 停止播放图片帧动画。                               |
77e41f4b71Sopenharmony_ci| resume   | -    | 继续播放图片帧。                                 |
78e41f4b71Sopenharmony_ci| getState | -    | 获取播放状态。可能值有:<br/>-&nbsp;playing:播放中<br/>-&nbsp;paused:已暂停<br/>-&nbsp;stopped:已停止。 |
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci## 示例
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci```html
85e41f4b71Sopenharmony_ci<!-- xxx.hml -->
86e41f4b71Sopenharmony_ci<div class="container">
87e41f4b71Sopenharmony_ci  <image-animator class="animator" ref="animator" images="{{frames}}" duration="1s" />
88e41f4b71Sopenharmony_ci  <div class="btn-box">
89e41f4b71Sopenharmony_ci    <input class="btn" type="button" value="start" @click="handleStart" />
90e41f4b71Sopenharmony_ci    <input class="btn" type="button" value="stop" @click="handleStop" />
91e41f4b71Sopenharmony_ci    <input class="btn" type="button" value="pause" @click="handlePause" />
92e41f4b71Sopenharmony_ci    <input class="btn" type="button" value="resume" @click="handleResume" />
93e41f4b71Sopenharmony_ci  </div>
94e41f4b71Sopenharmony_ci</div>
95e41f4b71Sopenharmony_ci```
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci```css
99e41f4b71Sopenharmony_ci/* xxx.css */
100e41f4b71Sopenharmony_ci.container {
101e41f4b71Sopenharmony_ci  flex-direction: column;
102e41f4b71Sopenharmony_ci  justify-content: center;
103e41f4b71Sopenharmony_ci  align-items: center;
104e41f4b71Sopenharmony_ci  left: 0px;
105e41f4b71Sopenharmony_ci  top: 0px;
106e41f4b71Sopenharmony_ci  width: 454px;
107e41f4b71Sopenharmony_ci  height: 454px;
108e41f4b71Sopenharmony_ci}
109e41f4b71Sopenharmony_ci.animator {
110e41f4b71Sopenharmony_ci  width: 70px;
111e41f4b71Sopenharmony_ci  height: 70px;
112e41f4b71Sopenharmony_ci}
113e41f4b71Sopenharmony_ci.btn-box {
114e41f4b71Sopenharmony_ci  width: 264px;
115e41f4b71Sopenharmony_ci  height: 120px;
116e41f4b71Sopenharmony_ci  flex-wrap: wrap;
117e41f4b71Sopenharmony_ci  justify-content: space-around;
118e41f4b71Sopenharmony_ci  align-items: center;
119e41f4b71Sopenharmony_ci}
120e41f4b71Sopenharmony_ci.btn {
121e41f4b71Sopenharmony_ci  border-radius: 8px;
122e41f4b71Sopenharmony_ci  width: 120px;
123e41f4b71Sopenharmony_ci  margin-top: 8px;
124e41f4b71Sopenharmony_ci}
125e41f4b71Sopenharmony_ci```
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci```js
129e41f4b71Sopenharmony_ci//xxx.js
130e41f4b71Sopenharmony_ciexport default {
131e41f4b71Sopenharmony_ci  data: {
132e41f4b71Sopenharmony_ci    frames: [
133e41f4b71Sopenharmony_ci      {
134e41f4b71Sopenharmony_ci        src: "/common/asserts/heart78.png",
135e41f4b71Sopenharmony_ci      },
136e41f4b71Sopenharmony_ci      {
137e41f4b71Sopenharmony_ci        src: "/common/asserts/heart79.png",
138e41f4b71Sopenharmony_ci      },
139e41f4b71Sopenharmony_ci      {
140e41f4b71Sopenharmony_ci        src: "/common/asserts/heart80.png",
141e41f4b71Sopenharmony_ci      },
142e41f4b71Sopenharmony_ci      {
143e41f4b71Sopenharmony_ci        src: "/common/asserts/heart81.png",
144e41f4b71Sopenharmony_ci      },
145e41f4b71Sopenharmony_ci      {
146e41f4b71Sopenharmony_ci        src: "/common/asserts/heart82.png",
147e41f4b71Sopenharmony_ci      },
148e41f4b71Sopenharmony_ci      {
149e41f4b71Sopenharmony_ci        src: "/common/asserts/heart83.png",
150e41f4b71Sopenharmony_ci      },
151e41f4b71Sopenharmony_ci      {
152e41f4b71Sopenharmony_ci        src: "/common/asserts/heart84.png",
153e41f4b71Sopenharmony_ci      },
154e41f4b71Sopenharmony_ci      {
155e41f4b71Sopenharmony_ci        src: "/common/asserts/heart85.png",
156e41f4b71Sopenharmony_ci      },
157e41f4b71Sopenharmony_ci      {
158e41f4b71Sopenharmony_ci        src: "/common/asserts/heart86.png",
159e41f4b71Sopenharmony_ci      },
160e41f4b71Sopenharmony_ci      {
161e41f4b71Sopenharmony_ci        src: "/common/asserts/heart87.png",
162e41f4b71Sopenharmony_ci      },
163e41f4b71Sopenharmony_ci      {
164e41f4b71Sopenharmony_ci        src: "/common/asserts/heart88.png",
165e41f4b71Sopenharmony_ci      },
166e41f4b71Sopenharmony_ci      {
167e41f4b71Sopenharmony_ci        src: "/common/asserts/heart89.png",
168e41f4b71Sopenharmony_ci      },
169e41f4b71Sopenharmony_ci      {
170e41f4b71Sopenharmony_ci        src: "/common/asserts/heart90.png",
171e41f4b71Sopenharmony_ci      },
172e41f4b71Sopenharmony_ci      {
173e41f4b71Sopenharmony_ci        src: "/common/asserts/heart91.png",
174e41f4b71Sopenharmony_ci      },
175e41f4b71Sopenharmony_ci      {
176e41f4b71Sopenharmony_ci        src: "/common/asserts/heart92.png",
177e41f4b71Sopenharmony_ci      },
178e41f4b71Sopenharmony_ci      {
179e41f4b71Sopenharmony_ci        src: "/common/asserts/heart93.png",
180e41f4b71Sopenharmony_ci      },
181e41f4b71Sopenharmony_ci      {
182e41f4b71Sopenharmony_ci        src: "/common/asserts/heart94.png",
183e41f4b71Sopenharmony_ci      },
184e41f4b71Sopenharmony_ci      {
185e41f4b71Sopenharmony_ci        src: "/common/asserts/heart95.png",
186e41f4b71Sopenharmony_ci      },
187e41f4b71Sopenharmony_ci      {
188e41f4b71Sopenharmony_ci        src: "/common/asserts/heart96.png",
189e41f4b71Sopenharmony_ci      },
190e41f4b71Sopenharmony_ci    ],
191e41f4b71Sopenharmony_ci  },
192e41f4b71Sopenharmony_ci  handleStart() {
193e41f4b71Sopenharmony_ci    this.$refs.animator.start();
194e41f4b71Sopenharmony_ci  },
195e41f4b71Sopenharmony_ci  handlePause() {
196e41f4b71Sopenharmony_ci    this.$refs.animator.pause();
197e41f4b71Sopenharmony_ci  },
198e41f4b71Sopenharmony_ci  handleResume() {
199e41f4b71Sopenharmony_ci    this.$refs.animator.resume();
200e41f4b71Sopenharmony_ci  },
201e41f4b71Sopenharmony_ci  handleStop() {
202e41f4b71Sopenharmony_ci    this.$refs.animator.stop();
203e41f4b71Sopenharmony_ci  },
204e41f4b71Sopenharmony_ci};
205e41f4b71Sopenharmony_ci```
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci![image-animator](figures/image-animator-lite.gif)
208