Kotlin Android – Draw Circle Border – Paint, Canvas – Example
Android – Draw Circle Border
In our previous tutorial – Draw Shape on Canvas, we learned how to draw a shape like oval onto the canvas. Circle is a special case of oval.
We can also draw border for circle, and set specific border thickness, border color, etc.
In this tutorial, we shall learn how to draw a border for a circle on Canvas.
Steps to Draw Circle Border in Android
To draw a circle border to Canvas using Paint, follow these steps.
- Initialize a Paint Object
- Set AntiAlias and Dither effects to true, to get a smooth drawing effect of circle.
- Set paint style to STROKE. Geometry and text drawn with this style will be stroked, respecting the stroke-related fields on the paint.
- Set stroke width. Provide a float value.
- Set the color.
- Draw Circle using Canvas and Paint Objects.
Following is a sample screenshot, after we are done drawing a border to a circle on Canvas in Android.
Example – Draw Circle Border in Android
Create an Android Application with Empty Activity and replace the content of layout and Activity files with the following.
Complete code for XML layout file and MainActivity is given below.
activity_main.xml
MainActivity.kt
Run this Android Application in your Android phone or Emulator. You would get a Circle on the screen with specified border properties.
Conclusion
In this Kotlin Android Tutorial – Draw Circle Border, we have learnt to use Paint and Canvas to draw a circle with only border onto a ImageView.
Источник