Saturday, July 4, 2020

android kotlin - Canvas draw multiline text

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.*
import android.os.Bundle
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)


        // lines to draw on canvas
        val lines = listOf<String>(
            "I am first line.",
            "This is second line.",
            "It is another line.",
            "Last line."
        )


        // draw multiline text on canvas and get bitmap
        val bitmap = drawMultilineText(
            lines = lines,
            initialY = 225F,
            textSize = 110F,
            textColor = Color.parseColor("#333399"),
            canvasColor = Color.parseColor("#A2A2D0")
        )


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



// function to draw multiline text on canvas
fun drawMultilineText(
    lines:List<String> = listOf(),
    initialY:Float = 200F,
    textColor:Int = Color.BLACK,
    textSize:Float = 120F,
    canvasColor:Int = Color.WHITE
):Bitmap{
    val bitmap = Bitmap.createBitmap(
        1500,
        750,
        Bitmap.Config.ARGB_8888
    )


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


    // paint to draw the text
    val paint = Paint().apply {
        isAntiAlias = true
        color = textColor

        // paint's text size in pixel units
        this.textSize = textSize

        // set typeface with style
        typeface = Typeface.create(
            Typeface.SANS_SERIF,Typeface.NORMAL)

        // important to align text horizontally center
        textAlign = Paint.Align.CENTER
    }


    // calculate canvas horizontal center
    val x = canvas.width / 2F

    // initial canvas y position to draw text
    var y = initialY


    // finally, draw the lines on canvas center
    lines.forEach {
        canvas.drawText(
            it, // text to draw
            x, // x-coordinate of the origin of the text being drawn
            y, // y-coordinate of the baseline of the text being drawn
            paint // paint used for the text e.g. color, size, style
        )

        // calculate next line y position
        y  += paint.descent() - paint.ascent()
    }

    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>