OpenHarmony中实现动画动态效果调整,主要通过属性动画(核心方式)、转场动画、CSS/JS动画及SVG动画四大类技术,覆盖组件属性、页面过渡、样式变化及矢量图形等多场景的动态调整需求。
属性动画是OpenHarmony中最灵活的动画方式,通过改变组件的可动画属性(如缩放、旋转、位移、透明度、尺寸等),实现平滑的动态效果。核心API为animateTo,支持动态配置动画参数(时长、曲线、延迟、重复性)。
@State装饰器绑定状态变量(如scaleValue),在点击事件中调用animateTo修改变量值,组件会自动过渡到新状态。@Entry
@Component
struct Index {
@State scaleValue: number = 1.0; // 绑定缩放属性
build() {
Row() {
Column() {
Text('Hello World')
.fontSize(50)
.scale({ x: this.scaleValue, y: this.scaleValue }) // 绑定缩放值
.onClick(() => {
animateTo({ duration: 500, curve: Curve.Linear }, () => {
this.scaleValue = this.scaleValue === 1.0 ? 0.5 : 1.0; // 动态切换缩放值
});
})
}
}
}
}
animateTo支持动态传入duration(时长)、curve(曲线,如Linear、EaseInOut)、delay(延迟)等参数,实现不同节奏的动画效果。例如:animateTo({ duration: 1000, curve: Curve.EaseInOut, delay: 200 }, () => {
this.translateValue = 100; // 动态调整位移
});
animateTo({ duration: 500 }, () => {
this.translateValue = -30;
this.rotateAngle = 90;
this.alphaValue = 0.5;
});
repeat参数设置动画重复次数(Infinity为无限循环),或reverse参数实现往返动画:animateTo({ duration: 1000, repeat: Infinity, reverse: true }, () => {
this.rotateAngle += 360; // 无限旋转
});
转场动画用于组件进入或离开页面时的动态效果,避免生硬的显示/隐藏。OpenHarmony提供了PageTransition组件(页面级)和transition方法(组件级)两种方式。
PageTransition组件设置页面入场/退场动画,支持SlideEffect(滑动)、FadeEffect(淡入淡出)等效果:@Entry
@Component
struct PageTransitionExample {
build() {
PageTransition({ type: PageTransitionType.SlideUp }) { // 入场时从下方滑动
Column() {
Text('Page Transition Example')
}
}
}
}
transition方法为组件添加插入/删除动画,动态调整动画参数(如缩放、透明度):@Entry
@Component
struct TransitionExample {
@State showComponent: boolean = true;
build() {
Column() {
if (this.showComponent) {
Text('Hello')
.transition({ insert: { scale: { from: 0, to: 1 } }, delete: { opacity: { from: 1, to: 0 } } }) // 插入时缩放,删除时淡出
}
Button('Toggle')
.onClick(() => this.showComponent = !this.showComponent)
}
}
}
通过CSS的@keyframes规则或JS动画库(如GSAP),实现样式属性的动态变化,适用于页面元素或Web内容的动画调整。
@keyframes规则,设置动画关键帧(如from到to),通过animation属性动态调整动画参数(时长、迭代次数、方向):@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.animated-element {
animation-name: fadeIn;
animation-duration: 2s; // 动态调整时长
animation-iteration-count: infinite; // 无限循环
animation-direction: alternate; // 往返方向
}
gsap.to方法动态创建动画,支持更复杂的缓动函数(如elastic、bounce)和属性调整:import { gsap } from '@ohos/gsap';
@Entry
@Component
struct GsapExample {
build() {
Column() {
Text('GSAP Animation')
.id('animatedText')
Button('Animate')
.onClick(() => {
gsap.to('#animatedText', { // 动态调整字体大小和颜色
fontSize: '40px',
color: 'red',
duration: 1,
ease: 'elastic.out(1, 0.3)'
});
})
}
}
}
SVG动画通过<animate>、<animateMotion>、<animateTransform>标签,实现矢量图形(如文字、形状)的动态效果,支持动态调整路径、颜色、变换等属性。
<animate>):通过attributeName指定要动画的属性(如font-size、fill),from/to设置起始/结束值,dur设置时长,repeatCount设置重复次数:<svg>
<text x="100" y="100" fill="blue">
Hello
<animate attributeName="font-size" from="30" to="60" dur="3s" repeatCount="indefinite"/> <!-- 字体大小动态变化 -->
<animate attributeName="fill" from="red" to="blue" dur="3s" repeatCount="indefinite"/> <!-- 颜色动态变化 -->
</text>
</svg>
<animateMotion>):通过path属性设置运动路径,rotate属性控制旋转方向,实现图形沿路径移动的动态效果:<svg>
<circle cx="0" cy="0" r="10" fill="red">
<animateMotion dur="3s" repeatCount="indefinite" rotate="auto">
<mpath href="#motionPath"/> <!-- 沿指定路径运动 -->
</animateMotion>
</circle>
<path id="motionPath" d="M100,100 Q200,50 300,100" fill="none" stroke="gray"/>
</svg>
<animateTransform>):通过attributeName="transform"设置变换类型(如translate、rotate、scale),values设置多个变换值,keyTimes设置关键帧时间点,实现复杂变换的动态调整:<svg>
<rect x="50" y="50" width="50" height="50" fill="green">
<animateTransform attributeName="transform" type="translate"
values="0,0; 100,0; 100,100; 0,100; 0,0"
dur="4s" repeatCount="indefinite"/> <!-- 矩形沿矩形路径移动 -->
</rect>
</svg>
以上方法覆盖了OpenHarmony中组件属性、页面过渡、样式变化、矢量图形等多场景的动态效果调整需求,开发者可根据具体场景选择合适的方式,通过调整参数(时长、曲线、重复性、路径等)实现灵活的动画效果。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。