开发者在使用放大和缩小动画效果时,可能会遇到以下几种典型场景:
import { UIContext } from '@kit.ArkUI';
@Entry
@Component
struct KeyframeDemo {
@State myScale: number = 1.0;
@State brightNess: number = 1;
uiContext: UIContext | undefined = undefined;
aboutToAppear() {
this.uiContext = this.getUIContext?.();
}
build() {
Column() {
Circle()
.width(100)
.height(100)
.fill('#46B1E3')
.margin(100)
.brightness(this.brightNess)
.scale({ x: this.myScale, y: this.myScale })
.onClick(() => {
if (!this.uiContext) {
return;
}
this.myScale = 1;
// 设置关键帧动画整体播放3次
this.uiContext.keyframeAnimateTo({ iterations: 3 }, [
{
// 第一段关键帧动画时长为800ms,scale属性做从1到1.5的动画
duration: 800,
event: () => {
this.myScale = 1.5;
this.brightNess = 1.3;
}
},
{
// 第二段关键帧动画时长为500ms,scale属性做从1.5到1的动画
duration: 500,
event: () => {
this.myScale = 1;
this.brightNess = 1;
}
}
]);
});
}.width('100%').margin({ top: 5 });
}
} @Entry
@Component
struct RingAnimationExample {
@State scaleValue: number = 1;
startAnimation() {
this.getUIContext().animateTo({
duration: 1000,
iterations: -1,
playMode: PlayMode.Alternate,
curve: Curve.EaseInOut
}, () => {
this.scaleValue = 1.2; // 仅作用于圆环
});
}
build() {
Column() {
Stack() {
// 此处仅为示例,请开发者自行替换为可用图片
Image($r('app.media.startIcon'))
.width(100)
.height(100)
.border({
width: 4,
color: Color.Red,
radius: '100%'
});
// 独立圆环组件
Stack()
.width(150)
.height(150)
.scale({ x: this.scaleValue, y: this.scaleValue }) // 仅缩放圆环
.border({
width: 2,
color: Color.Pink,
radius: '100%'
});
};
Text('直播中')
.width(150)
.height(50)
.fontColor(Color.White)
.fontWeight(600)
.fontSize(26)
.textAlign(TextAlign.Center)
.backgroundColor(Color.Red)
.border({
width: 1,
color: Color.Red,
radius: '100%'
})
.margin({ top: -20 });
Button('点击播放').onClick(() => {
this.startAnimation();
})
.margin({ top: 30 });
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center);
}
} @Entry
@Component
struct AnimationComponentPage0 {
@State widthSize: number | string = '100%';
build() {
Column() {
Column()
.width(this.widthSize)
.height(300)
.backgroundColor(Color.Orange)
.animation({
duration: 2000,
curve: Curve.EaseOut,
iterations: 1,
playMode: PlayMode.Normal
})
.onClick(() => {
this.widthSize = this.widthSize === 44 ? '100%' : 44;
});
}
.width('100%')
.margin({ top: 20 })
.alignItems(HorizontalAlign.Start);
}
} @Entry
@Component
struct AnimationComponentPage1 {
@State widthSize: number | string = '100%';
build() {
Column() {
Column()
.width(this.widthSize)
.height(300)
.backgroundColor(Color.Orange)
.onClick(() => {
this.getUIContext()?.animateTo({
duration: 2000,
curve: Curve.EaseOut,
iterations: 1,
playMode: PlayMode.Normal
}, () => {
this.widthSize = this.widthSize === 44 ? '100%' : 44;
});
});
}
.width('100%')
.margin({ top: 20 })
.alignItems(HorizontalAlign.Start);
}
} 两种方案对比:
方案 | 优缺点 | 适用场景 |
|---|---|---|
方案一:属性动画 (animation) | 简洁高效,直接绑定目标属性(如按钮点击后的颜色/大小变化)。仅修饰单一组件的特定属性,无法直接串联多个动画。 | 单组件属性渐变。 |
方案二:显式动画 (animateTo) | 闭包内状态变化可同时驱动多个组件的属性动画。通过闭包函数批量触发多属性动画,可嵌套调用实现复杂动画序列。 | 多个组件联动动画,复杂动画序列。 |