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.