HarmonyOS Canvas绘制圆角

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

HarmonyOS
2024-12-26 13:59:51
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

使用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%')
  }
}
  • 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.

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

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

分享
微博
QQ
微信
回复
2024-12-26 15:29:16
相关问题
HarmonyOS Canvas绘制圆角矩形
1038浏览 • 1回复 待解决
如何使用canvas绘制圆角矩形
1261浏览 • 1回复 待解决
HarmonyOS Canvas如何设置圆角
739浏览 • 1回复 待解决
HarmonyOS Canvas绘制曲线相关
793浏览 • 1回复 待解决
HarmonyOS canvas支持画圆角矩形吗
943浏览 • 1回复 待解决
如何操作canvas重新绘制
1934浏览 • 1回复 待解决
如何绘制圆角的矩形
1023浏览 • 1回复 待解决
HarmonyOS 如何使用canvas绘制虚线
624浏览 • 1回复 待解决
HarmonyOS Canvas绘制内容如何更新
648浏览 • 1回复 待解决
Canvas绘制内容如何动态更新
2822浏览 • 1回复 待解决
HarmonyOS canvas如何绘制成图片导出
735浏览 • 1回复 待解决
HarmonyOS Canvas绘制image的API相关问题
798浏览 • 1回复 待解决
HarmonyOS 如何在Canvas中直接绘制SVG?
791浏览 • 1回复 待解决
Canvas如何触发刷新重复绘制
1653浏览 • 1回复 待解决
HarmonyOS 如何清空canvas绘制的内容
859浏览 • 1回复 待解决
HarmonyOS Canvas中关于绘制图片问题
1062浏览 • 1回复 待解决
canvas怎么绘制资源目录下的图片
1496浏览 • 1回复 待解决