HarmonyOS 如何绘制一条虚线

目前了解到只有边框支持虚线,还有其他什么方式可以绘制一条虚线的?

HarmonyOS
2024-12-25 13:29:50
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

可以使用Canvas画布实现,参考示例如下:

@Entry
@Component
struct CanvasExample {
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  /* * text: 绘制的文本内容 * x: 开始位置的x坐标 * y: 开始位置的y坐标 * maxWidth: 最大换行宽度 * lineHeight: 行高 */
  wrapText(text: string, x: number, y: number, maxWidth?: number, lineHeight?: number) {
    if (typeof maxWidth == 'undefined') {
      maxWidth = 300;
    }
    if (typeof lineHeight == 'undefined') {
      lineHeight = 20
    }
    this.context.beginPath();
    this.context.moveTo(x, y + 10);
    this.context.lineTo(maxWidth, y + 10);
    this.context.stroke();
    this.context.closePath();
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context).width('100%').height('100%').onReady(() => {
        //设置线条粗细
        this.context.lineWidth = 1
        // 设置线条宽度
        this.context.strokeStyle = "black"
        // 设置线条颜色
        this.context.setLineDash([25, 10])
        // 设置虚线间隔
        this.wrapText("", 0, 40, this.context.width, 30)
      })
    }
    .width('100%').height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-25 16:07:22
相关问题
HarmonyOS GridItem之间有一条竖线
52浏览 • 1回复 待解决
HarmonyOS 如何使用canvas绘制虚线
171浏览 • 1回复 待解决
HarmonyOS 如何实现虚线
868浏览 • 1回复 待解决