android kotlin - Canvas draw arc

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.*
import android.os.Bundle
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.TextView
import kotlin.math.min


class MainActivity : Activity() {
    lateinit var imageView: ImageView
    lateinit var checkBox: CheckBox
    lateinit var seekBarStartAngle: SeekBar
    lateinit var seekBarSweepAngle: SeekBar
    lateinit var textView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        imageView = findViewById(R.id.imageView)
        checkBox = findViewById(R.id.checkBox)
        seekBarStartAngle = findViewById(R.id.seekBarStartAngle)
        seekBarSweepAngle = findViewById(R.id.seekBarSweepAngle)
        textView = findViewById(R.id.textView)


        // show arc drawing on image view
        imageView.setImageBitmap(drawArc())


        textView.text = "Start Angle: " +
                "${seekBarStartAngle.progress} | " +
                "Sweep Angle: ${seekBarSweepAngle.progress}"


        checkBox.setOnCheckedChangeListener {
                buttonView, isChecked ->
            imageView.setImageBitmap(
                drawArc(
                    checkBox.isChecked,
                    seekBarStartAngle.progress.toFloat(),
                    seekBarSweepAngle.progress.toFloat()
                )
            )
            textView.text = "Start Angle:" +
                    " ${seekBarStartAngle.progress} | " +
                    "Sweep Angle: ${seekBarSweepAngle.progress}"
        }


        seekBarStartAngle.setOnSeekBarChangeListener(
            object:SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?, progress: Int,
                    fromUser: Boolean) {
                    imageView.setImageBitmap(
                        drawArc(
                            checkBox.isChecked,
                            progress.toFloat(),
                            seekBarSweepAngle.progress.toFloat()
                        )
                    )
                    textView.text = "Start Angle:" +
                            " ${seekBarStartAngle.progress} | " +
                            "Sweep Angle: ${seekBarSweepAngle.progress}"
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })


        seekBarSweepAngle.setOnSeekBarChangeListener(
            object:SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?, progress: Int,
                    fromUser: Boolean) {
                    imageView.setImageBitmap(
                        drawArc(
                            checkBox.isChecked,
                            seekBarStartAngle.progress.toFloat(),
                            progress.toFloat()
                        )
                    )
                    textView.text = "Start Angle: " +
                            "${seekBarStartAngle.progress} | " +
                            "Sweep Angle: ${seekBarSweepAngle.progress}"
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })
    }
}



// function to draw arc on canvas
fun drawArc(
    useCenter : Boolean = true,
    startAngle : Float = 0F,
    sweepAngle : Float = 90F
):Bitmap?{
    val bitmap = Bitmap.createBitmap(
        1500,

        850,
        Bitmap.Config.ARGB_8888
    )


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


    val paint = Paint().apply {
        isAntiAlias = true
        color = Color.parseColor("#333399")
    }


    // rectF configurations
    val margin = 25F
    val diameter = min(canvas.width,
        canvas.height) - (margin * 2)
    val xOffset = (canvas.width - diameter) / 2f
    val yOffset = (canvas.height - diameter) / 2F


    // create a rectF as circular shape
    val rectF = RectF(
        xOffset, // left
        yOffset, // top
        diameter + xOffset, // right
        diameter + yOffset // bottom
    )


    // finally, draw the arc on canvas
    canvas.drawArc(
        // bounds of oval used to define
        // the shape and size of the arc
        rectF,
        // Starting angle (in degrees) where the arc begins
        startAngle,
        // Sweep angle (in degrees) measured clockwise
        sweepAngle,
        //  If true, include the center of the oval in the arc,
        //  and close it if it is being stroked
        useCenter,
        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" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:checked="true"
        android:text="Use Center?"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <SeekBar
        android:id="@+id/seekBarStartAngle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:max="360"
        android:progress="0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox" />

    <SeekBar
        android:id="@+id/seekBarSweepAngle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:max="360"
        android:progress="90"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seekBarStartAngle" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials