基础动画案例
@Entry
@Component
struct Index { @StatebtnWidth: number = 200 @StatebtnHeight: number = 100 build ( ) { Row ( ) { Column ( ) { Button ( "测试" ) . width ( this . btnWidth) . height ( this . btnHeight) Button ( "动画开始" ) . onClick ( ( ) => { animateTo ( { duration: 1000 } , ( ) => { this . btnWidth= 100 this . btnHeight= 50 } ) } ) } . width ( "100%" ) } . height ( "100%" ) }
}
动画的播放次数
@Entry
@Component
struct Index { @StatebtnWidth: number = 200 @StatebtnHeight: number = 100 build ( ) { Row ( ) { Column ( ) { Button ( "测试" ) . width ( this . btnWidth) . height ( this . btnHeight) Button ( "动画开始" ) . onClick ( ( ) => { animateTo ( { duration: 1000 , iterations: 3 } , ( ) => { this . btnWidth= 100 this . btnHeight= 50 } ) } ) } . width ( "100%" ) } . height ( "100%" ) }
}
动画的播放模式
@Entry
@Component
struct Index { @StatebtnWidth: number = 200 @StatebtnHeight: number = 100 build ( ) { Row ( ) { Column ( ) { Button ( "测试" ) . width ( this . btnWidth) . height ( this . btnHeight) Button ( "动画开始" ) . onClick ( ( ) => { animateTo ( { duration: 1000 , iterations: - 1 , playMode: PlayMode. Alternate} , ( ) => { this . btnWidth= 100 this . btnHeight= 50 } ) } ) } . width ( "100%" ) } . height ( "100%" ) }
}
animation动画
@Entry
@Component
struct Index { @StatebtnWidth: number = 200 @StatebtnHeight: number = 100 build ( ) { Row ( ) { Column ( ) { Button ( "测试" ) . width ( this . btnWidth) . height ( this . btnHeight) . animation ( { duration: 1000 , iterations: - 1 , playMode: PlayMode. Alternate} ) Button ( "动画开始" ) . onClick ( ( ) => { this . btnWidth= 100 this . btnHeight= 50 } ) } . width ( "100%" ) } . height ( "100%" ) }
}
scale缩放动画
@Entry
@Component
struct Index { @StatebtnWidth: number = 200 @StatebtnHeight: number = 100 @StatebtnScale: number = 1 ; build ( ) { Row ( ) { Column ( ) { Button ( "测试" ) . width ( this . btnWidth) . height ( this . btnHeight) . scale ( { x: this . btnScale, y: this . btnScale, } ) . animation ( { duration: 1000 , iterations: - 1 , playMode: PlayMode. Alternate} ) Button ( "动画开始" ) . onClick ( ( ) => { this . btnScale *= 2 } ) } . width ( "100%" ) } . height ( "100%" ) }
}
显示隐藏动画
@Entry
@Component
struct Index { @Statemessage: string = "你好, 张大鹏!" @StateisShowMessage: boolean = true build ( ) { Row ( ) { Column ( ) { Column ( ) { if ( this . isShowMessage) { Text ( this . message) . fontSize ( 50 ) . fontWeight ( FontWeight. Bold) } } . height ( 50 ) Button ( "显示/隐藏" ) . onClick ( ( ) => { animateTo ( { duration: 1000 } , ( ) => { this . isShowMessage= ! this . isShowMessage} ) } ) } . width ( "100%" ) } . height ( "100%" ) }
}
弹出模态框
@Entry
@Component
struct Index { @StateisShowDialog: boolean = false @BuildergetContent ( ) { Column ( ) { Text ( "测试" ) . fontColor ( Color. White) } . justifyContent ( FlexAlign. Center) . backgroundColor ( Color. Blue) . width ( "100%" ) . height ( "100%" ) } build ( ) { Row ( ) { Button ( "显示/隐藏" ) . onClick ( ( ) => { this . isShowDialog= ! this . isShowDialog} ) } . height ( "100%" ) . bindContentCover ( $$this . isShowDialog, this . getContent, ) }
}
弹出倒计时广告
@Entry
@Component
struct Index { @StateisShowDialog: boolean = false @StatetimerCount: number = 5 timer: number = - 1 beginTimerCount ( ) { this . timer = setInterval ( ( ) => { if ( this . timerCount === 0 ) { clearInterval ( this . timer) this . timerCount = 5 this . isShowDialog = false } this . timerCount-- } , 1000 ) } aboutToDisappear ( ) : void { clearInterval ( this . timer) } @BuildergetContent ( ) { Column ( ) { Row ( ) { Text ( ` 还剩 ${ this . timerCount} 秒 ` ) . fontColor ( Color. White) } . width ( "100%" ) . justifyContent ( FlexAlign. End) . padding ( 10 ) } . backgroundColor ( Color. Blue) . width ( "100%" ) . height ( "100%" ) } build ( ) { Row ( ) { Button ( "显示" ) . onClick ( ( ) => { this . isShowDialog = true this . beginTimerCount ( ) } ) } . height ( "100%" ) . bindContentCover ( $$this . isShowDialog, this . getContent, { modalTransition: ModalTransition. NONE , } ) }
}