How to Draw Circle by Canvas in Android

How to draw circle by canvas in Android?

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

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.drawCircle(x, y, radius, paint);

}

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

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

Android canvas draw circle with different colors

This is for 5 pieces. You can change radius, degree and other parameters to make this dynamically.

private fun drawCircle300(): Bitmap? {

var radius = 150f
val bitmap = Bitmap.createBitmap(
(radius * 2).toInt(),
(radius * 2).toInt(),
Bitmap.Config.ARGB_8888
)

val canvas = Canvas(bitmap)

val paint = Paint().apply {
strokeWidth = 3f
style = Paint.Style.FILL
isAntiAlias = true
}

val rect = RectF(0f, 0f, 300f, 300f)

var degree = 72f
var currentAngle = 0f
for (i in 0 until 5) {
if (i == 0)
paint.color = Color.BLACK
if (i == 1)
paint.color = Color.BLUE
if (i == 2)
paint.color = Color.RED
if (i == 3)
paint.color = Color.YELLOW
if (i == 4)
paint.color = Color.GREEN
canvas.drawArc(rect, currentAngle, degree, true, paint)
currentAngle += degree
}

return bitmap
}

Sample Image

How to draw a ring using canvas in android?

  1. You can draw a circle with a thick brush (use setStrokeWidth).
  2. You can draw two circles, one inside another. One filled with 'ring' color, and another (inner one) filled with screen 'background color'

Android: Draw circle in custom canvas on button click

You shouldn't call onDraw function of the canvas directly from the activity. Instead make a public method in the canvas view and write your logic there and call invalidate at the end. This calls onDraw method and contents are drawn on the screen again according to your new logic.

If you want a circle to be drawn at random locations on screen each time button is clicked you can do something like this

public class CanvasView extends View {
public Paint mPaint;
public static Canvas mCanvas;
private int mPivotX = 0;
private int mPivotY = 0;
private int radius = 60;

//constructor
public CanvasView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
}

public void drawCircle() {

int minX = radius * 2;
int maxX = getWidth() - (radius *2 );

int minY = radius * 2;
int maxY = getHeight() - (radius *2 );

//Generate random numbers for x and y locations of the circle on screen
Random random = new Random();
mPivotX = random.nextInt(maxX - minX + 1) + minX;
mPivotY = random.nextInt(maxY - minY + 1) + minY;

//important. Refreshes the view by calling onDraw function
invalidate();

}

//what I want to draw is here
protected void onDraw(Canvas canvas) {
mCanvas = canvas;
super.onDraw(mCanvas);
canvas.drawColor(Color.GRAY);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Style.STROKE);
mPaint.setAntiAlias(true);
canvas.drawCircle(mPivotX, mPivotY, radius, mPaint);
}
}

And then onButtonClick

mKick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
soundPool.play(kick, leftVolume, rightVolume, priority, no_loop, normal_playback_rate);

mCanvasView.drawCircle()

}

Hope it helps!

how to draw a circle on Canvas using java for android

Update your createBitMap method like this

private void createBitMap() {
// Create a mutable bitmap
Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

bitMap = bitMap.copy(bitMap.getConfig(), true);
// Construct a canvas with the specified bitmap to draw into
Canvas canvas = new Canvas(bitMap);
// Create a new paint with default settings.
Paint paint = new Paint();
// smooths out the edges of what is being drawn
paint.setAntiAlias(true);
// set color
paint.setColor(Color.BLACK);
// set style
paint.setStyle(Paint.Style.STROKE);
// set stroke
paint.setStrokeWidth(4.5f);
// draw circle with radius 30
canvas.drawCircle(50, 50, 30, paint);
// set on ImageView or any other view
imageView.setImageBitmap(bitMap);

}

Android: How to draw circle within two point?

The first two paramateres of addCircle are the x and y coordinates of the center. Assuming A and B are the furthest distance from each other on the circle you want, then the center should a point equidistant to both, hence:

float centerX = (pointA.x + pointB.x) /2
float centerY = (pointA.y + pointB.y) /2

And your radius should be, the distance between A and B, thus:

float radius = (Math.sqrt(Math.pow(x2−x1, 2) + Math.pow(y2−y1, 2))) / 2

Draw a circle onto a view (android)

A couple of observations:

You need to take into account the width and height assigned to your view when determining your circle's center point and radius.

You should take into account the padding assigned to your View so you don't draw in that reserved portion.

You should avoid allocating objects within your onDraw method since this gets called a lot.

In order to allow your view to be specified in an XML layout, you need to provide the constructor that takes a Context and an AttributeSet. The AttributeSet is the mechanism by which your XML attributes are passed to your view.

Give this a try:

package com.tak3r07.montecarlopi;

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

public class CircleView extends View
{
private static final int DEFAULT_CIRCLE_COLOR = Color.RED;

private int circleColor = DEFAULT_CIRCLE_COLOR;
private Paint paint;

public CircleView(Context context)
{
super(context);
init(context, null);
}

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

private void init(Context context, AttributeSet attrs)
{
paint = new Paint();
paint.setAntiAlias(true);
}

public void setCircleColor(int circleColor)
{
this.circleColor = circleColor;
invalidate();
}

public int getCircleColor()
{
return circleColor;
}

protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);

int w = getWidth();
int h = getHeight();

int pl = getPaddingLeft();
int pr = getPaddingRight();
int pt = getPaddingTop();
int pb = getPaddingBottom();

int usableWidth = w - (pl + pr);
int usableHeight = h - (pt + pb);

int radius = Math.min(usableWidth, usableHeight) / 2;
int cx = pl + (usableWidth / 2);
int cy = pt + (usableHeight / 2);

paint.setColor(circleColor);
canvas.drawCircle(cx, cy, radius, paint);
}
}

Draw a hollow circle on an Android Canvas

As asked :)
The solution is to set the style to be Paint.Style.STROKE



Related Topics



Leave a reply



Submit