当前位置: 首页 > news >正文

HarmonyOS ArkUI Progress 进度条:5 种类型与动画演示

系列:鸿蒙 HarmonyOS 6.1 新特性实战 · 第 36 篇

Progress 组件支持 5 种不同的视觉类型,从线形到环形再到胶囊形,配合定时器可实现丝滑的进度动画。本篇完整演示所有类型,并给出通用的进度动画模式和生命周期清理最佳实践。

运行效果

初始状态,展示 5 种进度条类型(进度 0%):

点击“开始演示“后,进度动画进行中:

一、Progress 5 种类型

// Linear 线形(最常用,适合页面顶部加载进度) Progress({ value: 60, total: 100, type: ProgressType.Linear }) .width('100%').height(8) .color('#0066ff').backgroundColor('#e0e8ff') // Ring 环形(适合圆形 Loading 指示) Progress({ value: 60, total: 100, type: ProgressType.Ring }) .width(80).height(80) .color('#0066ff').backgroundColor('#e0e8ff') .style({ strokeWidth: 8 }) // Eclipse 月食形(视觉特殊,用于特定场景) Progress({ value: 60, total: 100, type: ProgressType.Eclipse }) .width(80).height(80) .color('#8e44ad').backgroundColor('#f0e0ff') // ScaleRing 刻度环形(带刻度线,精密进度) Progress({ value: 60, total: 100, type: ProgressType.ScaleRing }) .width(80).height(80) .color('#e67e22').backgroundColor('#fff3e0') .style({ strokeWidth: 8, scaleCount: 20, scaleWidth: 3 }) // Capsule 胶囊形(适合下载进度、文件传输) Progress({ value: 60, total: 100, type: ProgressType.Capsule }) .width('100%').height(32) .color('#27ae60').backgroundColor('#e0ffe8')

二、Ring + Stack 显示百分比数字

环形进度条中间叠加文字是常见需求,用Stack实现:

Stack() { Progress({ value: this.progress, total: 100, type: ProgressType.Ring }) .width(80).height(80) .color('#e74c3c').backgroundColor('#ffe0e0') .style({ strokeWidth: 8 }) Text(this.progress.toString() + '%') .fontSize(14).fontColor('#e74c3c').fontWeight(FontWeight.Bold) }

三、进度动画:setInterval + aboutToDisappear

进度动画核心是用setInterval定期更新@State,页面销毁时必须清理定时器:

@State progress: number = 0 @State running: boolean = false private timer: number = -1 // 必须在 aboutToDisappear 清理定时器 aboutToDisappear(): void { if (this.timer >= 0) { clearInterval(this.timer) } } startProgress(): void { this.progress = 0 this.running = true this.timer = setInterval(() => { this.progress += 2 if (this.progress >= 100) { this.progress = 100 this.running = false clearInterval(this.timer) this.timer = -1 } }, 60) // 每 60ms +2%,约 3 秒完成 }

为什么timer初始值用 -1?避免用0导致与合法 timer ID 冲突(部分系统会返回 0 作为首个 timer ID),用 -1 作为“未启动“哨兵值更安全。

四、style 属性

.style()支持的参数因类型而异:

// Linear / Capsule .style({ strokeWidth: 14, enableSmoothEffect: true }) // Ring .style({ strokeWidth: 8 }) // ScaleRing .style({ strokeWidth: 8, scaleCount: 20, scaleWidth: 3 })

enableSmoothEffect: true开启线形进度条的平滑动效(默认开启)。

完整代码

@Entry @Component struct Index { @State progress: number = 0 @State running: boolean = false private timer: number = -1 aboutToDisappear(): void { if (this.timer >= 0) { clearInterval(this.timer) } } startProgress(): void { this.progress = 0 this.running = true this.timer = setInterval(() => { this.progress += 2 if (this.progress >= 100) { this.progress = 100 this.running = false clearInterval(this.timer) this.timer = -1 } }, 60) } build() { Scroll() { Column({ space: 20 }) { Text('Progress 全类型进度条') .fontSize(22).fontWeight(FontWeight.Bold).fontColor('#1a1a1a') Text('当前进度:' + this.progress.toString() + '%') .fontSize(16).fontColor('#0066ff').fontWeight(FontWeight.Bold) Button(this.running ? '进行中...' : '开始演示') .width('100%').height(48).backgroundColor(this.running ? '#aaa' : '#0066ff').fontColor('#fff') .enabled(!this.running).onClick(() => { this.startProgress() }) // Linear Column({ space: 8 }) { Text('一、Linear 线形').fontSize(15).fontWeight(FontWeight.Bold).fontColor('#333') Progress({ value: this.progress, total: 100, type: ProgressType.Linear }) .width('100%').height(8).color('#0066ff').backgroundColor('#e0e8ff') Progress({ value: this.progress, total: 100, type: ProgressType.Linear }) .width('100%').height(14).color('#27ae60').backgroundColor('#e0ffe8') .style({ strokeWidth: 14, enableSmoothEffect: true }) } .padding(12).backgroundColor('#fff').borderRadius(8).width('100%') .border({ width: 1, color: '#e0e0e0' }).alignItems(HorizontalAlign.Start) // Ring Column({ space: 8 }) { Text('二、Ring 环形').fontSize(15).fontWeight(FontWeight.Bold).fontColor('#333') Row({ space: 24 }) { Column({ space: 4 }) { Progress({ value: this.progress, total: 100, type: ProgressType.Ring }) .width(80).height(80).color('#0066ff').backgroundColor('#e0e8ff') .style({ strokeWidth: 8 }) Text('默认').fontSize(12).fontColor('#666') } Column({ space: 4 }) { Stack() { Progress({ value: this.progress, total: 100, type: ProgressType.Ring }) .width(80).height(80).color('#e74c3c').backgroundColor('#ffe0e0') .style({ strokeWidth: 8 }) Text(this.progress.toString() + '%') .fontSize(14).fontColor('#e74c3c').fontWeight(FontWeight.Bold) } Text('叠加文字').fontSize(12).fontColor('#666') } } } .padding(12).backgroundColor('#fff').borderRadius(8).width('100%') .border({ width: 1, color: '#e0e0e0' }).alignItems(HorizontalAlign.Start) // Eclipse + ScaleRing Column({ space: 8 }) { Text('三、Eclipse 月食 / ScaleRing 刻度环').fontSize(15).fontWeight(FontWeight.Bold).fontColor('#333') Row({ space: 24 }) { Column({ space: 4 }) { Progress({ value: this.progress, total: 100, type: ProgressType.Eclipse }) .width(80).height(80).color('#8e44ad').backgroundColor('#f0e0ff') Text('Eclipse').fontSize(12).fontColor('#666') } Column({ space: 4 }) { Progress({ value: this.progress, total: 100, type: ProgressType.ScaleRing }) .width(80).height(80).color('#e67e22').backgroundColor('#fff3e0') .style({ strokeWidth: 8, scaleCount: 20, scaleWidth: 3 }) Text('ScaleRing').fontSize(12).fontColor('#666') } } } .padding(12).backgroundColor('#fff').borderRadius(8).width('100%') .border({ width: 1, color: '#e0e0e0' }).alignItems(HorizontalAlign.Start) // Capsule Column({ space: 8 }) { Text('四、Capsule 胶囊形').fontSize(15).fontWeight(FontWeight.Bold).fontColor('#333') Progress({ value: this.progress, total: 100, type: ProgressType.Capsule }) .width('100%').height(32).color('#27ae60').backgroundColor('#e0ffe8') } .padding(12).backgroundColor('#fff').borderRadius(8).width('100%') .border({ width: 1, color: '#e0e0e0' }).alignItems(HorizontalAlign.Start) } .padding(20).width('100%') } .width('100%').height('100%').backgroundColor('#f5f5f5') } }

API 速查

属性/方法说明
Progress({ value, total, type })创建进度条
.color('#0066ff')进度颜色
.backgroundColor('#e0e8ff')轨道背景色
.style({ strokeWidth, ... })样式配置(按类型不同)
ProgressType.Linear线形
ProgressType.Ring环形
ProgressType.Eclipse月食形
ProgressType.ScaleRing刻度环形
ProgressType.Capsule胶囊形

小结

  • Linear 是最通用的:加载进度、下载进度首选,简洁明了
  • Ring + Stack = 圆形进度:用 Stack 叠加 Text 显示百分比,是标准做法
  • 定时器必须在 aboutToDisappear 清理:否则页面离开后定时器仍在跑,浪费资源可能崩溃
  • timer 初始值用 -1 而非 0:0 可能是有效 timer ID,用哨兵值 -1 避免误清理

上一篇:Slider 与 Rating 实战 | 下一篇:DatePicker / TimePicker 日期时间选择器完全指南

http://www.jsqmd.com/news/1217192/

相关文章:

  • SonarQube自定义规则开发实战指南
  • Java生产者消费者模式实战与性能优化
  • 零基础学习指南:从入门到实践的全流程策略
  • Logback 系列(2):日志门面与实现 SLF4J + Logback
  • Tableau排序与分析功能的执行时序与协同原理
  • 2026年7月现代终端工具推荐(ssh客户端)(Wave Terminal、Tabby、Warp、WezTerm、XPipe、WindTerm)ssh工具、terminal工具
  • RK3399平台Linux 5.2.8内核移植与优化指南
  • 北京爱彼回收价格查询及靠谱平台实测排行(2026年7月最新数据) - 嘉价奢侈品回收平台
  • cpu飙高的原因有哪些,什么思路去排查
  • 如何用AEUX插件在3分钟内将Figma设计转换为After Effects动画图层
  • Android输入法开发指南:从基础到高级功能实现
  • OpenClaw 网关服务安装与启动失败问题排查(Windows 环境)
  • ROC曲线本质:二分类模型的阈值决策地形图
  • Python新手入门:环境搭建与开发工具全指南
  • Python GUI开发别再自虐了!Tkinter调布局调到你崩溃?Formatio一招拯救你的界面
  • ChatGPT自定义指令扩展至5000字符:技术开发者完整配置指南
  • 线性回归五大假设验证实战:从残差图到稳健推断
  • 基于YOLO的交通标志识别系统设计与优化
  • Python入门指南:环境配置与基础语法详解
  • AI法规分析:从纽约州项目看结构化文本处理的技术实践
  • 【无标题】vmware虚拟机无法勾选**将主机虚拟适配器连接到此网络** /没有网络(不显示网络图标)的解决方法
  • 投标不是拼运气:用项目化思维拆解投标全流程的核心逻辑
  • AM62L硬件防火墙实战:从寄存器精读到多区域安全配置
  • Python输入输出操作与文件处理完全指南
  • 重庆欧米茄回收价格查询与靠谱平台实测排行(2026年7月最新) - 诚收名表回收平台
  • 代码随想录数组章节(二)移除元素
  • 从1/2开始的编程学习生活
  • Python甩开C++几条街?这脚本语言凭啥封神,简单到让程序员尖叫
  • AM62L DDR16SS子系统寄存器深度解析与性能调优实战
  • 国内游戏APP上架全过程详解:从开发到运营的完整指南