HarmonyOS如何在Canvas画布上绘制圆角矩形

HarmonyOS
2024-12-26 15:28:42
902浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

使用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.
分享
微博
QQ
微信
回复
2024-12-26 18:35:10


相关问题
HarmonyOS Canvas绘制圆角矩形
1037浏览 • 1回复 待解决
如何使用canvas绘制圆角矩形
1261浏览 • 1回复 待解决
如何绘制圆角矩形
1023浏览 • 1回复 待解决
HarmonyOS Canvas绘制圆角
932浏览 • 1回复 待解决
HarmonyOS canvas支持画圆角矩形
943浏览 • 1回复 待解决
画布绘制图片如何实现?
861浏览 • 1回复 待解决
HarmonyOS 如何在Canvas中直接绘制SVG?
786浏览 • 1回复 待解决
HarmonyOS canvas清除画布的问题
620浏览 • 1回复 待解决
HarmonyOS Canvas如何设置圆角
739浏览 • 1回复 待解决
解决Canvas画布缩放时闪烁
2408浏览 • 1回复 待解决
画布绘制文字,垂直居中
947浏览 • 1回复 待解决
修改分段按钮样式为圆角矩形
1608浏览 • 1回复 待解决
如何操作canvas重新绘制
1930浏览 • 1回复 待解决
HarmonyOS Canvas绘制内容如何更新
645浏览 • 1回复 待解决
HarmonyOS 如何使用canvas绘制虚线
624浏览 • 1回复 待解决
利用native接口实现一个圆角矩形
2417浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。