在 OpenHarmony(ArkUI / eTS) 中,实现“图表动画”通常有两种思路:
下面分别说明。
@ohos/chart 或 SIG 图表)部分官方/社区图表库已内置动画支持。
示例(伪代码):
import { LineChart } from '@ohos/charts'
LineChart({
data: [...],
animation: true,
animationDuration: 1000
})
✅ 优点
⚠️ 注意
Canvas + requestAnimationFrame@Entry
@Component
struct ChartAnimation {
private ctx: CanvasRenderingContext2D
@State progress: number = 0
aboutToAppear() {
this.animate()
}
animate() {
let start = Date.now()
const duration = 1000
const tick = () => {
const now = Date.now()
this.progress = Math.min((now - start) / duration, 1)
this.draw()
if (this.progress < 1) {
requestAnimationFrame(tick)
}
}
requestAnimationFrame(tick)
}
draw() {
const canvas = this.ctx
canvas.clearRect(0, 0, 300, 200)
const height = 150 * this.progress
canvas.fillStyle = '#007DFF'
canvas.fillRect(50, 200 - height, 40, height)
}
build() {
Canvas(this.ctx)
.width(300)
.height(200)
}
}
✅ 优点
❌ 缺点
@State height: number = 0
aboutToAppear() {
animateTo({ duration: 1000 }, () => {
this.height = 150
})
}
build() {
Column() {
Row()
.width(40)
.height(this.height)
.backgroundColor('#007DFF')
}
}
✅ 适合
| 动画类型 | 实现方式 |
|---|---|
| 渐显 | opacity |
| 增长 | height / scale |
| 平移 | translate |
| 路径绘制动画 | Canvas + progress |
| 数据切换 | 插值动画 |
setStaterequestAnimationFrame如果你能告诉我:
我可以给你 可直接运行的完整示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。