组件的某些通用属性变化时,可以通过属性动画实现渐变过渡效果,提升用户体验。支持的属性包括width、height、backgroundcolor、opacity、scale、rotate、translate等。
说明 : 从api version 7开始支持。开发语言ets.
参数:
名称 | 参数类型 | 必填 | 描述 |
---|---|---|---|
duration | number | 否 | 设置动画时长。单位为毫秒,默认动画时长为1000毫秒。默认值:1000 |
tempo | number | 否 | 动画播放速度。数值越大,动画播放速度越快,数值越小,播放速度越慢值为0时,表示不存在动画。默认值:1 |
curve | string | curve | icurve9+ |
delay | number | 否 | 设置动画延迟执行的时长。单位为毫秒,默认不延时播放。默认值:0 |
iterations | number | 否 | 设置播放次数。默认播放一次,设置为-1时表示无限次播放。默认值:1 |
playmode | playmode | 否 | 设置动画播放模式,默认播放完成后重头开始播放。默认值:playmode.normal |
onfinish | () => void | 否 | 状态回调,动画播放完成时触发。 |
示例代码:
@entry
@component
struct attranimation {
@state widthsize: number = 200;
@state heightsize: number = 100;
@state fontsize: number = 20;
@state attrflag: boolean = true;
build() {
column() {
row(){
text(this.attrflag ? '点击缩小':'点击变大').fontsize(this.fontsize)
}.onclick(() => {
if (this.attrflag) {
this.widthsize = 100
this.heightsize = 50
} else {
this.widthsize = 200
this.heightsize = 100
}
this.attrflag = !this.attrflag
}).width(this.widthsize)
.height(this.heightsize)
.backgroundcolor(0x317aff)
.justifycontent(flexalign.center)
.animation({
duration: 2000, // 动画时长
tempo:2,//动画播放速度
curve: curve.easeout, // 动画曲线
delay: 500, // 动画延迟
iterations: 1, // 播放次数
playmode: playmode.normal // 动画模式
})
}
.width('100%')
.height('100%')
.justifycontent(flexalign.center)
}
}
示例效果:
发表评论