js-apis-animator.md 11.6 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.animator (动画)
Z
zengyawen 已提交
2

T
explain  
tianyu 已提交
3 4
本模块提供组件动画效果,包括定义动画、启动动画和以相反的顺序播放动画等。

5
> **说明:**
H
HelloCrease 已提交
6
>
Z
zengyawen 已提交
7
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

## 导入模块
Z
zengyawen 已提交
11

H
HelloCrease 已提交
12
```js
Z
zengyawen 已提交
13 14
import animator from '@ohos.animator';
```
Z
zhaoxinyu 已提交
15
## create<sup>9+</sup>
Z
zengyawen 已提交
16

Z
zhaoxinyu 已提交
17
create(options: AnimatorOptions): AnimatorResult
Z
zengyawen 已提交
18

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

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

23
**参数:** 
Z
zhaoxinyu 已提交
24

H
HelloCrease 已提交
25 26 27
| 参数名     | 类型                                  | 必填   | 说明      |
| ------- | ----------------------------------- | ---- | ------- |
| options | [AnimatorOptions](#animatoroptions) | 是    | 定义动画选项。 |
Z
zengyawen 已提交
28

29
**返回值:** 
30

H
HelloCrease 已提交
31 32
| 类型                                | 说明            |
| --------------------------------- | ------------- |
T
explain  
tianyu 已提交
33
| [AnimatorResult](#animatorresult) | Animator结果接口。 |
Z
zengyawen 已提交
34

35
**示例:** 
Z
zengyawen 已提交
36

H
HelloCrease 已提交
37
  ```js
L
luoying_ace_admin 已提交
38
  let options = {
Z
zhaoxinyu 已提交
39 40 41 42
    duration: 1500,
    easing: 'friction',
    delay: 0,
    fill: 'forwards',
Y
yamila 已提交
43
    direction: 'normal',
Z
zhaoxinyu 已提交
44 45
    iterations: 3,
    begin: 200.0,
Y
yamila 已提交
46
    end: 400.0
Z
zhaoxinyu 已提交
47 48
  };
  animator.create(options);
Z
zengyawen 已提交
49
  ```
50 51 52 53 54

## AnimatorResult

定义Animator结果接口。

Z
zhaoxinyu 已提交
55 56 57
### reset<sup>9+</sup>

reset(options: AnimatorOptions): void
58 59 60 61 62 63

更新当前动画器。

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

**参数:** 
Z
zhaoxinyu 已提交
64

H
HelloCrease 已提交
65 66 67
| 参数名     | 类型                                  | 必填   | 说明      |
| ------- | ----------------------------------- | ---- | ------- |
| options | [AnimatorOptions](#animatoroptions) | 是    | 定义动画选项。 |
68

Z
zhaoxinyu 已提交
69 70 71 72
**错误码:**

以下错误码的详细介绍请参见[ohos.animator(动画)](../errorcodes/errorcode-animator.md)错误码。

73
| 错误码ID   | 错误信息 |
Z
zhaoxinyu 已提交
74
| --------- | ------- |
Y
yamila 已提交
75
| 100001    | If no page is found for pageId or fail to get object property list. |
Z
zhaoxinyu 已提交
76 77


78
**示例:**
Z
zhaoxinyu 已提交
79

H
HelloCrease 已提交
80
```js
L
luoying_ace_admin 已提交
81
let options = {
Z
zhaoxinyu 已提交
82 83 84 85
  duration: 1500,
  easing: 'friction',
  delay: 0,
  fill: 'forwards',
Y
yamila 已提交
86
  direction: 'normal',
Z
zhaoxinyu 已提交
87 88
  iterations: 3,
  begin: 200.0,
Y
yamila 已提交
89
  end: 400.0
Z
zhaoxinyu 已提交
90 91 92 93 94 95
};
try {
  animator.reset(options);
} catch(error) {
  console.error(`Animator reset failed, error code: ${error.code}, message: ${error.message}.`);
}
96 97 98 99 100 101
```

### play

play(): void

102 103
启动动画。

104 105 106
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**示例:**
Z
zhaoxinyu 已提交
107

H
HelloCrease 已提交
108
```js
109
animator.play();
110 111 112 113 114 115
```

### finish

finish(): void

116 117
结束动画。

118 119 120
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**示例:**
Z
zhaoxinyu 已提交
121

H
HelloCrease 已提交
122
```js
123
animator.finish();
124 125 126 127 128 129
```

### pause

pause(): void

130 131
暂停动画。

132 133 134
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**示例:**
Z
zhaoxinyu 已提交
135

H
HelloCrease 已提交
136
```js
137
animator.pause();
138 139 140 141 142 143
```

### cancel

cancel(): void

144 145
删除动画。

146 147 148
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full

**示例:**
Z
zhaoxinyu 已提交
149

H
HelloCrease 已提交
150
```js
151
animator.cancel();
152 153 154 155 156 157
```

### reverse

reverse(): void

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

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

162
**示例:**
Z
zhaoxinyu 已提交
163

H
HelloCrease 已提交
164
```js
165
animator.reverse();
166 167
```

168 169 170 171
### onframe

onframe: (progress: number) => void

172
接收到帧时回调。
173

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

176
**参数:** 
Z
zhaoxinyu 已提交
177

H
HelloCrease 已提交
178 179 180
| 参数名      | 类型     | 必填   | 说明       |
| -------- | ------ | ---- | -------- |
| progress | number | 是    | 动画的当前进度。 |
181 182

**示例:**
183

H
HelloCrease 已提交
184
```js
L
liyujie 已提交
185 186 187 188
let animatorResult = animator.create(options)
animatorResult.onframe = function(value) {
  console.info("onframe callback")
}
189
```
190 191 192 193 194

### onfinish

onfinish: () => void

195
动画完成时回调。
196

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

199
**示例:**
Z
zhaoxinyu 已提交
200

H
HelloCrease 已提交
201
```js
L
liyujie 已提交
202 203 204 205
let animatorResult = animator.create(options)
animatorResult.onfinish = function() {
  console.info("onfinish callback")
}
206 207
```

208 209 210
### oncancel

oncancel: () => void
211

212
动画被取消时回调。
213

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

216
**示例:**
Z
zhaoxinyu 已提交
217

H
HelloCrease 已提交
218
```js
L
liyujie 已提交
219 220 221 222
let animatorResult = animator.create(options)
animatorResult.oncancel = function() {
  console.info("oncancel callback")
}
223 224
```

225 226 227 228
### onrepeat

onrepeat: () => void

229 230
动画重复时回调。

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

233
**示例:**
Z
zhaoxinyu 已提交
234

H
HelloCrease 已提交
235
```js
L
liyujie 已提交
236 237 238 239
let animatorResult = animator.create(options)
animatorResult.onrepeat = function() {
  console.info("onrepeat callback")
}
240 241
```

Z
zhaoxinyu 已提交
242 243


244 245 246 247
## AnimatorOptions

定义动画选项。

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

250
| 名称         | 类型                                     | 必填   | 说明                                       |
H
HelloCrease 已提交
251 252 253 254 255 256 257 258 259
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
| 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。                             |
Z
zhaoxinyu 已提交
260 261 262


## 完整示例
L
liyujie 已提交
263
### 基于JS扩展的类Web开发范式
Z
zhaoxinyu 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

```html
<!-- hml -->
<div class="container">
  <div class="Animation" style="height: {{divHeight}}px; width: {{divWidth}}px; background-color: red;" onclick="Show">
  </div>
</div>
```

```js
export default {
  data: {
    divWidth: 200,
    divHeight: 200,
    animator: null
  },
  onInit() {
L
luoying_ace_admin 已提交
281
    let options = {
Z
zhaoxinyu 已提交
282 283 284 285
      duration: 1500,
      easing: 'friction',
      delay: 0,
      fill: 'forwards',
Y
yamila 已提交
286
      direction: 'normal',
Z
zhaoxinyu 已提交
287 288 289 290 291 292 293
      iterations: 2,
      begin: 200.0,
      end: 400.0
    };
    this.animator = animator.create(options);
  },
  Show() {
L
luoying_ace_admin 已提交
294
    let options1 = {
Z
zhaoxinyu 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308
      duration: 1500,
      easing: 'friction',
      delay: 0,
      fill: 'forwards',
      direction: "normal",
      iterations: 2,
      begin: 0,
      end: 400.0,
    };
    try {
      this.animator.reset(options1);
    } catch(error) {
      console.error(`Animator reset failed, error code: ${error.code}, message: ${error.message}.`);
    }
L
luoying_ace_admin 已提交
309
    let _this = this;
Z
zhaoxinyu 已提交
310 311 312 313 314 315 316 317 318 319 320
    this.animator.onframe = function(value) {
      _this.divWidth = value;
      _this.divHeight = value;
    };
    this.animator.play();
  }
}
```

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

L
liyujie 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
### 基于TS扩展的声明式开发范式

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

@Entry
@Component
struct AnimatorTest {
  private TAG: string = '[AnimatorTest]'
  private backAnimator: any = undefined
  private flag: boolean = false
  @State wid: number = 100
  @State hei: number = 100

  create() {
    let _this = this
    this.backAnimator = animator.create({
      duration: 2000,
      easing: 'ease',
      delay: 0,
      fill: 'none',
      direction: 'normal',
      iterations: 1,
      begin: 100,
      end: 200
    })
    this.backAnimator.onfinish = function () {
      _this.flag = true
      console.info(_this.TAG, 'backAnimator onfinish')
    }
    this.backAnimator.onrepeat = function () {
      console.info(_this.TAG, 'backAnimator repeat')
    }
    this.backAnimator.oncancel = function () {
      console.info(_this.TAG, 'backAnimator cancel')
    }
    this.backAnimator.onframe = function (value) {
      _this.wid = value
      _this.hei = value
    }
  }

  build() {
    Column() {
      Column() {
        Column()
          .width(this.wid)
          .height(this.hei)
          .backgroundColor(Color.Red)
      }
      .width('100%')
      .height(300)

      Column() {
        Row() {
          Button('create')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.create()
            })
        }
        .padding(10)

        Row() {
          Button('play')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.flag = false
              this.backAnimator.play()
            })
        }
        .padding(10)

        Row() {
          Button('pause')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.backAnimator.pause()
            })
        }
        .padding(10)

        Row() {
          Button('finish')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.flag = true
              this.backAnimator.finish()
            })
        }
        .padding(10)

        Row() {
          Button('reverse')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.flag = false
              this.backAnimator.reverse()
            })
        }
        .padding(10)

        Row() {
          Button('cancel')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.backAnimator.cancel()
            })
        }
        .padding(10)

        Row() {
          Button('reset')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              if (this.flag) {
                this.flag = false
                this.backAnimator.reset({
                  duration: 5000,
                  easing: 'ease-in',
                  delay: 0,
                  fill: 'none',
                  direction: 'normal',
                  iterations: 4,
                  begin: 100,
                  end: 300
                })
              } else {
                console.info(this.TAG, 'Animation not ended')
              }
            })
        }
        .padding(10)
      }
    }
  }
}
```

Z
zhaoxinyu 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
## update<sup>(deprecated)</sup>

update(options: AnimatorOptions): void

更新当前动画器。

从API version9开始不再维护,建议使用[reset<sup>9+</sup>](#reset9)

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

**参数:** 

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

**示例:**

```js
animator.update(options);
```

## createAnimator<sup>(deprecated)</sup>

createAnimator(options: AnimatorOptions): AnimatorResult

定义Animator类。

从API version9开始不再维护,建议使用[create<sup>9+</sup>](#create9)

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

**参数:** 

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

**返回值:** 

| 类型                                | 说明            |
| --------------------------------- | ------------- |
| [AnimatorResult](#animatorresult) | Animator结果接口。 |

**示例:** 

```js
L
luoying_ace_admin 已提交
514
let options = {
Z
zhaoxinyu 已提交
515 516 517 518
  duration: 1500,
  easing: 'friction',
  delay: 0,
  fill: 'forwards',
Y
yamila 已提交
519
  direction: 'normal',
Z
zhaoxinyu 已提交
520 521 522 523 524 525
  iterations: 3,
  begin: 200.0,
  end: 400.0,
};
this.animator = animator.createAnimator(options);
```