How to draw a ring using canvas in android?

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

+5
source share
2 answers
  • You can draw a circle with a thick brush (use setStrokeWidth ).
  • You can draw two circles, one inside the other. One is filled with a β€œring” color, and the other (inner) is filled with a background color screen
+17
source

In kotlin you can do:

  • define your paint with the stroke style in the initialization 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 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) } 
0
source

All Articles