HarmonyOS 自定义控件实现

是否有刻度的自定义控件示例,如图所示:

HarmonyOS 自定义控件实现 -鸿蒙开发者社区

HarmonyOS
2024-12-25 13:30:39
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

参考示例如下:

import componentUtils from '@ohos.arkui.componentUtils'

@Entry
@Component
export struct WeightUi {
  @State @Watch('onCountUpdated') progressNum: number = 0
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  @State Max_Num: number = 15
  @State angleChange: number = 270 / this.Max_Num;

  onCountUpdated(): void {
    if (Math.round(this.progressNum * 10) / 10 > this.Max_Num) {
      console.info('progressNum22', this.progressNum)
      this.progressNum = 0
    }
    this.setProgress()
  }

  setProgress = (): void => {
    //画底层灰色进度条,数值固定不动
    let endAngle: number = this.progressNum //读取当前数值
    if (endAngle > this.Max_Num) {
      endAngle = 0
    } //防止溢出
    let modePosition: componentUtils.ComponentInfo = componentUtils.getRectangleById('flag1'); //获取相对位置信息
    let circleX =
      px2vp(modePosition.localOffset.x + modePosition.size.width * 0.5) //flag相对父组件的位置加上自身长度,将px转换成pv
    let circleY = px2vp(modePosition.localOffset.y + modePosition.size.height * 0.5)
    this.context.beginPath()
    this.context.arc(circleX, circleY, 60, 135 * (Math.PI / 180),
      45 * (Math.PI / 180))
    this.context.lineWidth = 5
    this.context.strokeStyle = "#bbb7fc"
    this.context.lineCap = 'round'
    this.context.stroke()
    //画选中进度条
    this.context.beginPath()
    let angleChange1: number =
      (135 + this.angleChange * endAngle + 0.01) *
        (Math.PI / 180) //计算弧形进度条的结束位置
    this.context.arc(circleX, circleY, 60, 135 * (Math.PI / 180),
      angleChange1)
    this.context.lineWidth = 5
    this.context.strokeStyle = "#ac49f5"
    this.context.lineCap = 'round'
    this.context.stroke()
    //画外层虚线进度条
    let outerRadius = 75 //外部圆圈半径
    let inerRadius = 68 //内部圆圈半径
    let addAngle: number = 1 * this.angleChange * (Math.PI / 180) //设置数值每变化1,画一条线,防止过于密集
    for (let angle = 135 * (Math.PI / 180);
      angle <= 405 * (Math.PI / 180); angle += addAngle) {
      this.context.beginPath()
      this.context.moveTo(circleX + outerRadius * Math.cos(angle), circleY + outerRadius * Math.sin(angle)) //起点
      this.context.lineTo(circleX + inerRadius * Math.cos(angle), circleY + inerRadius * Math.sin(angle)) //终点
      this.context.strokeStyle = "#bbb7fc"
      this.context.lineWidth = 2 //线段宽度
      this.context.stroke()
    }

    //给虚线进度条上色,和弧形进度条同步
    let angleChange2: number = endAngle === 0 ?
      (135 + this.angleChange * endAngle - 0.01) * (Math.PI / 180) :
      (135 + this.angleChange * endAngle) * (Math.PI / 180) //计算虚线进度条的结束位置
    for (let angle = 135 * (Math.PI / 180); angle <= angleChange2; angle += addAngle) {
      this.context.beginPath()
      this.context.moveTo(circleX + outerRadius * Math.cos(angle), circleY + outerRadius * Math.sin(angle)) //起点
      this.context.lineTo(circleX + inerRadius * Math.cos(angle), circleY + inerRadius * Math.sin(angle)) //终点
      this.context.strokeStyle = "#ac49f5"
      this.context.lineWidth = 2 //线段宽度
      this.context.stroke()
    }
  }

  build() {
    Column() { //中间当前数据
      Text(this.progressNum.toString())
        .width(50)
        .height(50)
        .textAlign(TextAlign.Center)
        .fontColor(Color.Black)
        .fontSize(25)
        .fontWeight(FontWeight.Bold)
        .position({ x: '40%', y: '32%' })
        .id('flag1')

      Canvas(this.context)
        .width("100%")
        .height('100%')
        .onReady(() => {
          this.setProgress()
        })

      Button() { //更改数据按钮
        Text('数据++')
          .fontColor(Color.White)
          .fontSize(16)
      }
      .height(40)
      .width('100%')
      .backgroundColor(Color.Blue)
      .type(ButtonType.Capsule)
      .position({ x: '0%', y: '70%' })
      .onClick(() => {
        //在这里传入值
        this.progressNum = Number(Math.round(this.progressNum * 10) / 10 + 1)
      })
    }
    .height('100%')
    .width('100%')
    .id('flag2')

  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
分享
微博
QQ
微信
回复
2024-12-25 15:28:06
相关问题
HarmonyOS 自定义时间控件和日期控件
1192浏览 • 1回复 待解决
HarmonyOS 如何实现自定义Toast
650浏览 • 1回复 待解决
HarmonyOS如何实现自定义scheme?
820浏览 • 1回复 待解决
HarmonyOS 数字自定义键盘如何实现
1187浏览 • 1回复 待解决
HarmonyOS补充nativgation的自定义实现
825浏览 • 1回复 待解决
HarmonyOS 自定义StepperView组件如何实现
806浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗的实现
883浏览 • 1回复 待解决
HarmonyOS CoverFlow效果自定义组件实现
1099浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1917浏览 • 1回复 待解决