Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a ring using canvas in android?

Tags:

android

Can anybody suggest me how to draw a ring using canvas methods. I may draw to circles using canvas.drawCircle() but how should I feel a space between them?

like image 554
teoREtik Avatar asked May 26 '11 12:05

teoREtik


People also ask

Can we draw directly on Canvas in android studio?

The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI).

How do you draw a circle on a Canvas?

To draw arcs or circles, we use the arc() or arcTo() methods. Draws an arc which is centered at (x, y) position with radius r starting at startAngle and ending at endAngle going in the given direction indicated by counterclockwise (defaulting to clockwise).


2 Answers

  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'
like image 142
Olegas Avatar answered Oct 05 '22 23:10

Olegas


In kotlin you can do:

  • define your paint with stroke style whitin the init block
class CustomView(context: Context, attrs: AttributeSet) : View(context, attrs) {

private var ringPaint: Paint
  init {
        ringPaint = Paint()
        ringPaint.color = R.color.RED // Your color here
        ringPaint.style = Paint.Style.STROKE // This is the important line
        ringPaint.strokeWidth = 20f // Your stroke width in pixels
  }

}
  • draw your circle in the onDraw method by using drawCircleFunction (centerX,centerY,radius,paint)
override fun draw(canvas: Canvas?) {
            super.draw(canvas)
            canvas?.drawCircle(width / 2.0f, height / 2.0f, (width - 10) / 2.0f, ringPaint)
        }
like image 20
Nicola Gallazzi Avatar answered Oct 05 '22 23:10

Nicola Gallazzi