Drawing Arc In Canvas View

This example will explain how to draw arc in canvas view.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it ArcDrawingExample.

2.) Write following into main.xml:

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ArcDrawingExampleActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

 

3.) Create and write following into MyView.java:

 

package com.example.arcdrawingexample;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

Paint paint;
Path path;

public MyView(Context context) {
super(context);
init();
}

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init(){
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(1);
paint.setStyle(Paint.Style.STROKE);

}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);

final RectF oval = new RectF();
paint.setStyle(Paint.Style.STROKE);
/*
* drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
*
* oval - The bounds of oval used to define the shape and size of the arc
* startAngle - Starting angle (in degrees) where the arc begins
* sweepAngle - Sweep angle (in degrees) measured clockwise
* useCenter - If true, include the center of the oval in the arc, and close it if it is being stroked. This will draw a wedge
* paint - The paint used to draw the arc
*/
oval.set(50, 50, 150, 150);
canvas.drawArc(oval, 0, 45, true, paint);

oval.set(200, 150, 450, 350);
canvas.drawArc(oval, 0, 270, true, paint);

oval.set(200, 400, 450, 600);
canvas.drawArc(oval, 0, 270, false, paint);
}
}

 

4.) Run for output.

Steps:

1.) Create a project named ArcDrawingExample and set the information as stated in the image.

Build Target: Android 4.4
Application Name: ArcDrawingExample
Package Name: com.example.ArcDrawingExample
Activity Name: ArcDrawingExampleActivity

arcdrawing1

2.) Open ArcDrawingExampleActivity.java file and write following code there:

 

package com.example.arcdrawingexample;

import android.app.Activity;
import android.os.Bundle;

public class ArcDrawingExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
}

 

3.) Compile and build the project.

Output

arcdrawing2