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%')
  }
}
相关推荐
chenyingjian13 小时前
鸿蒙|性能优化-概述与工具使用
harmonyos
二流小码农13 小时前
鸿蒙开发:路由组件升级,支持页面一键创建
android·ios·harmonyos
TT_Close18 小时前
【Flutter×鸿蒙】debug 包也要签名,这点和 Android 差远了
android·flutter·harmonyos
TT_Close1 天前
【Flutter×鸿蒙】FVM 不认鸿蒙 SDK?4步手动塞进去
flutter·swift·harmonyos
hqk1 天前
鸿蒙项目实战:手把手带你实现 WanAndroid 布局与交互
android·前端·harmonyos
TT_Close1 天前
【Flutter×鸿蒙】一个"插队"技巧,解决90%的 command not found
flutter·harmonyos
Hcourage3 天前
鸿蒙工程获取C/C++代码覆盖
harmonyos
二流小码农3 天前
鸿蒙开发:上传一张参考图片便可实现页面功能
android·ios·harmonyos
万少3 天前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
Huang兄4 天前
鸿蒙-List和Grid拖拽排序:仿微信小程序删除效果
harmonyos·arkts·arkui