HarmonyOS Canvas绘制圆角矩形

我需要在屏幕上 canvas 绘制圆角矩形,查看了 api ,我应该是需要使用 lineto、arcto 进行绘制。但是发现 arcto 方法貌似并不按照我理解的进行绘制,能否给一段代码?

HarmonyOS
2024-12-25 07:43:01
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

以下Demo供参考

@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('#ff8f00')
        .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.fillStyle = Color.Red
          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.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
分享
微博
QQ
微信
回复
2024-12-25 09:48:46
相关问题
如何使用canvas绘制圆角矩形
1261浏览 • 1回复 待解决
HarmonyOS Canvas绘制圆角
932浏览 • 1回复 待解决
如何绘制圆角矩形
1023浏览 • 1回复 待解决
HarmonyOS canvas支持画圆角矩形
943浏览 • 1回复 待解决
修改分段按钮样式为圆角矩形
1609浏览 • 1回复 待解决
HarmonyOS Canvas如何设置圆角
739浏览 • 1回复 待解决
HarmonyOS Canvas绘制曲线相关
793浏览 • 1回复 待解决
利用native接口实现一个圆角矩形
2422浏览 • 1回复 待解决
HarmonyOS 如何使用canvas绘制虚线
624浏览 • 1回复 待解决
HarmonyOS Canvas绘制内容如何更新
648浏览 • 1回复 待解决
如何操作canvas重新绘制
1934浏览 • 1回复 待解决
HarmonyOS canvas如何绘制成图片导出
735浏览 • 1回复 待解决
Canvas绘制内容如何动态更新
2822浏览 • 1回复 待解决
HarmonyOS Canvas绘制image的API相关问题
798浏览 • 1回复 待解决
HarmonyOS 如何在Canvas中直接绘制SVG?
791浏览 • 1回复 待解决
HarmonyOS 如何清空canvas绘制的内容
859浏览 • 1回复 待解决
Canvas如何触发刷新重复绘制
1653浏览 • 1回复 待解决