Search code examples
mauiskiasharp

How to draw Skia Svg Path with stroke and fill, but with different stroke color


I want to draw a path using skia in my maui project, but I want to use a different color for the stroke then for the fill, for example I want to draw some path with a black border and filled with red.

I tried this

SKPath sVgPath = SKPath.ParseSvgPathData(path); // path holds the svg path as a string
sVgPath.Transform(_matrix);

paint.Color = SKColors.Red;
paint.Style = SKPaintStyle.Fill;

paint.Color = SKColors.Black;
paint.Style = SKPaintStyle.Stroke;

canvas.DrawPath(sVgPath, paint);

but the rendered shape is not filled with red, it seems like the paint style stroke clears the red again.

I also tried to stroke first and then fill, but then the stroke is gone again.

Can this be done ? Seems simple to me but I can't figure it out

EDIT
I am using VS 2022 the project is maui using net8.0 and intended for android


Solution

  • You may try setting strokePaint and fillPaint separately.

    SKPaint strokePaint = new SKPaint
    {
        Style = SKPaintStyle.Stroke,
        Color = SKColors.Red,
        StrokeWidth = 20
    };
    
    SKPaint fillPaint = new SKPaint
    {
        Style = SKPaintStyle.Fill,
        Color = SKColors.Blue
    };
    

    And draw the path according to whether the path is stroked or filled or both, and in what order:

            canvas.DrawPath(path, strokePaint);
            canvas.DrawPath(path, fillPaint);
    

    For more info, you may refer to The Path Fill Types.