OpenHarmony 创建动画的实用路径
一 选择技术路径
二 快速上手 ArkUI 显式动画 animateTo
import router from '@ohos.router'
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() {
Column() {
Image($r('app.media.logo'))
.width(120).height(120)
.scale({ x: this.scaleValue, y: this.scaleValue })
.opacity(this.opacityValue)
}
.width('100%').height('100%').justifyContent(FlexAlign.Center)
.onAppear(() => {
animateTo({
duration: 1000, // 动画时长
delay: 100, // 延迟
curve: this.curve1, // 先慢后快
onFinish: () => {
setTimeout(() => router.replaceUrl({ url: 'pages/Main' }), 1000) // 定格1s后跳转
}
}, () => {
this.opacityValue = 1
this.scaleValue = 1
})
})
}
}
三 其他常见做法
.bear {
position: absolute;
width: 200px; height: 100px;
background-image: url('./imgs/xiong.png');
animation: bearRun 1s steps(8) infinite;
}
@keyframes bearRun {
0% { background-position: 0 0; }
100% { background-position: -1600px 0; }
}
import lottie, { AnimationItem } from '@ohos/lottie'
@Entry
@Component
struct LottieDemo {
private ctx: CanvasRenderingContext2D
private anim: AnimationItem | null = null
build() {
Column() {
Canvas(this.ctx = new CanvasRenderingContext2D(new RenderingContextSettings(true)))
.width('100%').height(300).backgroundColor('#f5f5f5')
.onReady(() => this.load())
Row({ space: 10 }) {
Button('播放').onClick(() => this.anim?.play())
Button('暂停').onClick(() => this.anim?.pause())
Button('停止').onClick(() => this.anim?.stop())
}.margin({ top: 10 })
}.padding(15)
}
private load() {
this.ctx.imageSmoothingEnabled = true
this.ctx.imageSmoothingQuality = 'high'
this.anim = lottie.loadAnimation({
container: this.ctx,
renderer: 'canvas',
loop: true, autoplay: false,
name: 'demo',
path: 'common/animations/loading.json'
})
this.anim.addEventListener('DOMLoaded', () => this.anim?.setSpeed(1.2))
}
}
四 性能与体验建议
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。