HarmonyOS如何绘制多边形并给多边形填充颜色

通过DrawContext 的 canvas 绘制了倒三角形,但是不知道怎么将这个闭合的倒三角形填充颜色。代码如下:

class TriangleRenderNode extends RenderNode { 
  flag: boolean = false; 
  color: common2D.Color = {alpha: 255, red: 255, green: 0, blue: 0} 
 
  draw(context: DrawContext) { 
 
    const canvas = context.canvas; 
    const pen = new drawing.Pen(); 
    pen.setStrokeWidth(vp2px(1)); 
    pen.setColor(this.color); 
    let path = new drawing.Path(); 
    path.moveTo(0,0); 
    path.lineTo(vp2px(context.size.width),0); 
    path.lineTo(vp2px(context.size.width*0.5),vp2px(context.size.height)); 
    path.lineTo(0,0) 
    path.close(); 
    canvas.attachPen(pen); 
    canvas.drawPath(path); 
    canvas.detachPen(); 
  } 
}
HarmonyOS
2024-08-26 17:28:36
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

参考如下:

draw(context: DrawContext) { 
  const canvas = context.canvas; 
  const pen = new drawing.Pen(); 
  pen.setStrokeWidth(vp2px(1)); 
  pen.setColor(this.color); 
  let path = new drawing.Path(); 
  path.moveTo(0,0); 
  path.lineTo(vp2px(context.size.width),0); 
  path.lineTo(vp2px(context.size.width*0.5),vp2px(context.size.height)); 
  path.lineTo(0,0) 
  path.close(); 
  let brush = new drawing.Brush(); 
  brush.setColor({alpha:255, red:0,green: 255, blue:255}) 
  canvas.attachPen(pen); 
  canvas.attachBrush(brush) 
  canvas.drawPath(path); 
  canvas.detachPen(); 
}

代码如上,填充图形需要使用brush

分享
微博
QQ
微信
回复
2024-08-26 22:53:13
相关问题
关于鸿蒙系统升级正式版都有啥机
9332浏览 • 1回复 待解决
HarmonyOS 支持gif设置颜色
33浏览 • 1回复 待解决
HarmonyOS 怎么图片渲染不同的颜色
34浏览 • 1回复 待解决
TextField怎么某段文字设置颜色
8989浏览 • 2回复 待解决