Drawing circles in android

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.

  1. Initialize a Paint Object
    1. Set AntiAlias and Dither effects to true, to get a smooth drawing effect of circle.
    2. Set paint style to STROKE. Geometry and text drawn with this style will be stroked, respecting the stroke-related fields on the paint.
    3. Set stroke width. Provide a float value.
    4. Set the color.
  2. 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.

Читайте также:  Hisuite для андроид что это

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.

Источник

Оцените статью