.netmaui

How to draw a path with gradient on NET Maui app?


Looking at the GraphicsView examples created by MAUI creators:

https://github.com/dotnet/maui-samples/tree/main/7.0/UserInterface/Views/GraphicsViewDemos

you can find some GraphicsView examples, and specially an example that shows how to draw a reactangle with gradient (vertical, horizontal, etc..)

internal class VerticalLinearGradientPaintDrawable : IDrawable
{
    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        LinearGradientPaint linearGradientPaint = new LinearGradientPaint
        {
            StartColor = Colors.Yellow,
            EndColor = Colors.Green,
            // StartPoint is already (0,0)
            EndPoint = new Point(0, 1)
        };

        RectF linearRectangle = new RectF(10, 10, 200, 100);
        canvas.SetFillPaint(linearGradientPaint, linearRectangle);
        canvas.SetShadow(new SizeF(10, 10), 10, Colors.Grey);
        canvas.FillRoundedRectangle(linearRectangle, 12);
    }
}

But how to draw a path for a specific path (or polygon area)? I can't find the way.

Thanks.

I tried to find a method to draw a polygon with gradient but have not found the way to do it.


Solution

  • Update:

    Define in code behind:

    public MainPage()
    {
        InitializeComponent();
    
        Polygon polygon = new Polygon() { WidthRequest=70,HeightRequest=80,Stroke=Colors.Transparent};
        PointCollection points = new PointCollection();
        points.Add(new Point(0, 0));
        points.Add(new Point(40, 10));
        points.Add(new Point(70, 80));
        points.Add(new Point(10, 50));
        polygon.Points = points;
    
        GradientStopCollection gradientStops = new GradientStopCollection();
        gradientStops.Add(new GradientStop(color: Colors.Yellow, offset: 0));
        gradientStops.Add(new GradientStop(color: Colors.Green, offset: 1));
        LinearGradientBrush linearGradientBrush = new LinearGradientBrush(gradientStops,startPoint:new Point(0,0),endPoint:new Point(1,0));
        
        polygon.Fill = linearGradientBrush;
    
        mystack.Children.Add(polygon);
    

    You could try using a Linear gradient brushes and Polygon . Consider the following code:

    <StackLayout BackgroundColor="AliceBlue">
        <Polygon Points="0,0 40,10 70,80 10,50"
             Stroke="Transparent"
             StrokeThickness="5" >
                <Polygon.Fill>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Color="Yellow" Offset="0" />                  
                        <GradientStop Color="Green" Offset="1" />
                    </LinearGradientBrush>
                </Polygon.Fill>
        </Polygon>
    </StackLayout>
    

    Vertical linear gradient result (EndPoint="0,1"):

    enter image description here

    Horizontal linear gradient (EndPoint="1,0"):

    enter image description here

    Diagonal linear gradient (EndPoint="1,1"):

    enter image description here

    Hope it helps.