question

njsokalski avatar image
0 Votes"
njsokalski asked LeonLu-MSFT commented

Cannot Fill Canvas During OnDraw

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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.




· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My class inherits from View with the following signature:

 public class SquareView : View

My override of OnDraw begins with the following:

 base.OnDraw(canvas);
 canvas.DrawColor(Resources.GetColor(Resource.Color.White, null));

The rest of the OnDraw override contains a combination of DrawPath, DrawCircle, DrawText and DrawRect, all of which display as expected. I tried removing the Draw* methods, as well as adding canvas.Save(); and canvas.Restore(); neither of which helped.

0 Votes 0 ·

I just add canvas.DrawColor(Resources.GetColor(Resource.Color.White, null)); in custom view, it worked as normal. Can you share SquareView code completely?

0 Votes 0 ·

And I put this SquareView in the RelativeLayout directly, Can you try it to exclude other layout issue.

0 Votes 0 ·