HarmonyOS 自定义进度条 Stage模型

通过onTouch监听滑动,动态改变圆角

TypeScript 复制代码
let radius = 0

@Entry
@Component
struct TestPage {
  @State flip: boolean = false
  @State progress:number = 20

  build() {
    Row() {
      Column() {
        RelativeContainer(){
          Rect({ width: '100%', height: 40 })
            .radius(10)
            .fill("#505050")
            .stroke(Color.Transparent)
            .alignRules({
              left:{anchor:'__container__',align:HorizontalAlign.Start},
              center:{anchor:'__container__',align:VerticalAlign.Center}
            })
            .id('brightness_back')
          Rect({ width: `${this.progress}%`, height: 40 })
            .radius([
              [10, 10],
              [radius, radius],
              [radius, radius],
              [10, 10]
            ])
            .fill("#FFFFFF")
            .stroke(Color.Transparent)
            .alignRules({
              left:{anchor:'__container__',align:HorizontalAlign.Start},
              center:{anchor:'__container__',align:VerticalAlign.Center}
            })
            .id('brightness_progress')
          Image($r('app.media.brightness'))
            .objectFit(ImageFit.None)
            .width(40)
            .alignRules({
              left:{anchor:'__container__',align:HorizontalAlign.Start},
              center:{anchor:'__container__',align:VerticalAlign.Center}
            })
            .id('brightness_icon')
          Text(`${this.progress}%`)
            .fontColor(0x000000)
            .fontSize(14)
            .alignRules({
              center:{anchor:'__container__',align:VerticalAlign.Center},
              left:{anchor:'brightness_icon',align:HorizontalAlign.End}
            })
            .id('text_brightness')
        }
        .width('90%')
        .margin({top:10})
        .height(40)
        .onTouch((event: TouchEvent) => {
          let x = event.touches[0].x
          // @ts-ignore
          let width:number = event.target.area.width
          this.progress = Math.max(0,Math.min(100,Math.round(x/width * 100)))
          let length = width - x
          if( x <= width && length < 10 ){
            radius = (10 - length)
          } else if (x > width){
            radius = 10
          } else {
            radius = 0
          }
        })
      }
      .width('100%')
    }.backgroundColor(Color.Gray)
    .height('100%')
  }
}
相关推荐
李李李勃谦10 分钟前
Flutter 框架跨平台鸿蒙开发 - 手工技能学习
学习·flutter·华为·harmonyos
世人万千丶16 分钟前
开源鸿蒙跨平台Flutter开发:时间管理番茄钟应用
学习·flutter·华为·开源·harmonyos
提子拌饭13333 分钟前
开源鸿蒙跨平台Flutter开发:AR太空探索应用
flutter·华为·架构·开源·harmonyos·鸿蒙
互联网散修41 分钟前
零基础鸿蒙应用开发第三十一节:await简化异步编程与Promise进阶并发
harmonyos
梁山好汉(Ls_man)1 小时前
鸿蒙电脑 DevEco Studio 预览版
华为·harmonyos
梁山好汉(Ls_man)1 小时前
鸿蒙_使用组件导航Navigation搭建应用框架
华为·harmonyos·鸿蒙·arkui
autumn20052 小时前
Flutter 框架跨平台鸿蒙开发 - AR历史场景穿越
flutter·华为·ar·harmonyos
AI_零食2 小时前
开源鸿蒙跨平台Flutter开发:手账记事模板库应用
学习·flutter·华为·开源·harmonyos·鸿蒙
提子拌饭1332 小时前
开源鸿蒙跨平台Flutter开发:家庭影像传承系统
flutter·华为·开源·harmonyos·鸿蒙
HwJack202 小时前
HarmonyOS NDK 开发:冲破 ArkUI 性能桎梏的“降维打击”
华为·harmonyos