1

追加したカスタム シェイプにストロークを追加しようとしています。

ここにコードがあります

    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
}

出力

出力

ここでは、長方形と三角形の閉じたエッジがあることがわかります。

ただし、ストロークはシェイプの外側にある必要があります。

要件

要件

前もって感謝します

4

2 に答える 2

0

このコードを試してください:

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);
}

私はそれを構築しませんでした、間違いがないことを願っています

于 2018-08-02T09:52:14.227 に答える