Sunday, July 5, 2020

android kotlin - Canvas draw text with border

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 with border on canvas and get bitmap
        val bitmap = drawBorderedText(
            text = "ANDROID",
            textSize = 300F,
            strokeWidth = 30F,
            textFillColor = Color.parseColor("#F0FFFF"),
            textBorderColor = Color.parseColor("#333399"),
            canvasColor = Color.parseColor("#A2A2D0")
        )


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



// function to draw bordered text on canvas
fun drawBorderedText(
    text: String = "Text To Draw",
    textSize: Float = 200F,
    strokeWidth: Float = 25F,
    textFillColor: Int = Color.YELLOW,
    textBorderColor: Int = Color.RED,
    canvasColor: Int = Color.LTGRAY
):Bitmap?{
    val bitmap = Bitmap.createBitmap(
        1500,
        850,
        Bitmap.Config.ARGB_8888
    )


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


    // text paint to draw text border
    val paintBorder = TextPaint().apply {
        isAntiAlias = true
        color = textBorderColor
        style = Paint.Style.STROKE
        this.strokeWidth = strokeWidth
        this.textSize = textSize
        textAlign = Paint.Align.CENTER
    }


    // calculate text y offset
    val textHeight = paintBorder.descent() - paintBorder.ascent()
    val textOffset = textHeight / 2 - paintBorder.descent()


    // calculate canvas x and y to center text
    val x = canvas.width / 2F
    val y = canvas.height / 2F + textOffset


    // draw text border on canvas center
    canvas.drawText(text, x, y, paintBorder)


    // text paint to draw text fill on canvas
    val paintFill = TextPaint().apply {
        isAntiAlias = true
        color = textFillColor
        style = Paint.Style.FILL
        this.textSize = textSize
        textAlign = Paint.Align.CENTER
    }


    // draw text fill on canvas center
    canvas.drawText(text, x, y, paintFill)

    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>