??官网对于属性动画的定义如下:
组件的某些通用属性变化时,可以通过属性动画实现渐变过渡效果,提升用户体验。支持的属性包括width、height、backgroundColor、opacity、scale、rotate、translate等
??个人理解:属性动画针对的是同一个组件的属性变化情况,如在同一个页面中触发了组件的属性变化,这个变化需要以过渡的方式体现,这就需要属性动画来承接这个过渡过程,不然变化就会显得十分突兀与生硬。因此,只要组件的指定属性有变化,那么这个变化就会通过属性动画来体现。
??使用属性动画,通过animation
属性来为每个组件定义该组件对应的属性动画,如下所示:
animation(
value: {
duration?: number,
tempo?: number,
curve?: string | Curve | ICurve,
delay?:number,
iterations: number,
playMode?: PlayMode,
onFinish?: () => void
}
)
??其中的每个参数都会改变属性动画的展现情况,
??如果没有加属性动画,那么在点击按钮时就会很生硬地变动,如下所示:
Button('change size')
.onClick(() => {
if (this.flag) {
this.widthSize = 150
this.heightSize = 60
} else {
this.widthSize = 250
this.heightSize = 100
}
this.flag = !this.flag
})
.margin(30)
.width(this.widthSize)
.height(this.heightSize)
??使用属性动画后,点击按钮后按钮变大产生了动画效果,更加顺滑:
Button('change size')
.onClick(() => {
if (this.flag) {
this.widthSize = 150
this.heightSize = 60
} else {
this.widthSize = 250
this.heightSize = 100
}
this.flag = !this.flag
})
.margin(30)
.width(this.widthSize)
.height(this.heightSize)
??我们可以通过调整animation的属性来调整动画效果。
??backgroundColor
属性也可以通过属性动画进行调节,如下所示:
Button('change size')
.onClick(() => {
if (this.flag) {
this.widthSize = 150
this.heightSize = 60
this.bgColor = Color.Blue
} else {
this.widthSize = 250
this.heightSize = 100
this.bgColor = Color.Black
}
this.flag = !this.flag
})
.margin(30)
.backgroundColor(this.bgColor)
.width(this.widthSize)
.height(this.heightSize)
.animation({
duration: 500,
curve: Curve.EaseOut,
iterations: 1,
playMode: PlayMode.Normal
})
??rotate
属性也可以根据属性动画进行变动,如下所示:
Button('change rotate angle')
.onClick(() => {
if(this.flag){
this.rotateAngle = 90
}
else{
this.rotateAngle = 180
}
this.flag = !this.flag
})
.margin(50)
.rotate({ angle: this.rotateAngle })
.animation({
duration: 1200,
curve: Curve.Friction,
delay: 500,
iterations: 1, // 设置-1表示动画无限循环
playMode: PlayMode.Alternate
})
??通过onFinish
属性,在动画播放完成后替换文字:
Button(this.btnText)
.onClick(() => {
if (this.flag) {
this.widthSize = 150
this.heightSize = 60
this.bgColor = Color.Blue
this.opa = 0.8
} else {
this.widthSize = 250
this.heightSize = 100
this.bgColor = Color.Black
this.opa = 0.5
}
this.flag = !this.flag
})
.opacity(this.opa)
.margin(30)
.backgroundColor(this.bgColor)
.width(this.widthSize)
.height(this.heightSize)
.animation({
duration: 500,
curve: Curve.EaseOut,
iterations: 1,
playMode: PlayMode.Normal,
onFinish: () => {
if(this.flag){
this.btnText = "button clicked"
}else{
this.btnText = "button"
}
}
})
??我们可以通过函数来替换onFinish
属性对应的函数,如下所示:
private onBtnAnimationFinish(){
if(this.flag){
this.btnText = "button clicked"
}else{
this.btnText = "button"
}
}
build() {
Column() {
Button(this.btnText)
.onClick(() => {
if (this.flag) {
this.widthSize = 150
this.heightSize = 60
this.bgColor = Color.Blue
this.opa = 0.8
} else {
this.widthSize = 250
this.heightSize = 100
this.bgColor = Color.Black
this.opa = 0.5
}
this.flag = !this.flag
})
.opacity(this.opa)
.margin(30)
.backgroundColor(this.bgColor)
.width(this.widthSize)
.height(this.heightSize)
.animation({
duration: 500,
curve: Curve.EaseOut,
iterations: 1,
playMode: PlayMode.Normal,
onFinish: this.onBtnAnimationFinish
})
}.width('100%').margin({ top: 20 })
}