Friday, July 3, 2020

android kotlin - Canvas draw text

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.*
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView


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)
        val textView = findViewById<TextView>(R.id.textView)


        // draw text on canvas and get bitmap
        val bitmap = drawText(
            text = "Android",
            textSize = 250F,
            textColor = Color.parseColor("#333399"),
            canvasColor = Color.parseColor("#A2A2D0"),
            typeface = Typeface.SANS_SERIF,
            style = Typeface.BOLD_ITALIC,
            isUnderline = true
        )


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


        // about canvas drawing
        textView.text = "typeface : SANS_SERIF"
        textView.append("\nstyle : BOLD_ITALIC")
        textView.append("\ntext color #333399")
        textView.append("\ncanvas color #A2A2D0")
        textView.append("\ntext size : 250px")
        textView.append("\nisUnderline : true")
    }
}



// function to draw text on canvas
fun drawText(
    text:String = "Text To Draw",
    textColor:Int = Color.BLACK,
    textSize:Float = 200F,
    typeface: Typeface = Typeface.SERIF,
    style:Int = Typeface.NORMAL,
    isUnderline:Boolean = false,
    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

        this.typeface = typeface

        // set typeface with style
        setTypeface(Typeface.create(typeface,style))

        // to underline text
        if (isUnderline){flags = Paint.UNDERLINE_TEXT_FLAG}
    }


    // finally, draw the text on canvas
    canvas.drawText(
        text, // text to draw
        // x-coordinate of the origin of the text being drawn
        100F,
        // y-coordinate of the baseline of the text being drawn
        canvas.height/2F,
        paint // paint used for the text e.g. color, size, style
    )

    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" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:fontFamily="sans-serif"
        android:text="Canvas"
        android:textAlignment="center"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

</androidx.constraintlayout.widget.ConstraintLayout>