前言
在纯血鸿蒙中最具有用户特色的效果就是自定义的动画效果。在纯血鸿蒙中有多种定义方式,但是今天介绍的是ApI中的自定义动画。
注意:
动画本身具有生命周期,但是不支持在UIAbility的文件使用,简单而言就是不允许在UIAbility生命周期中调用
涉及到的API属性
Animator
属性 参数 功能 参数类型 返回类型 create create(options: AnimatorOptions): AnimatorResult 创建动画 AnimatorOptions (必填) AnimatorResult
AnimatorResult
属性 参数 功能 参数类型 返回类型 reset reset(options: AnimatorOptions): void 更新当前动画器。 AnimatorOptions(必填) 无 play play(): void 启动动画。动画会保留上一次的播放状态,比如播放状态设置reverse后,再次播放会保留reverse的播放状态。 无 无 finish finish(): void 结束动画 无 无 pause pause(): void 暂停动画。 无 无 cancel cancel(): void 取消动画。 无 无 reverse reverse(): void 以相反的顺序播放动画。使用interpolating-spring曲线时此接口无效。 无 无
回调函数(理解为生命周期)
属性 参数 功能 参数类型 返回类型 onFrame onFrame: (progress: number) => void 接收到帧时回调。 progress:number(必填) 无 onFinish onFinish: () => void 动画完成时回调。 无 无 onCancel onCancel: () => void 动画被取消时回调。 无 无 onRepeat onRepeat: () => void 动画重复时回调。 无 无
AnimatorOptions(定义动画选项。)
名称 类型 必填 功能 duration number 是 动画持续时间 easing string 是 动画插值曲线 delay number 是 动画延时播放时长,单位毫秒,设置为0时,表示不延时。 fill ‘none’ ‘forwards’ ‘backwards’ ‘both’ 是 动画执行后是否恢复到初始状态,动画执行后,动画结束时的状态(在最后一个关键帧中定义)将保留。 direction ‘normal’ ‘reverse’ ‘alternate’ ‘alternate-reverse’ 是 动画播放模式正序,逆序,交替正序,交替逆序 iterations number 是 动画播放次数 begin number 是 动画插值起点。 end number 是 动画插值终点。
注意:以上为涉及属性和参数一下为示例代码和详细注释
import { Animator as animator, AnimatorResult } from '@kit.ArkUI' ;
@ Entry
@ Component
struct AnimatorTest { private TAG : string = '[AnimatorTest]' private backAnimator: AnimatorResult | undefined = undefined private flag: boolean = false @ State wid: number = 100 @ State hei: number = 100 create ( ) { this . backAnimator = animator. create ( { duration: 2000 , easing: "ease" , delay: 0 , fill: "forwards" , direction: "normal" , iterations: 1 , begin: 100 , end: 200 } ) this . backAnimator. onFinish = ( ) => { this . flag = true console . info ( this . TAG , 'backAnimator onfinish' ) } this . backAnimator. onRepeat = ( ) => { console . info ( this . TAG , 'backAnimator repeat' ) } this . backAnimator. onCancel = ( ) => { console . info ( this . TAG , 'backAnimator cancel' ) } this . backAnimator. onFrame = ( value: number ) => { this . wid = value this . hei = value } } aboutToDisappear ( ) { this . backAnimator = undefined ; } 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 if ( this . backAnimator) { this . backAnimator. play ( ) } } ) } . padding ( 10 ) Row ( ) { Button ( 'pause' ) . fontSize ( 30 ) . fontColor ( Color. Black) . onClick ( ( ) => { if ( this . backAnimator) { this . backAnimator. pause ( ) } } ) } . padding ( 10 ) Row ( ) { Button ( 'finish' ) . fontSize ( 30 ) . fontColor ( Color. Black) . onClick ( ( ) => { this . flag = true if ( this . backAnimator) { this . backAnimator. finish ( ) } } ) } . padding ( 10 ) Row ( ) { Button ( 'reverse' ) . fontSize ( 30 ) . fontColor ( Color. Black) . onClick ( ( ) => { this . flag = false if ( this . backAnimator) { this . backAnimator. reverse ( ) } } ) } . padding ( 10 ) Row ( ) { Button ( 'cancel' ) . fontSize ( 30 ) . fontColor ( Color. Black) . onClick ( ( ) => { if ( this . backAnimator) { this . backAnimator. cancel ( ) } } ) } . padding ( 10 ) Row ( ) { Button ( 'reset' ) . fontSize ( 30 ) . fontColor ( Color. Black) . onClick ( ( ) => { if ( this . flag) { this . flag = false if ( this . backAnimator) { this . backAnimator. reset ( { duration: 3000 , easing: "ease-in" , delay: 0 , fill: "forwards" , direction: "alternate" , iterations: 3 , begin: 100 , end: 300 } ) } } else { console . info ( this . TAG , 'Animation not ended' ) } } ) } . padding ( 10 ) } } }
}