Saturday, October 31, 2015

Android Canvas- Arc Drawing

Hello My Android Developer friends, This blog I am writing to detail drawing "ARC" on android canvas. We will use canvas's method "drawCanvas" to draw arc on Andrdoi. Before proceeding to actual drawing, let me brief some point about arc,circle and angle division in Android canvas. In Android, angles are divided starting from 0 count at X axis and at nth point and 0 degree counting in clockwise direction to 360 degree ending at same point on X axis. To draw a circle one has to provide a center on pain i.e. its X and Y co-ordinates and its circle. Same applies to arc too, but somewhat in different way. An arc is drawn in rectangle, specifying the radius with which it has to be drawn. Here come the complexity and confusion for the one who is working on arc for first time. If you look at the function for canvas
public void drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
or
ppublic void drawArc (float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
it need a rectangle (or co-ordinates for rectangle), radius, startAngle and sweepAngle.Let me bring more details on what exactly they are required for. 1. Rectangle :- The rectangle on canvas in which an arc has to be fit. Whatever radius and size of arc it will be, arc will fit in this rectangle. For example, 2. Radius :- Radius of arc 3. startAngle :- an angle where arc will start drawing. For example, if you specify start angle as 20, then arc will start drawing itself from 20 degree i angular pane. 4. sweepAngle : Angle upto which an arc needs to drawn. For example, sweepAngle 120 will draw an arc from 20 degree to 120 degree i.e. an arc of total 100 degree. So the method drawArc( 0,0,500,400,20,120,true,paint) will draw an arc of 100 degree in rectangle of width 500, starting from top of android device screen. Happy Coding!!!!!