HarmonyOS 自定义控件实现

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

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

HarmonyOS
1天前
浏览
收藏 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')

  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 自定义时间控件和日期控件
351浏览 • 1回复 待解决
HarmonyOS 如何实现自定义Toast
33浏览 • 1回复 待解决
HarmonyOS如何实现自定义scheme?
62浏览 • 1回复 待解决
HarmonyOS补充nativgation的自定义实现
368浏览 • 1回复 待解决
HarmonyOS 数字自定义键盘如何实现
354浏览 • 1回复 待解决
HarmonyOS CoverFlow效果自定义组件实现
300浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗的实现
38浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1170浏览 • 1回复 待解决
如何实现自定义应用入场动画
811浏览 • 1回复 待解决
使用自定义弹窗实现分享弹窗
600浏览 • 1回复 待解决