ts-basic-components-imageanimator.md 6.0 KB
Newer Older
Z
zengyawen 已提交
1 2
# ImageAnimator

Z
zengyawen 已提交
3 4
提供帧动画组件来实现逐帧播放图片的能力,可以配置需要播放的图片列表,每张图片可以配置时长。

H
hebingxue 已提交
5 6 7
>  **说明:**
>
> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10


Z
zengyawen 已提交
11
## 子组件
Z
zengyawen 已提交
12 13 14 15




Z
zengyawen 已提交
16
## 接口
Z
zengyawen 已提交
17

Z
zengyawen 已提交
18
ImageAnimator()
Z
zengyawen 已提交
19 20


Z
zengyawen 已提交
21
## 属性
Z
zengyawen 已提交
22

23 24
| 参数名称     | 参数类型                  |参数描述                   |
| ---------- | ----------------------- |-------- |
25
| images     | Array&lt;[ImageFrameInfo](#imageframeinfo对象说明)&gt; | 设置图片帧信息集合。每一帧的帧信息(ImageFrameInfo)包含图片路径、图片大小、图片位置和图片播放时长信息,详见ImageFrameInfo属性说明。<br/>默认值:[]  |
L
luoying_ace_admin 已提交
26
| state      | [AnimationStatus](ts-appendix-enums.md#animationstatus) |  默认为初始状态,用于控制播放状态。<br/>默认值:AnimationStatus.Initial |
27 28 29 30
| duration   | number  | 单位为毫秒,默认时长为1000ms;duration为0时,不播放图片;值的改变只会在下一次循环开始时生效;当images中任意一帧图片设置了单独的duration后,该属性设置无效。<br/>默认值:1000 |
| reverse    | boolean | 设置播放顺序。false表示从第1张图片播放到最后1张图片;&nbsp;true表示从最后1张图片播放到第1张图片。<br/>默认值:false |
| fixedSize  | boolean | 设置图片大小是否固定为组件大小。&nbsp;true表示图片大小与组件大小一致,此时设置图片的width&nbsp;、height&nbsp;、top&nbsp;和left属性是无效的。false表示每一张图片的width&nbsp;、height&nbsp;、top和left属性都要单独设置。<br/>默认值:true |
| preDecode  | number  | 是否启用预解码,默认值为0,即不启用预解码,如该值设为2,则播放当前页时会提前加载后面两张图片至缓存以提升性能。<br/>默认值:0 |
31
| fillMode   | [FillMode](ts-appendix-enums.md#fillmode) | 否    | 设置动画开始前和结束后的状态,可选值参见FillMode说明。<br/>默认值:FillMode.Forwards |
32
| iterations | number  | 默认播放一次,设置为-1时表示无限次播放。<br/>默认值:1 |
Z
zengyawen 已提交
33

34
## ImageFrameInfo对象说明
H
hebingxue 已提交
35 36 37

| 参数名称   | 参数类型   | 必填 | 参数描述 |
| -------- | -------------- | -------- | -------- |
38
| src      | string \| [Resource](ts-types.md#resource)<sup>9+</sup> | 是    | 图片路径,图片格式为svg,png和jpg,从API Version9开始支持[Resource](ts-types.md#resource)类型的路径。|
H
hebingxue 已提交
39 40 41 42 43
| width    | [Length](ts-types.md#length)  | 否  | 图片宽度。<br/>默认值:0          |
| height   | [Length](ts-types.md#length)  | 否  | 图片高度。<br/>默认值:0             |
| top      | [Length](ts-types.md#length)  | 否  | 图片相对于组件左上角的纵向坐标。<br/>默认值:0    |
| left     | [Length](ts-types.md#length)  | 否  | 图片相对于组件左上角的横向坐标。<br/>默认值:0    |
| duration | number          | 否     | 每一帧图片的播放时长,单位毫秒。<br/>默认值:0               |
44

Z
zengyawen 已提交
45

Z
zengyawen 已提交
46
## 事件
Z
zengyawen 已提交
47

G
gmy 已提交
48
| 名称 | 功能描述 |
49
| -------- | -------- |
K
kangchongtao 已提交
50 51
| onStart(event:&nbsp;()&nbsp;=&gt;&nbsp;void)  | 状态回调,动画开始播放时触发。 |
| onPause(event:&nbsp;()&nbsp;=&gt;&nbsp;void)  | 状态回调,动画暂停播放时触发。 |
L
luoying_ace_admin 已提交
52
| onRepeat(event:&nbsp;()&nbsp;=&gt;&nbsp;void) | 状态回调,动画重复播放时触发。 |
K
kangchongtao 已提交
53 54
| onCancel(event:&nbsp;()&nbsp;=&gt;&nbsp;void) | 状态回调,动画取消播放时触发。 |
| onFinish(event:&nbsp;()&nbsp;=&gt;&nbsp;void) | 状态回调,动画播放完成时触发。 |
Z
zengyawen 已提交
55 56


Z
zengyawen 已提交
57
## 示例
Z
zengyawen 已提交
58

H
geshi  
HelloCrease 已提交
59 60
```ts
// xxx.ets
Z
zengyawen 已提交
61 62 63 64 65 66 67 68
@Entry
@Component
struct ImageAnimatorExample {
  @State state: AnimationStatus = AnimationStatus.Initial
  @State reverse: boolean = false
  @State iterations: number = 1

  build() {
L
luoying_ace_admin 已提交
69
    Column({ space: 10 }) {
Z
zengyawen 已提交
70 71
      ImageAnimator()
        .images([
L
luoying_ace_admin 已提交
72 73
          {
            src: $r('app.media.img1'),
Z
zengyawen 已提交
74
            duration: 500,
L
luoying_ace_admin 已提交
75 76
            width: 170,
            height: 120,
Z
zengyawen 已提交
77 78 79 80
            top: 0,
            left: 0
          },
          {
L
luoying_ace_admin 已提交
81
            src: $r('app.media.img2'),
Z
zengyawen 已提交
82
            duration: 500,
L
luoying_ace_admin 已提交
83 84
            width: 170,
            height: 120,
Z
zengyawen 已提交
85
            top: 0,
L
luoying_ace_admin 已提交
86
            left: 170
Z
zengyawen 已提交
87 88
          },
          {
L
luoying_ace_admin 已提交
89
            src: $r('app.media.img3'),
Z
zengyawen 已提交
90
            duration: 500,
L
luoying_ace_admin 已提交
91 92 93 94
            width: 170,
            height: 120,
            top: 120,
            left: 170
Z
zengyawen 已提交
95 96
          },
          {
L
luoying_ace_admin 已提交
97
            src: $r('app.media.img4'),
Z
zengyawen 已提交
98
            duration: 500,
L
luoying_ace_admin 已提交
99 100 101
            width: 170,
            height: 120,
            top: 120,
Z
zengyawen 已提交
102 103 104 105
            left: 0
          }
        ])
        .state(this.state).reverse(this.reverse).fixedSize(false).preDecode(2)
L
luoying_ace_admin 已提交
106 107 108
        .fillMode(FillMode.None).iterations(this.iterations).width(340).height(240)
        .margin({ top: 100 })
        .onStart(() => {
Z
zengyawen 已提交
109 110 111 112 113 114 115 116 117 118 119
          console.info('Start')
        })
        .onPause(() => {
          console.info('Pause')
        })
        .onRepeat(() => {
          console.info('Repeat')
        })
        .onCancel(() => {
          console.info('Cancel')
        })
L
luoying_ace_admin 已提交
120
        .onFinish(() => {
Z
zengyawen 已提交
121 122 123 124 125
          console.info('Finish')
        })
      Row() {
        Button('start').width(100).padding(5).onClick(() => {
          this.state = AnimationStatus.Running
L
luoying_ace_admin 已提交
126
        }).margin(5)
Z
zengyawen 已提交
127
        Button('pause').width(100).padding(5).onClick(() => {
L
luoying_ace_admin 已提交
128 129
          this.state = AnimationStatus.Paused     // 显示当前帧图片
        }).margin(5)
Z
zengyawen 已提交
130
        Button('stop').width(100).padding(5).onClick(() => {
L
luoying_ace_admin 已提交
131 132
          this.state = AnimationStatus.Stopped    // 显示动画的起始帧图片
        }).margin(5)
Z
zengyawen 已提交
133
      }
L
luoying_ace_admin 已提交
134

Z
zengyawen 已提交
135 136 137
      Row() {
        Button('reverse').width(100).padding(5).onClick(() => {
          this.reverse = !this.reverse
L
luoying_ace_admin 已提交
138
        }).margin(5)
Z
zengyawen 已提交
139 140
        Button('once').width(100).padding(5).onClick(() => {
          this.iterations = 1
L
luoying_ace_admin 已提交
141 142 143 144
        }).margin(5)
        Button('infinite').width(100).padding(5).onClick(() => {
          this.iterations = -1 // 无限循环播放
        }).margin(5)
Z
zengyawen 已提交
145
      }
L
luoying_ace_admin 已提交
146
    }.width('100%').height('100%')
Z
zengyawen 已提交
147 148 149 150
  }
}
```