Saturday, July 4, 2020

android kotlin - Canvas draw text wrap

MainActivity.kt

package com.cfsuman.kotlintutorials

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


        // text to draw on canvas
        val multilineText = "Lorem Ipsum is simply dummy text of the" +
                " printing and typesetting industry. Lorem Ipsum has been" +
                " the industry's standard dummy text ever since the 1500s," +
                " when an unknown printer took a galley of type and" +
                " scrambled it to make a type specimen book."


        // draw multiline text on canvas and get bitmap
        val bitmap = drawTextWrap(
            text = multilineText,
            textSize = 80F,
            margin = 75,
            textColor = Color.parseColor("#333399"),
            canvasColor = Color.parseColor("#A2A2D0")
        )


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



// function to draw text wrap
fun drawTextWrap(
    text:String = "Text to draw",
    textSize:Float = 100F, // in pixels
    margin:Int = 50, // in pixels
    textColor:Int = Color.BLACK,
    canvasColor:Int = Color.WHITE
):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 on canvas
    val textPaint = TextPaint().apply {
        isAntiAlias = true
        color = textColor
        this.textSize = textSize
    }


    // static layout configurations
    val width = canvas.width - margin * 2
    val alignment = Layout.Alignment.ALIGN_NORMAL
    val spacingMultiplier = 1F
    val spacingAddition = 0F
    val includePadding = false


    // generate a static layout
    val staticLayout:StaticLayout =
        StaticLayout.Builder.obtain(
            text,0,text.length,textPaint,width)
            .setAlignment(alignment)
            .setLineSpacing(spacingAddition,spacingMultiplier)
            .setIncludePad(includePadding)
            .build()


    // finally, draw the multiline text on canvas
    canvas.save()


    // add margin on canvas to draw text
    canvas.translate(margin.toFloat(),margin.toFloat())
    staticLayout.draw(canvas)
    canvas.restore()

    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>