teoREtik
teoREtik

Reputation: 7916

How to draw a ring using canvas in 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?

Upvotes: 9

Views: 10438

Answers (2)

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8705

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)
        }

Upvotes: 2

Olegas
Olegas

Reputation: 10507

  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'

Upvotes: 23

Related Questions