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%')
  }
}
相关推荐
光锥智能1 天前
小平板元年,华为MatePad Mini创出一片蓝海
华为·电脑
祥睿夫子1 天前
零基础搞定 ArkTS 类与对象!保姆级教程:定义→创建→测试全流程 + 代码示例
harmonyos
程序员潘Sir1 天前
HarmonyOS实现快递APP自动识别地址
harmonyos·鸿蒙
萌虎不虎1 天前
【鸿蒙(openHarmony)自定义音频播放器的开发使用说明】
华为·音视频·harmonyos
李洋-蛟龙腾飞公司1 天前
HarmonyOSAI编程万能卡片生成(一)
华为·ai编程·harmonyos
HarmonyOS_SDK1 天前
打破场景边界,支付宝联合实况窗提供全新出行服务体验
harmonyos
安卓开发者1 天前
鸿蒙NEXT应用数据持久化全面解析:从用户首选项到分布式数据库
数据库·分布式·harmonyos
森之鸟1 天前
开发中使用——鸿蒙播放本地mp3文件
华为·harmonyos
不是三毛没有半1 天前
华为USG6000v2 NAT模式下IPSEC IKE V1 实验
网络·网络安全·华为