Monday, July 6, 2020

android kotlin - Canvas draw triangle

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)


        // draw triangle on canvas
        val bitmap = drawTriangle(
            sideLength = 650F,
            yOffsetTopPoint = 150F,
            xOffsetTopPoint = 750F,
            xOffsetLeftPoint = 200F,
            yOffsetBottomHand = 150F,
            strokeWidth = 20F,
            strokeColor = Color.parseColor("#F0FFFF"),
            fillColor = Color.parseColor("#333399"),
            canvasColor = Color.parseColor("#A2A2D0")
        )


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



// function to draw triangle on canvas
fun drawTriangle(
    sideLength : Float = 600F,
    yOffsetTopPoint : Float = 100F,
    xOffsetTopPoint : Float = 600F,
    xOffsetLeftPoint : Float = 200F,
    yOffsetBottomHand : Float = 150F,
    strokeWidth : Float = 15F,
    strokeColor : Int = Color.BLACK,
    fillColor : Int = Color.CYAN,
    canvasColor : Int = Color.LTGRAY
):Bitmap?{
    val bitmap = Bitmap.createBitmap(
        1500,
        850,
        Bitmap.Config.ARGB_8888
    )


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


    // paint to draw triangle fill color
    val paint = Paint().apply {
        isAntiAlias = true
        color = fillColor
        this.strokeWidth = strokeWidth
        style = Paint.Style.FILL
    }


    // create a path to draw triangle
    val path = Path().apply {
        fillType = Path.FillType.EVEN_ODD
        // draw rectangle on canvas
        moveTo(xOffsetLeftPoint,
            canvas.height - yOffsetBottomHand)
        lineTo(xOffsetTopPoint, yOffsetTopPoint)
        lineTo(xOffsetTopPoint  + sideLength,
            canvas.height - yOffsetBottomHand)
        close()
    }


    // draw path on canvas
    // it will draw triangle fill color
    canvas.drawPath(path, paint)


    // change paint color to draw triangle border
    paint.apply {
        color = strokeColor
        style = Paint.Style.STROKE
    }

    // it will draw triangle border on canvas
    canvas.drawPath(path,paint)

    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>