OpenHarmony图形图像动态效果实现指南
一、实现路径总览
二、ArkUI 声明式动画实践
import Curves from '@ohos.curves';
import router from '@ohos.router';
@Entry
@Component
struct Logo {
@State opacityValue: number = 0;
@State scaleValue: number = 0;
private curve1 = Curves.cubicBezier(0.4, 0, 1, 1);
build() {
Shape() {
// ... 你的图形 ...
}
.scale({ x: this.scaleValue, y: this.scaleValue })
.opacity(this.opacityValue)
.onAppear(() => {
animateTo({
duration: 1000,
delay: 100,
curve: this.curve1,
onFinish: () => {
// 动画完成后跳转
setTimeout(() => router.replaceUrl({ url: 'pages/Home' }), 1000);
}
}, () => {
this.opacityValue = 1;
this.scaleValue = 1;
});
})
}
}
@Entry
@Component
struct MyComponent {
@State show: boolean = true;
build() {
Column() {
if (this.show) {
Text('Hello')
.id('myText')
.transition(TransitionEffect.OPACITY.animation({ duration: 1000 }))
}
Button('toggle')
.onClick(() => { this.show = !this.show; })
}
}
}
三、SVG 与逐帧动画
<svg width="400" height="400">
<text x="200" y="200" fill="blue" font-size="30">
Hello
<animate attributeName="font-size" from="30" to="60" dur="3s" repeatCount="indefinite"/>
<animate attributeName="fill" from="red" to="blue" dur="3s" repeatCount="indefinite"/>
<animate attributeName="opacity" from="1" to="0.3" dur="3s" repeatCount="indefinite"/>
</text>
<path d="M300,200 h-150 a150 150 0 1 0 150 -150 z" fill="none" stroke="blue" stroke-width="5"/>
<path fill="red" d="M-5,-5 L10,0 L-5,5 L0,0 Z">
<animateMotion dur="2s" repeatCount="indefinite" rotate="auto-reverse"
path="M300,200 h-150 a150 150 0 1 0 150 -150 z"/>
</path>
</svg>
@Entry
@Component
export default struct FrameAnim {
@State manX: number = 0;
private timerId: number = -1;
start() {
this.timerId = setInterval(() => {
this.manX += 5;
if (this.manX > 300) this.manX = 0;
}, 80);
}
stop() {
if (this.timerId >= 0) {
clearInterval(this.timerId);
this.timerId = -1;
}
}
build() {
Column() {
Image($r('app.media.man'))
.width(100).height(100)
.translate({ x: this.manX, y: 0 })
Button('run').onClick(() => this.start())
Button('stop').onClick(() => this.stop())
}
}
}
四、底层图形与性能优化
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。