OpenHarmony添加动画效果的实用指南
一、动画类型与适用场景
二、快速上手三步法
三、代码示例
import { Curves } from '@ohos.curves';
import { animateTo, Curve, PlayMode } from '@ohos.animation';
@Entry
@Component
struct AnimateToExample {
@State widthSize: number = 250;
@State heightSize: number = 100;
@State rotateAngle: number = 0;
private flag: boolean = true;
build() {
Column({ space: 30 }) {
Button('change width and height')
.width(this.widthSize)
.height(this.heightSize)
.onClick(() => {
if (this.flag) {
animateTo({
duration: 2000,
curve: Curve.EaseOut,
iterations: 3,
playMode: PlayMode.Normal,
onFinish: () => console.info('play end')
}, () => {
this.widthSize = 100;
this.heightSize = 50;
});
} else {
animateTo({}, () => {
this.widthSize = 250;
this.heightSize = 100;
});
}
this.flag = !this.flag;
});
Button('change rotate angle')
.rotate({ angle: this.rotateAngle })
.onClick(() => {
animateTo({
duration: 1200,
curve: Curve.Friction,
delay: 500,
iterations: -1, // 无限循环
playMode: PlayMode.AlternateReverse,
onFinish: () => console.info('play end')
}, () => {
this.rotateAngle = 90;
});
});
}
.width('100%')
.margin({ top: 20 });
}
}
import { TransitionEffect } from '@ohos.animation.transition';
@Entry
@Component
struct TransitionExample {
@State show: boolean = true;
build() {
Column({ space: 20 }) {
if (this.show) {
Text('Hello, OpenHarmony')
.id('myText') // 建议设置 id,便于打断/管理
.fontSize(20)
.backgroundColor('#007DFF')
.fontColor(Color.White)
.padding(10)
.borderRadius(8)
.transition(TransitionEffect.OPACITY.animation({ duration: 1000 }))
}
Button(this.show ? 'Hide' : 'Show')
.onClick(() => {
this.show = !this.show;
})
}
.width('100%')
.padding(20);
}
}
import { pageTransition, SharedTransitionEffect, SharedTransitionDirection } from '@ohos.animation.pageTransition';
@Entry
@Component
struct PageA {
build() {
Column() {
Image($r('app.media.icon'))
.width(80).height(80)
.sharedTransition('icon', SharedTransitionEffect.SCALE, SharedTransitionDirection.BOTH)
.onClick(() => {
// 跳转到 PageB
})
}
.width('100%').height('100%').justifyContent(FlexAlign.Center)
}
}
@Entry
@Component
struct PageB {
build() {
Column() {
Image($r('app.media.icon'))
.width(160).height(160)
.sharedTransition('icon', SharedTransitionEffect.SCALE, SharedTransitionDirection.BOTH)
}
.width('100%').height('100%').justifyContent(FlexAlign.Center)
}
}
import router from '@ohos.router';
import { animateTo, Curve } from '@ohos.animation';
import { Curves } from '@ohos.curves';
@Entry
@Component
struct Logo {
@State opacityValue: number = 0;
@State scaleValue: number = 0;
private curve1 = Curves.cubicBezier(0.4, 0, 1, 1);
build() {
// 假设 Logo 使用 Image 或 Shape
Image($r('app.media.logo'))
.width(120).height(120)
.opacity(this.opacityValue)
.scale({ x: this.scaleValue, y: this.scaleValue })
.onAppear(() => {
animateTo({
duration: 1000,
curve: this.curve1,
delay: 100,
onFinish: () => {
setTimeout(() => {
router.replaceUrl({ url: 'pages/Main' }); // 跳转到主页
}, 1000); // 动画定格 1s 后跳转
}
}, () => {
this.opacityValue = 1;
this.scaleValue = 1;
});
})
}
}
以上示例覆盖了属性动画、显式动画、转场与页面间共享元素转场的常见用法,参数如 duration、curve、delay、iterations、playMode 可按需调整。
四、性能与最佳实践
五、常见问题与排查
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。