HarmonyOS Canvas绘制圆角?

Canvas 有直接提供圆角矩形的绘制吗

阅读 559
1 个回答

使用Canvas绘制图形请参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-drawing-customization-on-canvas-V5

示例代码:

@Entry
@Component
struct test {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('90%')
        .height(300)
        .backgroundColor('#ffff00')
        .onReady(() => {
          let radius = 10; //圆角弧度
          let x = 40; //左上角坐标x
          let y = 40; //左上角坐标y
          let width = 100; //矩形宽
          let height = 40; //矩形高
          this.context.beginPath();
          this.context.moveTo(x + radius, y);
          this.context.lineTo(x + width - radius, y);
          this.context.arcTo(x + width, y, x + width, y + radius, radius);
          this.context.lineTo(x + width, y + height - radius);
          this.context.arcTo(x + width, y + height, x + width - radius, y + height, radius);
          this.context.lineTo(x + radius, y + height);
          this.context.arcTo(x, y + height, x, y + height - radius, radius);
          this.context.lineTo(x, y + radius);
          this.context.arcTo(x, y, x + radius, y, radius);
          this.context.closePath();
          this.context.fill();
        })
    }
    .width('100%')
    .height('100%')
  }
}

或者可以尝试下ArkTS的绘制组件:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-drawing-components-rect-V5

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进