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?
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).
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).
In kotlin you can do:
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
}
}
override fun draw(canvas: Canvas?) {
super.draw(canvas)
canvas?.drawCircle(width / 2.0f, height / 2.0f, (width - 10) / 2.0f, ringPaint)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With