How to draw a circle on canvas in Android?

I want to draw a circle on the canvas. Here is my code:

[MyActivity.java]:

public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { ... setContentView(new View(this,w,h)); } } 

[View.java]:

 public class View extends SurfaceView { public View(Context context, int w, int h) { super(context); Canvas grid = new Canvas(Bitmap.createBitmap(h,w, Bitmap.Config.ARGB_8888)); grid. drawColor(Color.WHITE); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); grid.drawCircle(w/2, h/2 , w/2, paint); } } 

So, I only have a black screen without a circle. Why is this not working? How to fix it?

+62
java android android-canvas
Jul 30 '13 at 18:25
source share
7 answers

You can override the onDraw method of your view and draw a circle.

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

For a better reference on creating custom views, check out the official Android documentation.

http://developer.android.com/training/custom-views/custom-drawing.html

+73
Jul 30 '13 at 19:01
source share
 import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } public class MyView extends View { Paint paint = null; public MyView(Context context) { super(context); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int x = getWidth(); int y = getHeight(); int radius; radius = 100; paint.setStyle(Paint.Style.FILL); paint.setColor(Color.WHITE); canvas.drawPaint(paint); // Use Color.parseColor to define HTML colors paint.setColor(Color.parseColor("#CD5C5C")); canvas.drawCircle(x / 2, y / 2, radius, paint); } } } 

Edit if you want to draw a circle in the center. You can also transfer your entire canvas to the center, then draw a circle in the center. Using

 canvas.translate(getWidth()/2f,getHeight()/2f); canvas.drawCircle(0,0, radius, paint); 

These two links also help.

http://www.compiletimeerror.com/2013/09/introduction-to-2d-drawing-in-android.html#.VIg_A5SSy9o

http://android-coding.blogspot.com/2012/04/draw-circle-on-canvas-canvasdrawcirclet.html

+30
Dec 10 '14 at
source share
 public class CircleView extends View { private static final String COLOR_HEX = "#E74300"; private final Paint drawPaint; private float size; public CircleView(final Context context, final AttributeSet attrs) { super(context, attrs); drawPaint = new Paint(); drawPaint.setColor(Color.parseColor(COLOR_HEX)); drawPaint.setAntiAlias(true); setOnMeasureCallback(); } @Override protected void onDraw(final Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(size, size, size, drawPaint); } private void setOnMeasureCallback() { ViewTreeObserver vto = getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { removeOnGlobalLayoutListener(this); size = getMeasuredWidth() / 2; } }); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener) { if (Build.VERSION.SDK_INT < 16) { getViewTreeObserver().removeGlobalOnLayoutListener(listener); } else { getViewTreeObserver().removeOnGlobalLayoutListener(listener); } } } 

Xml example: create a circle from 5dp

  <com.example.CircleView android:layout_width="10dp" android:layout_height="10dp"/> 
+9
Jul 02 '15 at 9:24
source share
 @Override public void onDraw(Canvas canvas){ canvas.drawCircle(xPos, yPos,radius, paint); } 

Above is the code to display the circle. Adjust the parameters to suit your requirements.

+3
Jul 30 '13 at 18:27
source share

try it

enter image description here

All the code to draw a circle or download the project source code and test it on your Android studio. Programmatically draw a circle on canvas

 import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Point; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.widget.ImageView; public class Shape { private Bitmap bmp; private ImageView img; public Shape(Bitmap bmp, ImageView img) { this.bmp=bmp; this.img=img; onDraw(); } private void onDraw(){ Canvas canvas=new Canvas(); if (bmp.getWidth() == 0 || bmp.getHeight() == 0) { return; } int w = bmp.getWidth(), h = bmp.getHeight(); Bitmap roundBitmap = getRoundedCroppedBitmap(bmp, w); img.setImageBitmap(roundBitmap); } public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) { Bitmap finalBitmap; if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false); else finalBitmap = bitmap; Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), finalBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), finalBitmap.getHeight()); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.parseColor("#BAB399")); canvas.drawCircle(finalBitmap.getWidth() / 2 + 0.7f, finalBitmap.getHeight() / 2 + 0.7f, finalBitmap.getWidth() / 2 + 0.1f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(finalBitmap, rect, rect, paint); return output; } 
0
Jan 13 '17 at 12:09 on
source share

If you use the custom custom ViewView extension class, you need to call the canvas.invalidate () method, which will call the onDraw method for internal use. You can use the default API for the canvas to draw a circle. X, Y coordinates define the center of the circle. You can also define the color and style of the drawing and convey the drawing object.

 public class CustomView extends View { public CustomView(Context context, AttributeSet attrs) { super(context, attrs); setupPaint(); } } 

Define the default drawing parameters and the canvas (initialize the drawing in the constructor so that you can reuse the same object everywhere and change only certain parameters where necessary)

 private Paint drawPaint; // Setup paint with color and stroke styles private void setupPaint() { drawPaint = new Paint(); drawPaint.setColor(Color.BLUE); drawPaint.setAntiAlias(true); drawPaint.setStrokeWidth(5); drawPaint.setStyle(Paint.Style.FILL_AND_STROKE); drawPaint.setStrokeJoin(Paint.Join.ROUND); drawPaint.setStrokeCap(Paint.Cap.ROUND); } 

And initialize the canvas object

 private Canvas canvas; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); this.canvas = canvas; canvas.drawCircle(xCordinate, yCordinate, RADIUS, drawPaint); } 

And finally, for every view update or new drawing on the screen, you need to call the invalidate method. Remember that your entire review has been redrawn, so this is an expensive call. Make sure that you do only the necessary operations in onDraw

 canvas.invalidate(); 

For more information on drawing on canvas, see https://medium.com/@mayuri.k18/android-canvas-for-drawing-and-custom-views-e1a3e90d468b.

0
Jan 11 '19 at 11:40
source share

Here's an example of drawing a circle stroke on canvas

 val paint = Paint().apply { color = Color.RED style = Paint.Style.STROKE strokeWidth = 10f } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) canvas?.drawCircle(200f, 100f, 100f, paint) } 

Result

pYian.png

An example for drawing a solid circle of canvas

 val paint = Paint().apply { color = Color.RED } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) canvas?.drawCircle(200f, 100f, 100f, paint) } 

Result

A5w9M.png

Hope this helps

0
Jul 19 '19 at 3:20
source share



All Articles