文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明。
API参考应用框架ArkUI(方舟UI框架)ArkTS APIUI界面@ohos.animator (动画)

@ohos.animator (动画)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

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

说明
  • 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

  • 本模块从API version 9开始支持在ArkTS中使用。

  • 该模块不支持在UIAbility的文件声明处使用,即不能在UIAbility的生命周期中调用,需要在创建组件实例后使用。

  • 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见UIContext说明。

  • 自定义组件中通常会持有一个由createAnimator接口返回的AnimatorResult对象,以确保动画对象在动画过程中不被析构,该对象通过回调捕获了自定义组件对象,因此需要在自定义组件销毁时的aboutToDisappear生命周期中释放动画对象,以避免因循环依赖导致内存泄漏。详细示例可参考:基于ArkTS扩展的声明式开发范式。

  • Animator对象析构或主动调用cancel、finish方法时,都会触发一次额外的onFrame,返回值是动画终点值。因此,如果在动画过程中调用cancel、finish,会导致属性值在一帧内跳变至终点。若希望动画在中途暂停,可先将onFrame设置为空函数,再调用finish。

  • 对于无限循环的Animator动画,即使开发者选项中将全局动画速率设置为0(关闭动画),循环动画仍会继续执行。

导入模块

收起
自动换行
深色代码主题
复制
  1. import { Animator as animator, AnimatorOptions, AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';

Animator

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

定义Animator类。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

create(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

create(options: AnimatorOptions): AnimatorResult

创建animator动画结果对象(AnimatorResult)。

说明
  • 从API version 9开始支持,从API version 18开始废弃,建议使用createAnimator替代。

  • 从API version 10开始,可以通过使用UIContext中的createAnimator来明确UI的执行上下文。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

参数:

展开
参数名 类型 必填 说明
options AnimatorOptions 是 动画配置选项,包含播放时长、插值曲线、延时、填充模式、播放方向、播放次数及插值起止值等参数。

返回值:

展开
类型 说明
AnimatorResult 动画控制对象,可用于设置动画过程中的回调函数。

错误码:

以下错误码详细介绍请参考通用错误码。

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

说明

推荐通过使用UIContext中的createAnimator接口明确UI上下文。

收起
自动换行
深色代码主题
复制
  1. import { Animator as animator, AnimatorOptions } from '@kit.ArkUI';
  2. let options: AnimatorOptions = {
  3. duration: 1500,
  4. easing: 'friction',
  5. delay: 0,
  6. fill: "forwards",
  7. direction: "normal",
  8. iterations: 3,
  9. begin: 200.0,
  10. end: 400.0
  11. };
  12. animator.create(options); // 建议使用 UIContext.createAnimator()接口

create18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

create(options: AnimatorOptions | SimpleAnimatorOptions): AnimatorResult

创建animator动画结果对象(AnimatorResult)。与create相比,新增对SimpleAnimatorOptions类型入参的支持。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
options AnimatorOptions | SimpleAnimatorOptions 是 定义动画选项。AnimatorOptions适用于需要完整自定义所有动画参数的场景;SimpleAnimatorOptions适用于仅需指定起点和终点的简易动画场景,其余参数使用默认值。

返回值:

展开
类型 说明
AnimatorResult 动画控制对象,可设置动画过程中的回调函数。

错误码:

以下错误码详细介绍请参考通用错误码。

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

说明

推荐通过使用UIContext中的createAnimator接口明确UI上下文。

收起
自动换行
深色代码主题
复制
  1. import { Animator as animator, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. let options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).duration(2000);
  3. animator.create(options); // 建议使用 UIContext.createAnimator()接口

createAnimator(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

createAnimator(options: AnimatorOptions): AnimatorResult

创建动画。本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,推荐通过使用UIContext中的createAnimator接口明确UI上下文。

说明
  • 从API version 6开始支持,从API version 9开始废弃,建议使用create替代。
  • 从API version 10开始,可以通过使用UIContext中的createAnimator来明确UI的执行上下文。

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

参数:

展开
参数名 类型 必填 说明
options AnimatorOptions 是 动画配置选项,用于定义动画的播放时长、插值曲线、延时、填充模式、播放方向、播放次数及插值起止值等参数。

返回值:

展开
类型 说明
AnimatorResult 动画控制对象,可设置动画过程中的回调函数。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { Animator as animator, AnimatorOptions } from '@kit.ArkUI';
  2. let options: AnimatorOptions = { // xxx.js文件中不需要强调显式类型AnimatorOptions
  3. duration: 1500,
  4. easing: "friction",
  5. delay: 0,
  6. fill: "forwards",
  7. direction: "normal",
  8. iterations: 3,
  9. begin: 200.0,
  10. end: 400.0,
  11. };
  12. this.animator = animator.createAnimator(options);

AnimatorResult

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

定义AnimatorResult接口,提供动画播放状态回调及动画控制方法。

属性

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

展开
名称 类型 只读 可选 说明
onFrame12+ (progress: number) => void 否 否

接收到帧时回调。

progress表示动画的当前值。取值范围为AnimatorOptions定义的[begin, end],默认取值范围为[0, 1]。

说明: 调用cancel、finish方法时,会触发一次额外的onFrame回调,返回值为动画终点值。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

onFinish12+ () => void 否 否

动画完成时回调。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

onCancel12+ () => void 否 否

动画被取消时回调。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

onRepeat12+ () => void 否 否

动画重复时回调。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

onframe(deprecated) (progress: number) => void 否 否

接收到帧时回调。

说明: 从API version 6开始支持,从API version 12开始废弃,推荐使用onFrame。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

onfinish(deprecated) () => void 否 否

动画完成时回调。

说明: 从API version 6开始支持,从API version 12开始废弃,推荐使用onFinish。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

oncancel(deprecated) () => void 否 否

动画被取消时回调。

说明: 从API version 6开始支持,从API version 12开始废弃,推荐使用onCancel。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

onrepeat(deprecated) () => void 否 否

动画重复时回调。

说明: 从API version 6开始支持,从API version 12开始废弃,推荐使用onRepeat。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

reset9+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

reset(options: AnimatorOptions): void

重置当前animator动画参数。建议在动画未开始播放或播放结束后(onFinish或onCancel回调触发后)调用此方法,重置后需调用play方法重新启动动画。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

参数:

展开
参数名 类型 必填 说明
options AnimatorOptions 是 动画配置选项,用于定义动画的播放时长、插值曲线、延时、填充模式、播放方向、播放次数及插值起止值等参数。

错误码:

以下错误码的详细介绍请参考通用错误码和接口调用异常错误码。

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.
100001 The specified page is not found or the object property list is not obtained.

示例:

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private animatorResult: AnimatorResult | undefined = undefined;
  6. create() {
  7. this.animatorResult = this.getUIContext().createAnimator({
  8. duration: 1500,
  9. easing: "friction",
  10. delay: 0,
  11. fill: "forwards",
  12. direction: "normal",
  13. iterations: 3,
  14. begin: 200.0,
  15. end: 400.0
  16. })
  17. this.animatorResult.reset({
  18. duration: 1500,
  19. easing: "friction",
  20. delay: 0,
  21. fill: "forwards",
  22. direction: "normal",
  23. iterations: 5,
  24. begin: 200.0,
  25. end: 400.0
  26. });
  27. }
  28. build() {
  29. // ...
  30. }
  31. }

reset18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

reset(options: AnimatorOptions | SimpleAnimatorOptions): void

重置当前animator动画参数。与reset相比,新增对SimpleAnimatorOptions类型入参的支持。建议在动画未开始播放或播放结束后(onFinish或onCancel回调触发后)调用此方法,重新设置动画参数后调用play启动新动画。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
options AnimatorOptions | SimpleAnimatorOptions 是 定义动画选项。

错误码:

以下错误码的详细介绍请参考通用错误码和接口调用异常错误码。

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.
100001 The specified page is not found or the object property list is not obtained.

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { Animator as animator, AnimatorResult, AnimatorOptions, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. let options: AnimatorOptions = {
  3. duration: 1500,
  4. easing: 'ease',
  5. delay: 0,
  6. fill: "forwards",
  7. direction: "normal",
  8. iterations: 1,
  9. begin: 100,
  10. end: 200
  11. };
  12. let optionsNew: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200)
  13. .duration(2000)
  14. .iterations(3)
  15. .delay(1000);
  16. let animatorResult: AnimatorResult = animator.create(options);
  17. animatorResult.reset(optionsNew);

play

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

play(): void

启动动画。动画暂停后调用此方法可恢复播放。动画会保留上一次的播放状态,比如播放状态设置reverse后,再次播放会保留reverse的播放状态。动画结束后(onFinish或onCancel回调触发后)可再次调用此方法重新播放动画。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. // animator需先通过this.getUIContext().createAnimator()获取AnimatorResult对象
  2. animator.play();

finish

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

finish(): void

结束动画,会触发onFinish回调。与cancel方法功能相同,但cancel()触发onCancel回调,建议使用finish方法结束动画。调用此方法时会触发一次额外的onFrame回调,返回值是动画终点值,可能导致属性值在一帧内跳变至终点。若希望动画在中途暂停,可先将onFrame设置为空函数,再调用finish。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. // animator需先通过this.getUIContext().createAnimator()获取AnimatorResult对象
  2. animator.finish();

pause

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

pause(): void

暂停动画。暂停后可调用play方法恢复播放,也可调用finish或cancel方法结束动画。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. // animator需先通过this.getUIContext().createAnimator()获取AnimatorResult对象
  2. animator.pause();

cancel

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

cancel(): void

取消动画,会触发onCancel回调。此接口和finish接口功能上没有区别,仅触发的回调不同,建议使用finish接口结束动画。调用此方法时会触发一次额外的onFrame回调,返回值是动画终点值,可能导致属性值在一帧内跳变至终点。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. // animator需先通过this.getUIContext().createAnimator()获取AnimatorResult对象
  2. animator.cancel();

reverse

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

reverse(): void

以相反的顺序播放动画。使用interpolating-spring曲线时此接口无效。调用reverse后动画将以相反方向继续播放,可通过pause暂停或finish结束动画。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. // animator需先通过this.getUIContext().createAnimator()获取AnimatorResult对象
  2. animator.reverse();

setExpectedFrameRateRange12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

setExpectedFrameRateRange(rateRange: ExpectedFrameRateRange): void

设置期望的帧率范围,包含最小、最大和期望帧率值。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
rateRange ExpectedFrameRateRange 是 设置期望的帧率范围。
说明

开发者通过设置有效的期望帧率后,系统会收集设置的请求帧率,进行决策和分发,在渲染管线上进行分频,尽量能够满足开发者的期望帧率。开发者设置的期望帧率值不能代表最终实际效果,会受限于系统能力和屏幕刷新率。

示例:

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult } from '@kit.ArkUI';
  2. let expectedFrameRate: ExpectedFrameRateRange = {
  3. min: 0,
  4. max: 120,
  5. expected: 30
  6. }
  7. @Entry
  8. @Component
  9. struct AnimatorTest {
  10. private backAnimator: AnimatorResult | undefined = undefined;
  11. create() {
  12. this.backAnimator = this.getUIContext().createAnimator({
  13. duration: 2000,
  14. easing: "ease",
  15. delay: 0,
  16. fill: "forwards",
  17. direction: "normal",
  18. iterations: 1,
  19. begin: 100, // 动画插值起点
  20. end: 200 // 动画插值终点
  21. })
  22. this.backAnimator.setExpectedFrameRateRange(expectedFrameRate);
  23. }
  24. build() {
  25. // ...
  26. }
  27. }

update(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

update(options: AnimatorOptions): void

更新当前animator动画参数。

说明

从API version 6开始支持,从API version 9开始废弃。建议使用reset替代。

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

参数:

展开
参数名 类型 必填 说明
options AnimatorOptions 是 动画配置选项,用于定义动画的播放时长、插值曲线、延时、填充模式、播放方向、播放次数及插值起止值等参数。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. // animator需先通过this.getUIContext().createAnimator()获取AnimatorResult对象
  2. animator.update(options);

AnimatorOptions

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

定义动画选项。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

属性

展开
名称 类型 只读 可选 说明
duration number 否 否

动画播放的时长,单位毫秒。

取值范围:[0, +∞)

默认值:0

说明: 使用interpolating-spring曲线时,duration不生效,由弹簧参数决定。

easing string 否 否

动画插值曲线,支持的曲线类型可参考表1。

非法字符串时取:"ease"。

delay number 否 否

动画延时播放时长,单位毫秒,设置为0时,表示不延时。设置为负数时动画提前播放,如果提前播放的时长大于动画总时长(由duration和iterations参数共同决定),动画直接过渡到终点。

默认值:0

fill 'none' | 'forwards' | 'backwards' | 'both' 否 否

动画填充模式,决定动画执行前(delay期间)和执行后是否将关键帧样式应用到目标上。

'none':在动画执行之前和之后都不会应用任何样式到目标上。

'forwards':在动画结束后,目标将保留动画结束时的状态(在最后一个关键帧中定义)。

'backwards':动画将在AnimatorOptions中的delay期间应用第一个关键帧中定义的值。当AnimatorOptions中的direction为'normal'或'alternate'时应用from关键帧中的值,当AnimatorOptions中的direction为'reverse'或'alternate-reverse'时应用to关键帧中的值。

'both':动画将遵循forwards和backwards的规则,从而在两个方向上扩展动画属性。

direction 'normal' | 'reverse' | 'alternate' | 'alternate-reverse' 否 否

动画播放方向。

'normal': 动画正向循环播放。

'reverse': 动画反向循环播放。

'alternate':动画交替循环播放,奇数次正向播放,偶数次反向播放。

'alternate-reverse':动画反向交替循环播放,奇数次反向播放,偶数次正向播放。

默认值:'normal'

说明: 使用interpolating-spring曲线时,direction固定设置为'normal',其他设置无效。

iterations number 否 否

动画播放次数。设置为0时不播放,设置为-1时无限次播放,设置大于0时为播放次数。

说明: 使用interpolating-spring曲线时,iterations固定设置为1,其他设置无效。

说明: 设置为除-1外其他负数视为无效取值,无效取值动画默认播放1次。

begin number 否 否

动画插值起点。

说明: 会影响onFrame回调的入参值。

默认值:0

end number 否 否

动画插值终点。

说明: 会影响onFrame回调的入参值。

默认值:1

表1 支持的曲线类型:

展开
类型 说明
"linear" 动画线性变化。
"ease" 动画开始和结束时的速度较慢,cubic-bezier(0.25, 0.1, 0.25, 1.0)。
"ease-in" 动画播放速度先慢后快,cubic-bezier(0.42, 0.0, 1.0, 1.0)。
"ease-out" 动画播放速度先快后慢,cubic-bezier(0.0, 0.0, 0.58, 1.0)。
"ease-in-out" 动画播放速度先加速后减速,cubic-bezier(0.42, 0.0, 0.58, 1.0)。
"fast-out-slow-in" 标准曲线,cubic-bezier(0.4, 0.0, 0.2, 1.0)。
"linear-out-slow-in" 减速曲线,cubic-bezier(0.0, 0.0, 0.2, 1.0)。
"fast-out-linear-in" 加速曲线,cubic-bezier(0.4, 0.0, 1.0, 1.0)。
"friction" 阻尼曲线,cubic-bezier(0.2, 0.0, 0.2, 1.0)。
"extreme-deceleration" 极缓曲线,cubic-bezier(0.0, 0.0, 0.0, 1.0)。
"rhythm" 节奏曲线,cubic-bezier(0.7, 0.0, 0.2, 1.0)。
"sharp" 锐利曲线,cubic-bezier(0.33, 0.0, 0.67, 1.0)。
"smooth" 平滑曲线,cubic-bezier(0.4, 0.0, 0.4, 1.0)。
"cubic-bezier(x1, y1, x2, y2)" 三次贝塞尔曲线,x1、x2的值必须处于0-1之间。例如"cubic-bezier(0.42, 0.0, 0.58, 1.0)"。
"steps(number, step-position)" 阶梯曲线,number必须设置,为正整数,step-position参数可选,支持设置start或end,默认值为end。例如"steps(3, start)"。
interpolating-spring(velocity, mass, stiffness, damping)

插值弹簧曲线。

velocity、mass、stiffness、damping都是数值类型,且mass、stiffness、damping参数均必须大于0,具体参数含义参考插值弹簧曲线curves.interpolatingSpring。

使用interpolating-spring时,duration不生效,由弹簧参数决定;fill、direction、iterations设置无效,fill固定设置为"forwards",direction固定设置为"normal",iterations固定设置为1,且对animator的reverse函数调用无效。即animator使用interpolating-spring时只能正向播放1次。

从API version 11开始支持且仅在ArkTS中支持使用。

SimpleAnimatorOptions18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

animator简易动画参数对象。与AnimatorOptions相比,duration、easing、delay、fill、direction、iterations等动画参数有默认值,可不设置。

constructor18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

constructor(begin: number, end: number)

创建SimpleAnimatorOptions实例,指定动画插值起点和终点。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
begin number 是

动画插值起点。

说明: 会影响onFrame回调的入参值,与end参数共同决定onFrame回调值的范围。

end number 是

动画插值终点。

说明: 会影响onFrame回调的入参值,与begin参数共同决定onFrame回调值的范围。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private animatorResult: AnimatorResult | undefined = undefined;
  6. options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200); // 动画插值过程从100到200,其余动画参数使用默认值。
  7. create() {
  8. this.animatorResult = this.getUIContext().createAnimator(this.options);
  9. }
  10. build() {
  11. // ......
  12. }
  13. }

duration18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

duration(duration: number): SimpleAnimatorOptions

设置animator动画时长。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
duration number 是

设置动画播放的时长,单位毫秒。

默认值:1000

说明: 使用interpolating-spring曲线时,duration不生效,由弹簧参数决定。

返回值:

展开
类型 说明
SimpleAnimatorOptions 返回当前简易动画参数对象,支持链式调用以继续配置动画参数。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private animatorResult: AnimatorResult | undefined = undefined;
  6. options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).duration(500);
  7. create() {
  8. this.animatorResult = this.getUIContext().createAnimator(this.options);
  9. }
  10. build() {
  11. // ......
  12. }
  13. }

easing18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

easing(curve: string): SimpleAnimatorOptions

设置animator动画插值曲线。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
curve string 是

设置animator动画插值曲线,具体说明参考AnimatorOptions。

默认值:“ease”

返回值:

展开
类型 说明
SimpleAnimatorOptions 返回当前简易动画参数对象,支持链式调用以继续配置动画参数。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private animatorResult: AnimatorResult | undefined = undefined;
  6. options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).easing("ease-in");
  7. create() {
  8. this.animatorResult = this.getUIContext().createAnimator(this.options);
  9. }
  10. build() {
  11. // ......
  12. }
  13. }

delay18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

delay(delay: number): SimpleAnimatorOptions

设置animator动画延时播放时长。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
delay number 是

设置animator动画播放时延,单位毫秒,设置为0时,表示不延时。设置为负数时动画提前播放,如果提前播放的时长大于动画总时长,动画直接过渡到终点。

默认值:0

返回值:

展开
类型 说明
SimpleAnimatorOptions 返回当前简易动画参数对象,支持链式调用以继续配置动画参数。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private animatorResult: AnimatorResult | undefined = undefined;
  6. options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).delay(500);
  7. create() {
  8. this.animatorResult = this.getUIContext().createAnimator(this.options);
  9. }
  10. build() {
  11. // ......
  12. }
  13. }

fill18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

fill(fillMode: FillMode): SimpleAnimatorOptions

设置animator动画填充方式。使用interpolating-spring曲线时,此设置无效,fill固定设置为FillMode.Forwards。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
fillMode FillMode 是

设置animator动画填充方式,影响动画delay期间和结束时的表现。使用interpolating-spring曲线时,fill设置无效,固定设置为FillMode.Forwards。

默认值:FillMode.Forwards

返回值:

展开
类型 说明
SimpleAnimatorOptions 返回当前简易动画参数对象,支持链式调用以继续配置动画参数。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private animatorResult: AnimatorResult | undefined = undefined;
  6. options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).fill(FillMode.Forwards);
  7. create() {
  8. this.animatorResult = this.getUIContext().createAnimator(this.options);
  9. }
  10. build() {
  11. // ......
  12. }
  13. }

direction18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

direction(direction: PlayMode): SimpleAnimatorOptions

设置animator动画播放模式。使用interpolating-spring曲线时,此设置无效,direction固定设置为PlayMode.Normal。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
direction PlayMode 是

设置animator动画播放方向。

PlayMode.Normal:动画正向循环播放。

PlayMode.Reverse:动画反向循环播放。

PlayMode.Alternate:动画交替循环播放,奇数次正向播放,偶数次反向播放。

PlayMode.AlternateReverse:动画反向交替循环播放,奇数次反向播放,偶数次正向播放。

默认值:PlayMode.Normal

返回值:

展开
类型 说明
SimpleAnimatorOptions 返回当前简易动画参数对象,支持链式调用以继续配置动画参数。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private animatorResult: AnimatorResult | undefined = undefined;
  6. options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).direction(PlayMode.Alternate);
  7. create() {
  8. this.animatorResult = this.getUIContext().createAnimator(this.options);
  9. }
  10. build() {
  11. // ......
  12. }
  13. }

iterations18+

Phone18+PC/2in118+Tablet18+TV19+Wearable18+

iterations(iterations: number): SimpleAnimatorOptions

设置animator动画播放次数。

元服务API: 从API version 18开始,该接口支持在元服务中使用。

模型约束: 此接口仅可在Stage模型下使用。

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

参数:

展开
参数名 类型 必填 说明
iterations number 是

设置animator动画播放次数,设置为0时不播放,设置为-1时无限次播放,设置大于0时为播放次数。

说明: 设置为除-1外其他负数视为无效取值,无效取值动画默认播放1次。

默认值:1

使用interpolating-spring曲线时,iterations设置无效,固定设置为1。

返回值:

展开
类型 说明
SimpleAnimatorOptions 返回当前简易动画参数对象,支持链式调用以继续配置动画参数。

示例:

完整示例请参考基于ArkTS扩展的声明式开发范式。

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private animatorResult: AnimatorResult | undefined = undefined;
  6. options: SimpleAnimatorOptions = new SimpleAnimatorOptions(100, 200).iterations(3);
  7. create() {
  8. this.animatorResult = this.getUIContext().createAnimator(this.options);
  9. }
  10. build() {
  11. // ......
  12. }
  13. }

完整示例

基于JS扩展的类Web开发范式

收起
自动换行
深色代码主题
复制
  1. <!-- hml -->
  2. <div class="container">
  3. <div class="Animation" style="height: {{divHeight}}px; width: {{divWidth}}px; background-color: red;" onclick="Show">
  4. </div>
  5. </div>
收起
自动换行
深色代码主题
复制
  1. import { Animator as animator, AnimatorResult, AnimatorOptions } from '@kit.ArkUI';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let DataTmp: Record<string, Animator> = {
  4. 'divWidth': 200,
  5. 'divHeight': 200,
  6. 'animator': animator
  7. }
  8. class Tmp {
  9. data: animator = DataTmp
  10. onInit: Function = () => {
  11. }
  12. show: Function = () => {
  13. }
  14. }
  15. class AnimatorState {
  16. divWidth: number = 0
  17. divHeight: number = 0
  18. animator: AnimatorResult | null = null
  19. }
  20. (Fn: (v: Tmp) => void) => {
  21. Fn({
  22. data: DataTmp,
  23. onInit() {
  24. let options: AnimatorOptions = {
  25. duration: 1500,
  26. easing: "friction",
  27. delay: 0,
  28. fill: "forwards",
  29. direction: "normal",
  30. iterations: 2,
  31. begin: 200.0,
  32. end: 400.0
  33. };
  34. let animatorState: AnimatorState = {
  35. divWidth: 200,
  36. divHeight: 200,
  37. animator: null
  38. }
  39. animatorState.animator = animator.create(options);
  40. },
  41. show() {
  42. let resetOptions: AnimatorOptions = {
  43. duration: 1500,
  44. easing: "friction",
  45. delay: 0,
  46. fill: "forwards",
  47. direction: "normal",
  48. iterations: 2,
  49. begin: 0,
  50. end: 400.0,
  51. };
  52. let animatorState: AnimatorState = {
  53. divWidth: 200,
  54. divHeight: 200,
  55. animator: null
  56. }
  57. try {
  58. animatorState.animator = animator.create(resetOptions);
  59. animatorState.animator.reset(resetOptions);
  60. } catch (error) {
  61. let message = (error as BusinessError).message
  62. let code = (error as BusinessError).code
  63. console.error(`Animator reset failed. Code: ${code}, message: ${message}`);
  64. }
  65. let _this = animatorState;
  66. if (animatorState.animator) {
  67. animatorState.animator.onFrame = (value: number) => {
  68. _this.divWidth = value;
  69. _this.divHeight = value;
  70. };
  71. animatorState.animator.play();
  72. }
  73. }
  74. })
  75. }

基于ArkTS扩展的声明式开发范式

说明

推荐通过使用UIContext中的createAnimator接口明确UI上下文。

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private TAG: string = '[AnimatorTest]'
  6. private backAnimator: AnimatorResult | undefined = undefined
  7. private flag: boolean = false
  8. @State columnWidth: number = 100
  9. @State columnHeight: number = 100
  10. create() {
  11. this.backAnimator = this.getUIContext().createAnimator({
  12. // 建议使用 this.getUIContext().createAnimator()接口
  13. duration: 2000,
  14. easing: "ease",
  15. delay: 0,
  16. fill: "forwards",
  17. direction: "normal",
  18. iterations: 1,
  19. begin: 100, // 动画插值起点
  20. end: 200 // 动画插值终点
  21. })
  22. this.backAnimator.onFinish = () => {
  23. this.flag = true;
  24. console.info(this.TAG, 'backAnimator onFinish');
  25. }
  26. this.backAnimator.onRepeat = () => {
  27. console.info(this.TAG, 'backAnimator repeat');
  28. }
  29. this.backAnimator.onCancel = () => {
  30. console.info(this.TAG, 'backAnimator cancel');
  31. }
  32. this.backAnimator.onFrame = (value: number) => {
  33. this.columnWidth = value;
  34. this.columnHeight = value;
  35. }
  36. }
  37. aboutToDisappear() {
  38. // 自定义组件消失时调用finish使未完成的动画结束,避免动画继续运行。
  39. // 由于backAnimator在onFrame中引用了this, this中保存了backAnimator,
  40. // 在自定义组件消失时应该将保存在组件中的backAnimator置空,避免内存泄漏
  41. this.backAnimator?.finish();
  42. this.backAnimator = undefined;
  43. }
  44. build() {
  45. Column() {
  46. Column() {
  47. Column()
  48. .width(this.columnWidth)
  49. .height(this.columnHeight)
  50. .backgroundColor(Color.Blue)
  51. }
  52. .width('100%')
  53. .height(300)
  54. Column() {
  55. Row() {
  56. Button('create')
  57. .fontSize(30)
  58. .fontColor(Color.Black)
  59. .onClick(() => {
  60. this.create()
  61. })
  62. }
  63. .padding(10)
  64. Row() {
  65. Button('play')
  66. .fontSize(30)
  67. .fontColor(Color.Black)
  68. .onClick(() => {
  69. this.flag = false
  70. if (this.backAnimator) {
  71. this.backAnimator.play()
  72. }
  73. })
  74. }
  75. .padding(10)
  76. Row() {
  77. Button('pause')
  78. .fontSize(30)
  79. .fontColor(Color.Black)
  80. .onClick(() => {
  81. if (this.backAnimator) {
  82. this.backAnimator.pause()
  83. }
  84. })
  85. }
  86. .padding(10)
  87. Row() {
  88. Button('finish')
  89. .fontSize(30)
  90. .fontColor(Color.Black)
  91. .onClick(() => {
  92. this.flag = true
  93. if (this.backAnimator) {
  94. this.backAnimator.finish()
  95. }
  96. })
  97. }
  98. .padding(10)
  99. Row() {
  100. Button('reverse')
  101. .fontSize(30)
  102. .fontColor(Color.Black)
  103. .onClick(() => {
  104. this.flag = false
  105. if (this.backAnimator) {
  106. this.backAnimator.reverse()
  107. }
  108. })
  109. }
  110. .padding(10)
  111. Row() {
  112. Button('cancel')
  113. .fontSize(30)
  114. .fontColor(Color.Black)
  115. .onClick(() => {
  116. if (this.backAnimator) {
  117. this.backAnimator.cancel()
  118. }
  119. })
  120. }
  121. .padding(10)
  122. Row() {
  123. Button('reset')
  124. .fontSize(30)
  125. .fontColor(Color.Black)
  126. .onClick(() => {
  127. if (this.flag) {
  128. this.flag = false
  129. if (this.backAnimator) {
  130. this.backAnimator.reset({
  131. duration: 3000,
  132. easing: "ease-in",
  133. delay: 0,
  134. fill: "forwards",
  135. direction: "alternate",
  136. iterations: 3,
  137. begin: 100,
  138. end: 300
  139. })
  140. }
  141. } else {
  142. console.info(this.TAG, 'Animation not ended')
  143. }
  144. })
  145. }
  146. .padding(10)
  147. }
  148. }
  149. }
  150. }

位移动画示例(简易入参)

收起
自动换行
深色代码主题
复制
  1. import { AnimatorResult, SimpleAnimatorOptions } from '@kit.ArkUI';
  2. @Entry
  3. @Component
  4. struct AnimatorTest {
  5. private TAG: string = '[AnimatorTest]'
  6. private backAnimator: AnimatorResult | undefined = undefined
  7. private flag: boolean = false
  8. @State translateX: number = 0
  9. create() {
  10. this.backAnimator = this.getUIContext()?.createAnimator(
  11. new SimpleAnimatorOptions(0, 100)
  12. )
  13. this.backAnimator.onFinish = () => {
  14. this.flag = true
  15. console.info(this.TAG, 'backAnimator onFinish')
  16. }
  17. this.backAnimator.onFrame = (value: number) => {
  18. this.translateX = value
  19. }
  20. }
  21. aboutToDisappear() {
  22. // 自定义组件消失时调用finish使未完成的动画结束,避免动画继续运行。
  23. // 由于backAnimator在onFrame中引用了this, this中保存了backAnimator,
  24. // 在自定义组件消失时应该将保存在组件中的backAnimator置空,避免内存泄漏
  25. this.backAnimator?.finish();
  26. this.backAnimator = undefined;
  27. }
  28. build() {
  29. Column() {
  30. Column() {
  31. Column()
  32. .width(100)
  33. .height(100)
  34. .translate({x: this.translateX})
  35. .backgroundColor(Color.Green)
  36. }
  37. .width('100%')
  38. .height(300)
  39. Column() {
  40. Column() {
  41. Button('create')
  42. .fontSize(30)
  43. .fontColor(Color.White)
  44. .onClick(() => {
  45. this.create()
  46. })
  47. }
  48. .padding(10)
  49. Column() {
  50. Button('play')
  51. .fontSize(30)
  52. .fontColor(Color.White)
  53. .onClick(() => {
  54. this.flag = false
  55. if(this.backAnimator){
  56. this.backAnimator.play()
  57. }
  58. })
  59. }
  60. .padding(10)
  61. Column() {
  62. Button('reset')
  63. .fontSize(30)
  64. .fontColor(Color.White)
  65. .onClick(() => {
  66. if (this.flag) {
  67. this.flag = false
  68. if(this.backAnimator){
  69. this.backAnimator.reset(
  70. new SimpleAnimatorOptions(0, -100)
  71. .duration(2000)
  72. .easing("ease-in")
  73. .fill(FillMode.Forwards)
  74. .direction(PlayMode.Alternate)
  75. .iterations(2)
  76. )
  77. }
  78. } else {
  79. console.info(this.TAG, 'Animation not ended')
  80. }
  81. })
  82. }
  83. .padding(10)
  84. }
  85. }
  86. }
  87. }

在 API参考 中进行搜索
请输入您想要搜索的关键词