android kotlin - Canvas draw text inside circle

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.*
import android.os.Bundle
import android.text.TextPaint
import android.widget.ImageView


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        val imageView = findViewById<ImageView>(R.id.imageView)


        // draw text on canvas inside circle and get bitmap
        val bitmap = drawTextInCircle(
            textToDraw = "Text to draw inside circle",
            textSize = 90F,
            radius = 300F,
            textColor = Color.parseColor("#F0FFFF"),
            circleColor = Color.parseColor("#333399"),
            canvasColor = Color.parseColor("#A2A2D0")
        )


        // show drawing on image view
        imageView.setImageBitmap(bitmap)
    }
}



// function to draw text inside canvas circle
fun drawTextInCircle(
    textToDraw:String = "Text to draw in circle",
    textSize:Float = 100F,
    radius:Float = 350F,
    textColor:Int = Color.BLACK,
    circleColor:Int = Color.GREEN,
    canvasColor:Int = Color.LTGRAY
):Bitmap?{
    val bitmap = Bitmap.createBitmap(
        1500,
        850,
        Bitmap.Config.ARGB_8888
    )


    // canvas to draw circle and text
    val canvas = Canvas(bitmap).apply {
        drawColor(canvasColor)
    }


    // paint to draw circle
    val paint = Paint().apply {
        color = circleColor
    }


    // text paint to draw text
    val textPaint = TextPaint().apply {
        color = textColor
        textAlign = Paint.Align.CENTER
        this.textSize  = textSize
    }


    // calculate the text offset to draw exact circle center
    val textHeight = textPaint.descent() - textPaint.ascent()
    val textOffset = (textHeight / 2) - textPaint.descent()


    // calculate canvas position to draw circle
    val cx = canvas.width / 2F + 250
    val cy = canvas.height / 2F + 35


    // draw circle on canvas
    canvas.drawCircle(
        cx, cy, radius, paint
    )


    // margin to draw text inside circle
    val margin = 100F


    // calculating no of characters can draw inside  circle
    var text = textToDraw
    val numOfChars: Int = textPaint.breakText(
        text,
        true,
        radius * 2 - margin,
        null
    )


    // truncate text to fit inside circle
    if (text.length > numOfChars){
        text = text.substring(0, numOfChars) + ".."
    }


    // finally, draw text inside circle on canvas
    canvas.drawText(
        text,
        cx,
        cy + textOffset,
        textPaint
    )

    return bitmap
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials