HarmonyOS:进度条 (Progress)

一、概述

Progress是进度条显示组件,显示内容通常为目标操作的当前进度。

二、创建进度条

Progress通过调用接口来创建,接口调用形式如下:

typescript 复制代码
Progress(options: {value: number, total?: number, type?: ProgressType})

其中,value用于设置初始进度值,total用于设置进度总长度,type用于设置Progress样式。

php 复制代码
Progress({ value: 24, total: 100, type: ProgressType.Linear }) // 创建一个进度总长为100,初始进度值为24的线性进度条

三、设置进度条样式

Progress有5种可选类型,通过ProgressType可以设置进度条样式,ProgressType类型包括:ProgressType.Linear(线性样式)、 ProgressType.Ring(环形无刻度样式)、ProgressType.ScaleRing(环形有刻度样式)、ProgressType.Eclipse(圆形样式)和ProgressType.Capsule(胶囊样式)。

四、示例

4.1 线性样式进度条(默认类型)

说明 从API version9开始,组件高度大于宽度时,自适应垂直显示;组件高度等于宽度时,保持水平显示。
效果图

scss 复制代码
Progress({ value: 30, total: 100, type: ProgressType.Linear }).width(100).height(10)
Progress({ value: 60, total: 100, type: ProgressType.Linear }).width(10).height(100)

4.2 环形无刻度样式进度条

效果图

scss 复制代码
// 从左往右,1号环形进度条,默认前景色为蓝色渐变,默认strokeWidth进度条宽度为2.0vp
Progress({ value: 30, total: 100, type: ProgressType.Ring }).width(60).height(60)
// 从左往右,2号环形进度条
Progress({ value: 60, total: 100, type: ProgressType.Ring }).width(60).height(60)
  .color(Color.Blue)// 进度条前景色为灰色
  .style({ strokeWidth: 6 }) // 设置strokeWidth进度条宽度为10.0vp

4.3 环形有刻度样式进度条

效果图

scss 复制代码
Row({ space: 10 }) {
        Progress({ value: 20, total: 150, type: ProgressType.ScaleRing }).width(60).height(60)
          .backgroundColor(Color.Grey)
          .style({ scaleCount: 20, scaleWidth: 5 }) // 设置环形有刻度进度条总刻度数为20,刻度宽度为5vp
        Progress({ value: 20, total: 150, type: ProgressType.ScaleRing }).width(60).height(60)
          .backgroundColor(Color.Grey)
          .style({ strokeWidth: 15, scaleCount: 20, scaleWidth: 5 }) // 设置环形有刻度进度条宽度15,总刻度数为20,刻度宽度为5vp
        Progress({ value: 20, total: 150, type: ProgressType.ScaleRing }).width(60).height(60)
          .backgroundColor(Color.Grey)
          .style({ strokeWidth: 15, scaleCount: 20, scaleWidth: 3 }) // 设置环形有刻度进度条宽度15,总刻度数为20,刻度宽度为3vp
      }

4.4 圆形样式进度条

效果图

scss 复制代码
Row({ space: 10 }) {
        // 从左往右,1号圆形进度条,默认前景色为蓝色
        Progress({ value: 10, total: 150, type: ProgressType.Eclipse }).width(60).height(60)
        // 从左往右,2号圆形进度条,指定前景色为灰色
        Progress({ value: 20, total: 150, type: ProgressType.Eclipse }).color(Color.Grey).width(60).height(60)
      }

4.5 胶囊样式进度条

说明

  • 头尾两端圆弧处的进度展示效果与ProgressType.Eclipse样式相同。
  • 中段处的进度展示效果为矩形状长条,与ProgressType.Linear线性样式相似。
  • 组件高度大于宽度的时候自适应垂直显示。
    效果图
scss 复制代码
Row({ space: 10 }) {
        Progress({ value: 10, total: 150, type: ProgressType.Capsule }).width(100).height(50)
        Progress({ value: 20, total: 150, type: ProgressType.Capsule }).width(50).height(100).color(Color.Grey)
        Progress({ value: 50, total: 150, type: ProgressType.Capsule })
          .width(50)
          .height(100)
          .color(Color.Blue)
          .backgroundColor(Color.Black)
      }

五、场景示例

更新当前进度值,如应用安装进度条,可通过点击Button增加progressValue,value属性将progressValue设置给Progress组件,进度条组件即会触发刷新,更新当前进度。
效果图

完整示例代码 TestProgress.ets

scss 复制代码
@Entry
@Component
struct TestProgress {
  @State message: string = 'Progress';
  @State progressValue: number = 0; // 设置进度条初始值为0

  build() {
    Column({ space: 10 }) {
      Text(this.message)
        .id('TestProgressHelloWorld')
        .fontSize($r('app.float.page_text_font_20fp'))
        .fontWeight(FontWeight.Bold)
        .margin({ top: 10 })

      Text('线性样式进度条(默认类型)')
        .fontColor(Color.Black)
        .fontSize(16)
        .margin({ top: 10 })
      Progress({ value: 30, total: 100, type: ProgressType.Linear }).width(100).height(10)
      Progress({ value: 60, total: 100, type: ProgressType.Linear }).width(10).height(100)

      Text('环形无刻度样式进度条')
        .fontColor(Color.Black)
        .fontSize(16)
        .margin({ top: 10 })

      Row({ space: 10 }) {
        // 从左往右,1号环形进度条,默认前景色为蓝色渐变,默认strokeWidth进度条宽度为2.0vp
        Progress({ value: 30, total: 100, type: ProgressType.Ring }).width(60).height(60)
        // 从左往右,2号环形进度条
        Progress({ value: 60, total: 100, type: ProgressType.Ring }).width(60).height(60)
          .color(Color.Blue)// 进度条前景色为灰色
          .style({ strokeWidth: 6 }) // 设置strokeWidth进度条宽度为10.0vp
      }

      Text('环形有刻度样式进度条')
        .fontColor(Color.Black)
        .fontSize(16)
        .margin({ top: 10 })
      Row({ space: 10 }) {
        Progress({ value: 20, total: 150, type: ProgressType.ScaleRing }).width(60).height(60)
          .backgroundColor(Color.Grey)
          .style({ scaleCount: 20, scaleWidth: 5 }) // 设置环形有刻度进度条总刻度数为20,刻度宽度为5vp
        Progress({ value: 20, total: 150, type: ProgressType.ScaleRing }).width(60).height(60)
          .backgroundColor(Color.Grey)
          .style({ strokeWidth: 15, scaleCount: 20, scaleWidth: 5 }) // 设置环形有刻度进度条宽度15,总刻度数为20,刻度宽度为5vp
        Progress({ value: 20, total: 150, type: ProgressType.ScaleRing }).width(60).height(60)
          .backgroundColor(Color.Grey)
          .style({ strokeWidth: 15, scaleCount: 20, scaleWidth: 3 }) // 设置环形有刻度进度条宽度15,总刻度数为20,刻度宽度为3vp
      }

      Text('圆形样式进度条')
        .fontColor(Color.Black)
        .fontSize(16)
        .margin({ top: 10 })
      Row({ space: 10 }) {
        // 从左往右,1号圆形进度条,默认前景色为蓝色
        Progress({ value: 10, total: 150, type: ProgressType.Eclipse }).width(60).height(60)
        // 从左往右,2号圆形进度条,指定前景色为灰色
        Progress({ value: 20, total: 150, type: ProgressType.Eclipse }).color(Color.Grey).width(60).height(60)
      }

      Text('胶囊样式进度条')
        .fontColor(Color.Black)
        .fontSize(16)
        .margin({ top: 10 })
      Row({ space: 10 }) {
        Progress({ value: 10, total: 150, type: ProgressType.Capsule }).width(100).height(50)
        Progress({ value: 20, total: 150, type: ProgressType.Capsule }).width(50).height(100).color(Color.Grey)
        Progress({ value: 50, total: 150, type: ProgressType.Capsule })
          .width(50)
          .height(100)
          .color(Color.Blue)
          .backgroundColor(Color.Black)
      }

      Progress({ value: this.progressValue, total: 100, type: ProgressType.Capsule }).width(100).height(40)
      Button('进度条+5, 当前进度值:' + this.progressValue)
        .fontColor(Color.Black)
        .fontWeight(FontWeight.Medium)
        .fontSize(16)
        .onClick(() => {
          this.progressValue += 5
          if (this.progressValue > 100) {
            this.progressValue = 0;
          }
        })

    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
互联网散修7 小时前
零基础鸿蒙应用开发第十九节:解锁灵活数据存储新技能Map/Set
harmonyos
枫叶丹47 小时前
【HarmonyOS 6.0】ArkData 应用间配置共享:构建跨应用协作新范式
开发语言·华为·harmonyos
互联网散修8 小时前
零基础鸿蒙应用开发第十八节:内置泛型工具类型应用
harmonyos
轻口味8 小时前
HarmonyOS 6 自定义人脸识别模型8:MindSpore Lite框架介绍与使用
c++·华为·ai·harmonyos
枫叶丹48 小时前
【HarmonyOS 6.0】ArkData 分布式数据对象新特性:资产传输进度监听与接续传输能力深度解析
开发语言·分布式·华为·wpf·harmonyos
枫叶丹48 小时前
【HarmonyOS 6.0】Agent Framework Kit深度解析:构建应用与智能体的无缝连接
华为·aigc·harmonyos
亚历克斯神21 小时前
Flutter for OpenHarmony: Flutter 三方库 mutex 为鸿蒙异步任务提供可靠的临界资源互斥锁(并发安全基石)
android·数据库·安全·flutter·华为·harmonyos
钛态21 小时前
Flutter 三方库 smartstruct 鸿蒙化字段映射适配指南:介入静态预编译引擎扫除视图及数据模型双向强转类型错乱隐患,筑稳如磐石的企业级模型治理防线-适配鸿蒙 HarmonyOS ohos
flutter·华为·harmonyos
键盘鼓手苏苏21 小时前
Flutter 组件 csv2json 适配鸿蒙 HarmonyOS 实战:高性能异构数据转换,构建 CSV 流式解析与全栈式数据映射架构
flutter·harmonyos·鸿蒙·openharmony
雷帝木木21 小时前
Flutter 三方库 hrk_logging 的鸿蒙化适配指南 - 实现标准化分层日志记录、支持多目的地输出与日志分级过滤
flutter·harmonyos·鸿蒙·openharmony·hrk_logging