HarmonyOS Next 之各类动画实现详解
2025-06-18 14:40:02
108次阅读
0个评论
动画官方链接: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-animation-V5
1. 渐变动画(淡入淡出)
通过修改组件的透明度实现淡入淡出效果。
代码实现
// FadeAnimation.ets
@Component
struct FadeAnimationExample {
@State opacityValue: number = 0.2;
build() {
Column() {
Button('点击淡入/淡出')
.opacity(this.opacityValue)
.onClick(() => {
animateTo({ duration: 1000, curve: Curve.EaseInOut }, () => {
this.opacityValue = this.opacityValue === 0.2 ? 1 : 0.2;
});
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
- 位移动画(平移) 通过修改组件的位置属性实现水平移动。
代码实现
// TranslateAnimation.ets
@Component
struct TranslateAnimationExample {
@State translateX: number = 0;
build() {
Column() {
Button('向右移动')
.translate({ x: this.translateX })
.onClick(() => {
animateTo({ duration: 800, curve: Curve.Spring }, () => {
this.translateX = this.translateX === 0 ? 200 : 0;
});
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
- 旋转动画 通过修改组件的旋转角度实现旋转效果。
代码实现
// RotateAnimation.ets
@Component
struct RotateAnimationExample {
@State rotateValue: number = 0;
build() {
Column() {
Image($r('app.media.icon'))
.width(100)
.height(100)
.rotate({ angle: this.rotateValue })
.onClick(() => {
animateTo({ duration: 1200 }, () => {
this.rotateValue += 360;
});
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
- 组合动画(缩放 + 颜色渐变) 同时修改多个属性实现复合动画效果。
代码实现
// CompositeAnimation.ets
@Component
struct CompositeAnimationExample {
@State scaleValue: number = 1;
@State colorValue: Color = Color.Blue;
build() {
Column() {
Text('缩放与变色')
.fontSize(20)
.fontColor(Color.White)
.backgroundColor(this.colorValue)
.padding(20)
.scale({ x: this.scaleValue, y: this.scaleValue })
.onClick(() => {
animateTo({ duration: 500 }, () => {
this.scaleValue = this.scaleValue === 1 ? 1.5 : 1;
this.colorValue = this.colorValue === Color.Blue ? Color.Red : Color.Blue;
});
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
- 转场动画(页面切换) 使用 transition 实现组件出现/消失的过渡效果。
代码实现
// TransitionAnimation.ets
@Component
struct TransitionAnimationExample {
@State isShow: boolean = true;
build() {
Column() {
if (this.isShow) {
Text('消失动画')
.fontSize(24)
.transition({ type: TransitionType.Insert, opacity: 0, translate: { x: 100 } })
}
Button(this.isShow ? '隐藏' : '显示')
.onClick(() => {
animateTo({ duration: 600 }, () => {
this.isShow = !this.isShow;
});
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
以上是一些动画小案例,希望可以在项目中有用到,增加一些动感,谢谢!
00
- 0回答
- 0粉丝
- 1关注
相关话题
- 122.HarmonyOS NEXT 数字滚动动画详解(二):动画实现机制
- 116.HarmonyOS NEXT 跑马灯组件详解(四):动画实现机制
- 123.HarmonyOS NEXT 数字滚动动画详解(三):布局与样式实现
- 06 HarmonyOS Next性能优化之LazyForEach 列表渲染基础与实现详解 (一)
- HarmonyOS Next 之-沉浸式详解与实战
- 83.HarmonyOS NEXT 动画系统详解:构建流畅的用户体验
- 210.HarmonyOS NEXT系列教程之 TabsConcaveCircle组件动画系统详解
- 124.HarmonyOS NEXT 数字滚动动画详解(四):性能优化指南
- HarmonyOS NEXT 小说阅读器总结篇之核心功能实现与翻页效果技术详解
- 144.HarmonyOS NEXT系列教程之3D立方体旋转轮播案例讲解之动画实现原理
- 94.HarmonyOS NEXT动画系统实现教程:深入理解FuncUtils
- 204.HarmonyOS NEXT系列教程之 TabsConcaveCircle组件动画系统实现
- HarmonyOS Next 之使用AES实现对称加解密
- 99.HarmonyOS NEXT跑马灯组件教程:动画配置与参数详解
- 121.HarmonyOS NEXT 数字滚动动画详解(一):基础结构与原理