js-apis-basic-features-animator.md 4.7 KB
Newer Older
Z
zengyawen 已提交
1
# 动画
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Z
zengyawen 已提交
5

Z
zengyawen 已提交
6 7

## 导入模块
Z
zengyawen 已提交
8 9 10 11 12 13

```
import animator from '@ohos.animator';
```


14
## createAnimator
Z
zengyawen 已提交
15

16
createAnimator(options: AnimatorOptions): AnimatorResult
Z
zengyawen 已提交
17

18
定义Animator类。
Z
zengyawen 已提交
19

20
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
21

22
**参数:** 
Z
zengyawen 已提交
23 24
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
25
  | options | [AnimatorOptions](#animatoroptions) | 是 | 定义动画选项,详细请参考AnimatorOptions。|
Z
zengyawen 已提交
26

27
**返回值:** 
Z
zengyawen 已提交
28 29
  | 类型 | 说明 |
  | -------- | -------- |
30
  | [AnimatorResult](#animatorresult) | Animator结果接口。 |
Z
zengyawen 已提交
31

32
**示例:** 
Z
zengyawen 已提交
33

Z
zengyawen 已提交
34 35 36 37
  ```
  <!-- hml -->
  <div class="container">
    <div class="Animation" style="height: {{divHeight}}px; width: {{divWidth}}px; background-color: red;" onclick="Show">
Z
zengyawen 已提交
38
    </div>
Z
zengyawen 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  </div>
  ```

  ```
  // js
  export default {
    data : {
      divWidth: 200,
      divHeight: 200,
      animator: null
    },
    onInit() {
      var options = {
        duration: 1500,
        easing: 'friction',
        fill: 'forwards',
        iterations: 2,
        begin: 200.0,
        end: 400.0
      };
      this.animator = animator.createAnimator(options);
    },
    Show() {
      var options1 = {
        duration: 2000,
        easing: 'friction',
        fill: 'forwards',
        iterations: 1,
        begin: 200.0,
        end: 400.0
      };
      this.animator.update(options1);
      var _this = this;
      this.animator.onframe = function(value) {
        _this.divWidth = value;
        _this.divHeight = value;
      };
      this.animator.play();
Z
zengyawen 已提交
77
    }
Z
zengyawen 已提交
78 79
  }
  ```
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

## AnimatorResult

定义Animator结果接口。

### update

update(options: AnimatorOptions): void

更新当前动画器。

**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**参数:** 
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | options | [AnimatorOptions](#animatoroptions) | 是 | 定义动画选项。|

**示例:**
```
100
animator.update(options);
101 102 103 104 105 106
```

### play

play(): void

107 108
启动动画。

109 110 111 112
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**示例:**
```
113
animator.play();
114 115 116 117 118 119
```

### finish

finish(): void

120 121
结束动画。

122 123 124 125
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**示例:**
```
126
animator.finish();
127 128 129 130 131 132
```

### pause

pause(): void

133 134
暂停动画。

135 136 137 138
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**示例:**
```
139
animator.pause();
140 141 142 143 144 145
```

### cancel

cancel(): void

146 147
删除动画。

148 149 150 151
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**示例:**
```
152
animator.cancel();
153 154 155 156 157 158
```

### reverse

reverse(): void

159 160
以相反的顺序播放动画。

161 162
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

163 164
**示例:**
```
165
animator.reverse();
166 167
```

168 169 170 171
### onframe

onframe: (progress: number) => void

172 173
回调时触发。

174 175
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

176 177 178 179 180 181 182
**参数:** 
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | progress | number | 是 | 动画的当前进度。|

**示例:**
```
183
animator.onframe();
184
```
185 186 187 188 189

### onfinish

onfinish: () => void

190 191
动画完成。

192 193
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

194 195
**示例:**
```
196
animator.onfinish();
197 198
```

199 200 201 202
### oncancel

oncancel: () => void

203 204
动画被取消。

205 206
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

207 208
**示例:**
```
209
animator.oncancel();
210 211
```

212 213 214 215 216 217
### onrepeat

onrepeat: () => void

**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

218 219
**示例:**
```
220
animator.onrepeat();
221 222 223
```

动画将重复。
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

## AnimatorOptions

定义动画选项。

**系统能力:**  以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full

| 名称 | 参数类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| duration | number | 是 | 动画播放的时长,单位毫秒,默认为0。 |
| easing | string | 是 | 动画插值曲线,默认为ease'。 |
| delay | number | 是 | 动画延时播放时长,单位毫秒,默认为0,即不延时。 |
| fill | "none" \| "forwards" \| "backwards" \| "both" | 是 | 动画执行后是否恢复到初始状态,默认值为"none"。动画执行后,动画结束时的状态(在最后一个关键帧中定义)将保留。 |
| direction | "normal" \| "reverse" \| "alternate" \| "alternate-reverse" | 是 | 动画播放模式,默认值"normal"。|
| iterations | number | 是 | 动画播放次数,默认值1。设置为0时不播放,设置为-1时无限次播放。 |
| begin | number | 是 | 动画插值起点,不设置时默认为0。 |
| end | number | 是 | 动画插值终点,不设置时默认为1。 |