MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.graphics.*
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import kotlin.math.min
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)
val textView = findViewById<TextView>(R.id.textView)
// draw circle on canvas and get bitmap
val bitmap = drawCircle(
color = Color.parseColor("#333399"),
radius = 300f,
canvasBackground = Color.parseColor("#A2A2D0")
)
// show drawing on image view
imageView.setImageBitmap(bitmap)
// about canvas drawing
textView.text = "bitmap (${bitmap.width} px" +
" * ${bitmap.height} px)"
textView.append("\ncircle radius 300 px")
textView.append("\ncircle color #333399")
textView.append("\ncanvas color #A2A2D0")
}
}
// function to draw circle on canvas
fun drawCircle(
color:Int = Color.LTGRAY,
bitmapWidth:Int = 1500,
bitmapHeight:Int = 750,
radius:Float = min(bitmapWidth, bitmapHeight) / 2f,
cx:Float = bitmapWidth / 2f,
cy:Float = bitmapHeight / 2f,
canvasBackground:Int = Color.WHITE
):Bitmap{
val bitmap = Bitmap.createBitmap(
bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888
)
// canvas to draw circle
val canvas = Canvas(bitmap).apply {
drawColor(canvasBackground)
}
// paint to draw on canvas
val paint = Paint().apply {
this.color = color
isAntiAlias = true
}
// draw circle on canvas
canvas.drawCircle(
cx, // x-coordinate of the center of the circle
cy, // y-coordinate of the center of the circle
radius, // radius of the circle
paint // paint used to draw the circle
)
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"
xmlns:tools="http://schemas.android.com/tools"
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" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif"
android:text="Canvas"
android:textAlignment="center"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - ImageView set image from assets
- android kotlin - ImageView tint programmatically
- android kotlin - ImageView set image from Uri
- android kotlin - Get battery temperature programmatically
- android kotlin - Repeat a task periodically
- android kotlin - Get app first install time
- android kotlin - Detect screen orientation change
- android kotlin - Convert dp to pixels programmatically
- android kotlin - RecyclerView horizontal
- android kotlin - GridView item height
- android kotlin - Canvas draw text inside circle
- android kotlin - Canvas draw text wrap
- android kotlin - Canvas draw multiline text
- android kotlin - Canvas center text
- android kotlin - Canvas draw text