js-apis-animator.md 5.1 KB
Newer Older
E
ester.zhou 已提交
1 2 3 4
# Animator

> **NOTE**<br>
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5 6


7
## Modules to Import
Z
zengyawen 已提交
8

E
ester.zhou 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 77 78 79 80 81 82 83 84 85 86 87 88 89
```
import animator from '@ohos.animator';
```


## createAnimator

createAnimator(options: AnimatorOptions): AnimatorResult

Creates an **Animator** object.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| options | [AnimatorOptions](#animatoroptions) | Yes| Animator options. For details, see **AnimatorOptions**.|

**Return value**
| Type| Description|
| -------- | -------- |
| [AnimatorResult](#animatorresult) | Animator result.|

**Example**

  ```
  <!-- 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() {
      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();
    }
  }
  ```

## AnimatorResult

Defines the animator result.

### update

update(options: AnimatorOptions): void

Updates this animator.
Z
zengyawen 已提交
90

E
ester.zhou 已提交
91
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
92

E
ester.zhou 已提交
93 94 95 96
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| options | [AnimatorOptions](#animatoroptions) | Yes| Animator options.|
Z
zengyawen 已提交
97

E
ester.zhou 已提交
98
**Example**
Z
zengyawen 已提交
99
```
E
ester.zhou 已提交
100
animator.update(options);
Z
zengyawen 已提交
101 102
```

E
ester.zhou 已提交
103
### play
Z
zengyawen 已提交
104

E
ester.zhou 已提交
105
play(): void
Z
zengyawen 已提交
106

E
ester.zhou 已提交
107
Plays this animation.
Z
zengyawen 已提交
108

E
ester.zhou 已提交
109
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
110

E
ester.zhou 已提交
111 112 113 114
**Example**
```
animator.play();
```
Z
zengyawen 已提交
115

E
ester.zhou 已提交
116
### finish
Z
zengyawen 已提交
117

E
ester.zhou 已提交
118
finish(): void
Z
zengyawen 已提交
119

E
ester.zhou 已提交
120
Ends this animation.
Z
zengyawen 已提交
121

E
ester.zhou 已提交
122
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
123

E
ester.zhou 已提交
124 125 126 127
**Example**
```
animator.finish();
```
Z
zengyawen 已提交
128

E
ester.zhou 已提交
129
### pause
Z
zengyawen 已提交
130

E
ester.zhou 已提交
131
pause(): void
Z
zengyawen 已提交
132

E
ester.zhou 已提交
133
Pauses this animation.
Z
zengyawen 已提交
134

E
ester.zhou 已提交
135
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
136

E
ester.zhou 已提交
137 138 139 140
**Example**
```
animator.pause();
```
Z
zengyawen 已提交
141

E
ester.zhou 已提交
142
### cancel
Z
zengyawen 已提交
143

E
ester.zhou 已提交
144
cancel(): void
Z
zengyawen 已提交
145

E
ester.zhou 已提交
146
Cancels this animation.
Z
zengyawen 已提交
147

E
ester.zhou 已提交
148
**System capability**: SystemCapability.ArkUI.ArkUI.Full
Z
zengyawen 已提交
149

E
ester.zhou 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
**Example**
```
animator.cancel();
```

### reverse

reverse(): void

Plays this animation in reverse order.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
```
animator.reverse();
```

### onframe

onframe: (progress: number) => void

Called when a frame is received.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| progress | number | Yes| Current progress of the animation.|

**Example**
```
animator.onframe();
```

### onfinish

onfinish: () => void

Called when this animation is finished.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
```
animator.onfinish();
```

### oncancel

oncancel: () => void

Called when this animation is canceled.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
```
animator.oncancel();
```

### onrepeat

onrepeat: () => void

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**
```
animator.onrepeat();
```

Called when this animation repeats.

## AnimatorOptions

Defines animator options.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| duration | number | Yes| Duration for playing the animation, in milliseconds. The default value is **0**.|
| easing | string | Yes| Animation interpolation curve. The default value is **ease**.|
| delay | number | Yes| Animation delay duration, in milliseconds. The default value is **0**, indicating that there is no delay.|
| fill | "none" \| "forwards" \| "backwards" \| "both" | Yes| State of the animated target after the animation is executed. The default value is **none**, which means that the target will retain its end state (defined by the last keyframe) after the animation is executed. |
| direction | "normal" \| "reverse" \| "alternate" \| "alternate-reverse" | Yes| Animation playback mode. The default value is **normal**.|
| iterations | number | Yes| Number of times that the animation is played. The default value is **1**. The value **0** means not to play the animation, and **-1** means to play the animation for an unlimited number of times.|
| begin | number | Yes| Start point of the animation interpolation. If this parameter is not set, the default value **0** is used.|
| end | number | Yes| End point of the animation interpolation. If this parameter is not set, the default value **1** is used.|