Can I use gradientColor in GraphicsView?

mc 5,186 Reputation points
2024-11-23T11:09:52.6333333+00:00

how to use GradientColor in GraphicsView?

canvas.FillColor can not use gradientColor

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,050 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,331 Reputation points Microsoft External Staff
    2024-11-25T07:15:32.9333333+00:00

    Hello,

    Yes, you can. Please use Linear gradient brushes, and refer to the following code:

    internal class HorizontalLinearGradientPaintDrawable : 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(1, 0)
             };
     
             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);
         }
    

    You could also set verticalLinearGradientDrawable, diagonalLinearGradientDrawable, fourColorLinearGradientDrawable. For more details, please see the official sample- maui-samples/9.0/UserInterface/Views/GraphicsViewDemos/GraphicsViewDemos/Views/LinearGradientPaintPage.xaml at main · dotnet/maui-samples

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.