Friday, February 22, 2013

Android beginner tutorial: Part 90 - Path and ArcShape

In this tutorial we will learn about drawing primitive shapes in Android using Path and ArcShape classes.

The Path class is similar in usage to the way shapes are drawn using AS3. It has the moveTo() and lineTo() methods that you've probably worked with before if you ever tried drawing graphics using Actionscript3.

Using this class it is possible to draw unordinary primitive shapes, like, for example, a star.

First we set up the path for the lines to follow:

Path p = new Path();
p.moveTo(50, 0);
p.lineTo(25, 100);
p.lineTo(100, 50);
p.lineTo(0, 50);
p.lineTo(75, 100);
p.lineTo(50, 0);

You can then apply the drawing to a ShapeDrawable object.

ShapeDrawable shape = new ShapeDrawable(new PathShape(p, 100, 100));
shape.setIntrinsicHeight(100);
shape.setIntrinsicWidth(100);
shape.getPaint().setColor(Color.RED);
shape.getPaint().setStyle(Paint.Style.STROKE);


Next class is ArcShape. This one lets us draw arcs, basically. There are two values you need to pass to the ArcShape constructor - float startAngle and float sweepAngle.

Example:

ShapeDrawable shape = new ShapeDrawable(new ArcShape(0, 250));
shape.setIntrinsicHeight(100);
shape.setIntrinsicWidth(100);
shape.getPaint().setColor(Color.RED);




Now we've covered all the primitive shape drawing classes.

Thanks for reading!

1 comment:

MarkOfShame said...

Is there anyway to make these custom shapes into buttons?

Post a Comment