Cannot Fill Canvas During OnDraw

Nathan Sokalski 4,111 Reputation points
2022-05-12T22:31:44.263+00:00

I have a custom view in which I override OnDraw. During this, I want to fill the entire canvas with a single color (in my case White). I have tried all of the following:

canvas.DrawColor(Resources.GetColor(Resource.Color.White, null));
canvas.DrawRGB(255, 255, 255);
canvas.DrawARGB(255, 255, 255, 255);

None of these seem to be having any effect. All the other draw methods that I am using work fine. What am I doing wrong? How can I perform the simple task of filling the canvas? Thanks.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-05-13T08:25:08.24+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Does your custom view implement View or ViewGroup?

    If your custom view implement ViewGroup, please fill the entire canvas in the dispatchDraw (Canvas canvas) method, because you fill Canvas During OnDraw will be covered by child control. It is not a good place to fill color.

    If your custom view implement View, you can fill Canvas in OnDraw method. Can you share the complete code about custom view?

    And please move your canvas.DrawColor(Color.Red); to the base.OnDraw(canvas); blew. Here is my test code, you can test it, if you get red background and black Circle

       internal class MyCustomControl : View  
           {  
               public MyCustomControl(Context context) : base(context)  
               {  
                   init();  
               }  
               public MyCustomControl(Context context, IAttributeSet attrs) : base(context, attrs)  
               {  
                   init();  
               }  
         
               public MyCustomControl(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)  
               {  
                   init();  
               }  
               Paint mPaint;  
               Path mPath;  
               private void init()  
               {  
         
                   mPaint = new Paint();  
                   mPath = new Path();  
               }  
               protected override void OnDraw(Canvas canvas)  
               {  
                   base.OnDraw(canvas);  
                   canvas.DrawColor(Color.Red);  
                   canvas.DrawCircle(500, 500, 300, mPaint);           
                   canvas.Save();            
                   canvas.DrawPath(mPath, mPaint);  
                   canvas.Restore();  
               }  
           }  
       }  
    

    Best Regards,

    Leon Lu


    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.


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.