ui-js-components-image-animator.md 7.3 KB
Newer Older
H
HelloCrease 已提交
1
# image-animator开发指导
H
HelloCrease 已提交
2 3


H
HelloCrease 已提交
4
image-animator组件为图片帧动画播放器。具体用法请参考[image-animator](../reference/arkui-js/js-components-basic-image-animator.md)
H
HelloCrease 已提交
5 6


H
HelloCrease 已提交
7
## 创建image-animator组件
H
HelloCrease 已提交
8

H
HelloCrease 已提交
9
在pages/index目录下的hml文件中创建一个image-animator组件,css文件中编写组件样式,js文件中引用图片。
H
HelloCrease 已提交
10 11


H
HelloCrease 已提交
12
```html
H
HelloCrease 已提交
13 14 15 16 17 18 19
<!-- xxx.hml -->
<div class="container">
  <image-animator class="animator" images="{{frames}}" duration="3s"/>
</div>
```


H
HelloCrease 已提交
20
```css
H
HelloCrease 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.animator {
  width: 500px;
  height: 500px;
}
```


H
HelloCrease 已提交
37 38
```js
// index.js
H
HelloCrease 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
export default {
  data: {
    frames: [
      {
        src: "/common/landscape1.jpg",
      },
      {
        src: "/common/landscape2.jpg",
      }
    ],
  },
};
```

![zh-cn_image_0000001218278612](figures/zh-cn_image_0000001218278612.gif)


H
HelloCrease 已提交
56
## 设置image-animator组件属性
H
HelloCrease 已提交
57 58 59 60

添加iteration(播放次数)、reverse(播放顺序)、fixedsize(图片大小是否固定为组件大小)、duration(播放时长)和fillmode(执行结束后的状态)属性,控制图片的播放效果。


H
HelloCrease 已提交
61
```html
H
HelloCrease 已提交
62 63 64 65 66 67 68
<!-- xxx.hml -->
<div class="container">
  <image-animator class="animator" fixedsize="false" iteration='2' reverse="false" ref="animator" fillmode="none" images="{{frames}}"   duration="5s" />
</div>
```


H
HelloCrease 已提交
69
```css
H
HelloCrease 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  background-color: #F1F3F5;
}
.animator {
  width: 500px;
  height: 500px;
}
```


H
HelloCrease 已提交
84 85
```js
// index.js
H
HelloCrease 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
export default {
  data: {
    frames: [
      {
        src: 'common/landscape1.jpg',
        width: '250px',
        height: '250px',
        left: '150px',
        top: '50px',
      },
      {
        src: 'common/landscape2.jpg',
        width: '300px',
        height: '300px',
        left: '150px',
        top: '100px',
      },
      {
        src: 'common/landscape1.jpg',
        width: '350px',
        height: '350px',
        left: '150px',
        top: '150px',
      },
      {
        src: 'common/landscape2.jpg',
        width: '400px',
        height: '400px',
        left: '150px',
        top: '200px',
      },
      {
        src: 'common/landscape3.jpg',
        width: '450px',
        height: '450px',
        left: '150px',
        top: '250px',
      },
      {
        src: 'common/landscape4.jpg',
        width: '500px',
        height: '500px',
        left: '150px',
        top: '300px',
      },
    ],
  },
};
```

![zh-cn_image_0000001218598678](figures/zh-cn_image_0000001218598678.gif)

H
HelloCrease 已提交
138 139 140
> **说明:**
> - 如果在images属性中设置了单独的duration属性,在image-animator组件中设置的duration属性无效。
>
H
HelloCrease 已提交
141
> - 如果fixedsize属性值设置为true,图片的width 、height 、top 和left属性无效。
H
HelloCrease 已提交
142
>
H
HelloCrease 已提交
143 144 145 146 147
> - 如果reverse属性值设置为false,表示从第1张图片播放到最后1张图片。 如果reverse属性值设置为true,表示从最后1张图片播放到第1张图片。


## 绑定事件

H
HelloCrease 已提交
148
向image-animator组件添加start、pause、stop和resume事件。当图片播放器开始播放时触发start事件,当图片播放器被点击时触发pause事件,长按图片播放器触发resume事件,图片播放器停止播放时触发stop事件。
H
HelloCrease 已提交
149 150


H
HelloCrease 已提交
151
```html
H
HelloCrease 已提交
152 153 154 155 156 157 158 159
<!-- xxx.hml -->
<div class="doc-page">
  <image-animator class="img" id="img" images="{{imginfo}}" iteration="1" duration="10s" onstart="popstart" onpause="poppause"   onstop="popstop" onresume="popresume" onlongpress="setresume" onclick="setpause">
  </image-animator>
</div>
```


H
HelloCrease 已提交
160
```css
H
HelloCrease 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/* xxx.css */
.doc-page {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
   background-color: #F1F3F5;
}
.img {
  width: 600px;
  height: 600px;
  border: 3px solid orange;
}
```


H
HelloCrease 已提交
178 179
```js
// index.js
180
import promptAction from '@ohos.promptAction';
H
HelloCrease 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
export default {
  data: {
    imginfo: [
      {
        src: 'common/landscape1.jpg',
      },{
        src: 'common/landscape2.jpg',
      },{
        src: 'common/landscape3.jpg',
      },{
        src: 'common/landscape4.jpg',
      }
    ],
  },
  onInit() {
  },
  setpause(e) {
    this.$element('img').pause()
  },
  setresume(e) {
    this.$element('img').resume()
  },
  popstart(e) {
204
    promptAction.showToast({
H
HelloCrease 已提交
205 206 207 208
      message: '开始'
    })
  },
  poppause(e) {
209
    promptAction.showToast({
H
HelloCrease 已提交
210 211 212 213
      message: '暂停'
    })
  },
  popstop(e) {
214
    promptAction.showToast({
H
HelloCrease 已提交
215 216 217 218
      message: '停止'
    })
  },
  popresume(e) {
219
    promptAction.showToast({
H
HelloCrease 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232
      message: '恢复'
    })
  }
}
```

![zh-cn_image_0000001263278477](figures/zh-cn_image_0000001263278477.gif)


## 场景示例

在本场景中,开发者可通过开始播放、停止播放等按钮切换图片的播放状态。

H
HelloCrease 已提交
233
image-animator组件通过调用start、pause、stop和resume方法控制图片的开始、暂停、停止和重新播放,通过getState方法查询图片的播放状态。
H
HelloCrease 已提交
234 235


H
HelloCrease 已提交
236
```html
H
HelloCrease 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
<!-- xxx.hml -->
<div class="doc-page">
  <image-animator class="img" id="img" images="{{imginfo}}" iteration="2" reverse="{{rev}}" duration="10s">
  </image-animator>
  <div style="width: 700px;height:450px;margin-top: 40px;flex-direction:column;justify-content:space-around;">
    <div class="container">
      <button type="capsule" value="开始播放" onclick="startimg"></button>
      <button type="capsule" value="暂停播放" onclick="pauseimg"></button>
    </div>
    <div class="container">
      <button type="capsule" value="停止播放" onclick="stopimg"></button>
      <button type="capsule" value="重新播放" onclick="resumeimg"></button>
    </div>
    <div class="container">
      <button type="capsule" value="获取播放状态" onclick="getimgstate"></button>
      <button type="capsule" value="{{revVal}}" onclick="revimg"></button>
    </div>
  </div>
</div>
```


H
HelloCrease 已提交
259
```css
H
HelloCrease 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/* xxx.css */
.doc-page {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}
.img {
  width: 600px;
  height: 600px;
  border: 3px solid orange;
}
button{
  width: 260px
}
.container {
  width: 100%;
  height: 120px;
  align-items: center;
  justify-content: space-around;
}
```


H
HelloCrease 已提交
286 287
```js
// index.js
Y
yamila 已提交
288
import promptAction from '@ohos.promptAction';
H
HelloCrease 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
export default {
  data: {
    rev:false,
    imginfo: [
      {
        src: 'common/landscape1.jpg',
      },{
        src: 'common/landscape2.jpg',
      },{
        src: 'common/landscape3.jpg',
      },{
        src: 'common/landscape4.jpg',
      }
    ],
    revVal: '反向播放'
  },
  onInit() {
  },
  startimg(e) {
    this.$element('img').start()
  },
  pauseimg(e) {
    this.$element('img').pause()
  },
  stopimg(e) {
    this.$element('img').stop()
  },
  resumeimg(e) {
    this.$element('img').resume()
  },
  getimgstate(e) {
Y
yamila 已提交
320
    promptAction.showToast({
H
HelloCrease 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
      message: '当前状态:' + this.$element('img').getState()
    })
  },
  revimg(e) {
    this.rev = !this.rev
    if (this.rev) {
      this.revVal = '正向播放'
    } else {
      this.revVal = '反向播放'
    }
  }
}
```

![zh-cn_image_0000001218758816](figures/zh-cn_image_0000001218758816.gif)
336 337 338 339


## 相关实例

H
HelloCrease 已提交
340
针对image-animator开发,有以下相关实例可供参考:
341

342
- [image、image-animator组件的使用(JS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/JSUI/ClickableJs)