HarmonyOS canvas支持画圆角矩形吗

HarmonyOS canvas支持画圆角矩形吗

HarmonyOS
2024-08-09 15:45:19
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

请参考如下demo:

@Entry 
@Component 
struct Page { 
  @State message: string = 'Hello World'; 
  private readonly settings: RenderingContextSettings = new RenderingContextSettings(true); 
  private readonly ctx: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 
  /** 
   绘制圆角矩形 
   @param {* 必填} x x坐标 
   @param {* 必填} y y坐标 
   @param {* 必填} width 宽度 
   @param {* 必填} height 高度 
   @param {* 必填} radius 圆角半径 
   @param {* 非必填 默认值:’#456’} strokeColor 边框颜色 
   @param {* 非必填 无默认值} fillColor 填充颜色 
   @param {* 非必填 默认值:[]实线} lineDash 边框样式 
   */ 
  drawRoundRect(x: number, y: number, width: number, height: number, radius: number, strokeColor?: string, fillColor?: string, lineDash?: []) { 
    strokeColor = strokeColor || '#333'; 
    lineDash = lineDash || []; 
    this.ctx.beginPath(); 
    this.ctx.setLineDash(lineDash); 
    this.ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2); 
    this.ctx.lineTo(width - radius + x, y); 
    this.ctx.arc(width - radius + x, radius + y, radius, Math.PI * 3 / 2, Math.PI * 2); 
    this.ctx.lineTo(width + x, height + y - radius); 
    this.ctx.arc(width - radius + x, height - radius + y, radius, 0, Math.PI / 2); 
    this.ctx.lineTo(radius + x, height + y); 
    this.ctx.arc(radius + x, height - radius + y, radius, Math.PI / 2, Math.PI); 
    this.ctx.lineTo(x, y + radius); 
    this.ctx.strokeStyle = strokeColor; 
    this.ctx.stroke(); 
    if (fillColor) { 
      this.ctx.fillStyle = fillColor; 
      this.ctx.fill(); 
    } 
    this.ctx.closePath(); 
  } 
  build() { 
    Row() { 
      Column() { 
        Canvas(this.ctx) 
          .width('100%') 
          .height('100%') 
          .onReady(() => { 
            this.drawRoundRect(50, 50, 100, 100, 10) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-08-09 18:51:17
相关问题
如何使用canvas绘制圆角矩形
170浏览 • 1回复 待解决
修改分段按钮样式为圆角矩形
622浏览 • 1回复 待解决
利用native接口实现一个圆角矩形
1377浏览 • 1回复 待解决
WebView支持设置WebView圆角
644浏览 • 1回复 待解决
HarmonyOS span中如何设置圆角
103浏览 • 1回复 待解决
HarmonyOS lottie支持url
57浏览 • 1回复 待解决
HarmonyOS支持无线调试
3005浏览 • 1回复 待解决
HarmonyOS svg、canvas 的使用详情
124浏览 • 1回复 待解决
HarmonyOS现在支持震动反馈
120浏览 • 1回复 待解决
HarmonyOS 支持使用.9图
130浏览 • 1回复 待解决
HarmonyOS使用canvas如何使文字垂直居中
147浏览 • 1回复 待解决
HarmonyOS Payment Kit支持虚拟商品
92浏览 • 1回复 待解决
HarmonyOS支持其他开发板
13430浏览 • 4回复 待解决
HarmonyOS 3.0支持 MediaProjectionManager
3221浏览 • 1回复 待解决
HarmonyOS应用开发 支持C++
17148浏览 • 1回复 待解决
HarmonyOS 支持rsa使用公钥解密
129浏览 • 1回复 待解决