Drawing on the canvas of a View

Sometimes using the animation and shape APIs are not enough to create the image or app that we want to create. This may be to create a game or draw a complex image.

How to do it...

Drawing to a canvas can be easily done by creating a new view that derives from View, and then overriding the OnDraw() method:

  1. First, we will need our custom View instance that implements the constructors that allow us to instantiate the view from either code or a layout file:
    public class CustomView : View { public CustomView(Context context) : base(context) { } public CustomView(Context context, IAttributeSet attrs) : base(context, attrs) { } public CustomView( Context context, IAttributeSet attrs, int defStyle) : base (context, attrs, ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.