Draw circle in android-ThrowExceptions

Exception or error:

How can I draw circle between two points using the Android SDK?

How to solve:

Create A bitmap then draw on its canvas and then add this bitmap to an imageview or button or whatever you want.

Create A bitmap:

    Bitmap bmp = Bitmap.createBitmap(width, height, config);

Draw on the bitmap canvas

    Canvas c = new Canvas(bmp);
    c.drawCircle(cx, cy, radius, paint)

setting to imageview

    img.setBackgroundDrawable(new BitmapDrawable(bmp));

###

You don’t necessarily need to create a bitmap manual.

For example if you use a SurfaceView, in the SurfaceView class you are able to draw a circle:

public class Circle extends SurfaceView implements SurfaceHolder.Callback {
private Paint paint;

    public void onDraw(Canvas canvas) {
        canvas.drawCircle(x, y, radius, this.paint);
    }
}

Then you can add the SurfaceView to your Activity class like:

public class MovingCircle extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new Circle());
    }

}

I hope this will also help you.

Leave a Reply

Your email address will not be published. Required fields are marked *