0% found this document useful (0 votes)
18 views3 pages

Drawing 2D Images: Recipe

This document provides a recipe for drawing a simple 2D oval shape in an Android application. The recipe involves creating a custom View class that extends View and uses a ShapeDrawable object to draw an oval on the canvas. It overrides the OnDraw method to call the ShapeDrawable's Draw method. The Activity's OnCreate is modified to set the custom View as the content view so that the oval is drawn when the app starts.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Drawing 2D Images: Recipe

This document provides a recipe for drawing a simple 2D oval shape in an Android application. The recipe involves creating a custom View class that extends View and uses a ShapeDrawable object to draw an oval on the canvas. It overrides the OnDraw method to call the ShapeDrawable's Draw method. The Activity's OnCreate is modified to set the custom View as the content view so that the oval is drawn when the app starts.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DRAWING 2D IMAGES

This recipe will provide an example of drawing a simple 2-D object.

Sample Code: [2DDrawing.zip] Related Google Documentation: ShapeDrawable

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:

public class MyOvalShape : View { public MyOvalShape(Context context) : base(context) { } }

4. Add an instance variable to the class:


private readonly ShapeDrawable _shape;

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); }

6. Override OnDraw inside MyOvalShape, this will draw the oval:


protected override void OnDraw(Canvas canvas) { _shape.Draw(canvas); }

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)); }

8. Run the application on a device, and an oval shape will be drawn:

Additional Information
ShapeDrawable

is an extension of the Drawable class which defines some basic geometric shapes such as arcs, ovals, and rectangles.

You might also like