Drawing 2D Images: Recipe
Drawing 2D Images: Recipe
Recipe
1. Create a new Mono for Android application named TwoDDrawing. Notice that the project name does not start with a number. 2. Delete Main.axml, it is not necessary for this recipe. 3. Create a new class in the project named MyOvalShape, have it extend Android.Views.View, and implement a constructor that takes a Android.Content.Context:
5. Add the following code into the constructor of MyOvalShape to create the shape that is to be drawn:
public MyOvalShape(Context context) : base(context) { var paint = new Paint(); paint.SetARGB(255, 200, 255, 0); paint.SetStyle(Paint.Style.Stroke); paint.StrokeWidth = 4; _shape = new ShapeDrawable(new OvalShape()); _shape.Paint.Set(paint); _shape.SetBounds(20, 20, 300, 200); }
7. Modify Activity1.OnCreate so that the oval will be drawn when the application starts:
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(new MyOvalShape(this)); }
Additional Information
ShapeDrawable
is an extension of the Drawable class which defines some basic geometric shapes such as arcs, ovals, and rectangles.