MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.renderscript.*
import android.widget.*
import java.io.IOException
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 imageView2 = findViewById<ImageView>(R.id.imageView2)
val textView2 = findViewById<TextView>(R.id.textView2)
val seekBar = findViewById<SeekBar>(R.id.seekBar)
val context = this
// get the bitmap from assets folder
val bitmap = assetsToBitmap("rose.jpg")
val duplicate = assetsToBitmap("rose.jpg")
bitmap?.apply {
// show original bitmap in first image view
imageView.setImageBitmap(duplicate)
// show blurred bitmap on second image view
val initialBlur = seekBar.progress.toFloat() + 1F
textView2.text = "Blur (Radius : ${initialBlur}F)"
imageView2.setImageBitmap(blur(context,initialBlur))
// blur bitmap on seek bar change
seekBar.setOnSeekBarChangeListener(
object:SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(
seekBar: SeekBar?,
progress: Int,
fromUser: Boolean
) {
val radius = progress.toFloat() + 1F
imageView2.setImageBitmap(blur(context,radius))
textView2.text = "Blur (Radius : ${radius}F)"
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
})
}
}
}
// extension function to get bitmap from assets
fun Context.assetsToBitmap(fileName:String):Bitmap?{
return try {
val stream = assets.open(fileName)
BitmapFactory.decodeStream(stream)
} catch (e: IOException) {
e.printStackTrace()
null
}
}
// extension function to blur a bitmap
fun Bitmap.blur(context:Context, radius:Float = 10F):Bitmap?{
val bitmap = copy(config,true)
RenderScript.create(context).apply {
val input = Allocation.createFromBitmap(this,this@blur)
val output = Allocation.createFromBitmap(this,this@blur)
ScriptIntrinsicBlur.create(this,Element.U8_4(this)).apply {
setInput(input)
// Set the radius of the Blur. Supported range 0 < radius <= 25
setRadius(radius)
forEach(output)
output.copyTo(bitmap)
destroy()
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="#DCDCDC"
android:padding="24dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="225dp"
android:layout_marginTop="8dp"
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="8dp"
android:padding="8dp"
android:text="Original Bitmap"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="225dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="Blur"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<SeekBar
android:id="@+id/seekBar"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:max="24"
android:progress="12"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - Draw text on bitmap
- android kotlin - Bitmap color matrix set scale
- android kotlin - Bitmap remove transparency
- android kotlin - Bitmap color balance
- android kotlin - Bitmap change brightness contrast
- android kotlin - Bitmap change saturation
- android kotlin - Bitmap rotate color axis
- android kotlin - Bitmap to base64 string
- android kotlin - Bitmap to byte array
- android kotlin - Bitmap to grayscale
- android kotlin - Convert drawable to bitmap
- android kotlin - Create GradientDrawable Programmatically
- android kotlin - ActionBar background color programmatically
- android kotlin - ActionBar title style
- android kotlin - ActionBar title padding left