簡體   English   中英

如何在Android畫布上的自定義形狀上繪制筆觸

[英]How to draw stroke on custom shape on android canvas

我試圖將筆觸添加到添加的自定義形狀中。

這是代碼

    private void drawBackground(Canvas canvas) {

          float height = getHeight();
          float width = getWidth();

          canvas.drawRoundRect(0, 0, width, height, 5, 5, canvasPaint);
          canvas.drawRoundRect(0, 0, getTextWidth(), getHeight(), 5, 5, canvasStrokePaint);//to draw black stroke

          canvas.drawText(name, width / 2 - getTextWidth() / 2, getBitmapHeight() + 20, textPaint);
          Path path = new Path();

          path.moveTo((width / 3), height);
          path.lineTo((width / 2), (height + height / 3));
          path.lineTo((width - width / 3), height);
          path.lineTo((width / 3), height);

          path.close();

          canvas.drawPath(path, canvasPaint);
          canvas.drawPath(path, canvasStrokePaint);//to draw black stroke
}

輸出

輸出

在這里,您可以看到矩形和三角形的閉合邊。

但是筆觸應該在形狀之外。

需求

需求

提前致謝

您可以嘗試以黑色繪制此特定形狀,然后在其頂部書寫綠色的另一個形狀(稍小一些)。 結果應該與您預期的一樣。

試試這個代碼:

private void drawStroke(Canvas canvas) {
    int rectangleWidth = 20;
    int rectangleHeight = 10;
    float height = getHeight();
    float width = getWidth();

    // start
    canvasStrokePath.reset();
    canvasStrokePath.moveTo(0, 5);

    // first arc
    arcRect.set(0, 0, 10, 10);
    canvasStrokePath.arcTo(arcRect, 180, -90, true);

    // going right
    canvasStrokePath.lineTo(width - 10, 0);

    // second arc
    arcRect.set(width - 10, 0, width, 10);
    canvasStrokePath.arcTo(arcRect, 90, -90, true);

    // going bottom
    canvasStrokePath.lineTo(width, height - rectangleHeight - 10);

    // third arc
    arcRect.set(width - 10, height - rectangleHeight - 10, width, height - rectangleHeight);
    canvasStrokePath.arcTo(arcRect, 0, -90, true);

    // going to rectangle (right edge)
    canvasStrokePath.lineTo(width / 2 + rectangleWidth / 2, height - rectangleHeight);

    // going to rectangle center
    canvasStrokePath.lineTo(width / 2, height);

    // going to rectangle (left edge)
    canvasStrokePath.lineTo(width / 2 - rectangleWidth / 2, height - rectangleHeight);

    // going to left
    canvasStrokePath.lineTo(5, height - rectangleHeight);

    // fourth arc
    arcRect.set(0, height - rectangleHeight - 10, 10, height - rectangleHeight);
    canvasStrokePath.arcTo(arcRect, 270, -90, true);

    // going to top
    canvasStrokePath.lineTo(0, 5);

    canvasStrokePath.close();

    canvas.drawPath(canvasStrokePath, canvasStrokePaint);
}

我沒有建造它,我希望不會有錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM