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%')
  }
}
相关推荐
zhengyquan1 小时前
华为 Pura X Max 将至:阔折叠再升级,4 月 20 日发布!
华为
Swift社区1 小时前
鸿蒙游戏中的“智能 NPC”架构设计
游戏·华为·harmonyos
2601_949593652 小时前
Flutter_OpenHarmony_三方库_webview_flutter网页内容嵌入与交互适配详解
flutter·harmonyos
前端不太难3 小时前
为什么 AI 游戏更适合鸿蒙?
人工智能·游戏·harmonyos
特立独行的猫a3 小时前
HarmonyOS 鸿蒙PC三方库移植:vcpkg方式的 Port 脚本编写简明教程
华为·harmonyos·openharmony·vcpkg·三方库移植
GLAB-Mary3 小时前
华为职业认证新版全景图介绍及重认证规则变更预通知
运维·服务器·华为·华为认证
Ww.xh4 小时前
鸿蒙Flutter混合开发实战:跨平台UI无缝集成
flutter·华为·harmonyos
chenbin___4 小时前
鸿蒙RN position: ‘absolute‘ 和 zIndex 的兼容性问题(转自千问)
前端·javascript·react native·harmonyos
苏杰豪5 小时前
DevEco Studio 启动鸿蒙模拟器提示未开启 Hyper-V,怎么处理?
华为·harmonyos
chenjixue5 小时前
记录下我理解的安卓,鸿蒙,ios, rn , fullter, Jetpack Compose,react 的相似与不同
android·华为·harmonyos