android kotlin - Canvas draw path

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)


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



// function to draw path on canvas
fun drawPath():Bitmap?{
    val bitmap = Bitmap.createBitmap(
        1500,
        850,
        Bitmap.Config.ARGB_8888
    )


    // canvas to draw path
    val canvas = Canvas(bitmap).apply {
        drawColor(Color.parseColor("#A2A2D0"))
    }


    // paint to draw path fill color
    val paintFill = Paint().apply {
        isAntiAlias = true
        color = Color.parseColor("#333399")
        style = Paint.Style.FILL
    }


    // create a path
    val path = Path().apply {
        fillType = Path.FillType.EVEN_ODD

        // margin in pixels
        val margin = 100

        // pencil head will draw the path
        // move pencil head to canvas top horizontal center
        val topHorizontalCenter = Point(canvas.width/2, margin)
        moveTo(topHorizontalCenter.x.toFloat(),
            topHorizontalCenter.y.toFloat())

        // move pencil head to canvas left vertical center
        val leftVerticalCenter = Point(margin,canvas.height/2)
        lineTo(leftVerticalCenter.x.toFloat(),
            leftVerticalCenter.y.toFloat())

        // move pencil head to canvas bottom horizontal center
        val bottomHorizontalCenter = Point(canvas.width/2,
            canvas.height - margin)
        lineTo(bottomHorizontalCenter.x.toFloat(),
            bottomHorizontalCenter.y.toFloat())

        // move pencil head to canvas right vertical center
        val rightVerticalCenter = Point(canvas.width
                - margin, canvas.height/2)
        lineTo(rightVerticalCenter.x.toFloat(),
            rightVerticalCenter.y.toFloat())

        close()
    }


    // finally, draw the path fill color on canvas
    canvas.drawPath(path, paintFill)


    // paint to draw path border
    val paintStroke = Paint().apply {
        isAntiAlias = true
        color = Color.parseColor("#F0FFFF")
        strokeWidth = 20F
        style = Paint.Style.STROKE
        strokeCap = Paint.Cap.ROUND
    }


    // draw path border on canvas
    canvas.drawPath(path, paintStroke)

    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