HarmonyOS NEXT练习:Progress环形进度条

140 阅读2分钟

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

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

Progress(options: {value: number, total?: number, type?: ProgressType})

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

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

组件属性

.value(value: number) //设置当前进度值。设置小于0的数值时置为0,设置大于total的数值时置为total。非法数值不生效。默认值:0
.color(value: ResourceColor | LinearGradient) //设置进度条前景色。
.style(value: ProgressStyleOptions | CapsuleStyleOptions | RingStyleOptions | LinearStyleOptions | ScaleRingStyleOptions | EclipseStyleOptions) //设置组件的样式。
.contentModifier(modifier:ContentModifier<ProgressConfiguration>) //在progress组件上,定制内容区的方法。modifier: 内容修改器,开发者需要自定义class实现ContentModifier接口。
.privacySensitive(isPrivacySensitiveMode: Optional<boolean>) //设置隐私敏感,隐私模式下进度清零,文字将被遮罩。true表示打开隐私敏感,false表示关闭隐私敏感。说明:设置null则不敏感。

ProgressConfiguration属性 value:当前进度值。当设置的数值小于0时,将其置为0。当设置的数值大于total时,将其置为total。默认值:0。取值范围:[0, total] total:进度总长。取值范围:[0, +∞]

CommonProgressStyleOptions属性 enableSmoothEffect:进度平滑动效的开关。开启平滑动效后设置进度,进度会从当前值渐变至设定值,否则进度从当前值突变至设定值。默认值:true,true表示开启进度平滑动效,false表示关闭进度平滑动效。

练习Demo:ProgressPage

@Entry
@Component
struct ProgressPage {
  @State progressValue: number = 40

  build() {
    Column() {
      Text('Progress环形进度条练习')
      Stack() {
        Progress({ value: this.progressValue, total: 150, type: ProgressType.Ring })
          .width('100%')
          .height('100%')
          .color(Color.Orange)// 进度条前景色为橙色
          .style({ strokeWidth: 30 }) // 设置strokeWidth进度条宽度为30vp
        Text(this.progressValue + '%').fontSize(30)
      }
      .width('100%')
      .aspectRatio(1)
      .padding({
        left: '20%',
        right: '20%',
        top: 20,
        bottom: 20
      })
    }
    .height('100%')
    .width('100%')
  }
}