MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.content.Context
import android.graphics.*
import android.os.Bundle
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 textView = findViewById<TextView>(R.id.textView)
val seekBarBrightness = findViewById<SeekBar>(R.id.seekBarBrightness)
val tvBrightness = findViewById<TextView>(R.id.tvBrightness)
val seekBarContrast = findViewById<SeekBar>(R.id.seekBarContrast)
val tvContrast = findViewById<TextView>(R.id.tvContrast)
// get the bitmap from assets folder
val bitmap = assetsToBitmap("flower103.jpg")
bitmap?.apply {
// show original bitmap in first image view
imageView.setImageBitmap(this)
// initial bitmap on second image view
imageView2.setImageBitmap(this)
// change bitmap brightness on seek bar change
seekBarBrightness.setOnSeekBarChangeListener(object:
SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(
seekBar: SeekBar?,
progress: Int,
fromUser: Boolean
) {
val brightness = progress.toFloat()-200
val contrast = seekBarContrast.progress.toFloat()/10F
imageView2.setImageBitmap(
setBrightnessContrast(
brightness=brightness,
contrast = contrast
)
)
tvBrightness.text = "Brightness $brightness"+"F"
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
})
// change bitmap contrast on seek bar change
seekBarContrast.setOnSeekBarChangeListener(object:
SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(
seekBar: SeekBar?,
progress: Int,
fromUser: Boolean
) {
val brightness = seekBarBrightness.progress.toFloat()-200
val contrast = progress.toFloat()/10F
imageView2.setImageBitmap(
setBrightnessContrast(
brightness=brightness,
contrast = contrast
)
)
tvContrast.text = "Contrast $contrast"+"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 change bitmap brightness and contrast
fun Bitmap.setBrightnessContrast(
brightness:Float = 0.0F,
contrast:Float = 1.0F
):Bitmap?{
val bitmap = copy(Bitmap.Config.ARGB_8888,true)
val paint = Paint()
// brightness -200..200, 0 is default
// contrast 0..2, 1 is default
// you may tweak the range
val matrix = ColorMatrix(
floatArrayOf(
contrast, 0f, 0f, 0f, brightness,
0f, contrast, 0f, 0f, brightness,
0f, 0f, contrast, 0f, brightness,
0f, 0f, 0f, 1f, 0f
)
)
val filter = ColorMatrixColorFilter(matrix)
paint.colorFilter = filter
Canvas(bitmap).drawBitmap(this,0f,0f,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"
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="210dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
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:text="Original Bitmap"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="210dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2">
<TextView
android:id="@+id/tvBrightness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brightness"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="@+id/seekBarBrightness"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:max="400"
android:progress="200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvBrightness" />
<TextView
android:id="@+id/tvContrast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Contrast"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/seekBarBrightness" />
<SeekBar
android:id="@+id/seekBarContrast"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:max="20"
android:progress="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvContrast" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - Bitmap sharpen
- android kotlin - Bitmap black and white effect
- android kotlin - Bitmap change saturation
- android kotlin - Bitmap rotate color axis
- android kotlin - Bitmap porter duff color filter
- android kotlin - Bitmap change opacity
- android kotlin - Bitmap set alpha
- android kotlin - Bitmap to base64 string
- android kotlin - Bitmap to byte array
- android kotlin - Bitmap to grayscale
- android kotlin - Bitmap color filter
- android kotlin - Drawable color programmatically
- android kotlin - Circle drawable programmatically
- android kotlin - Create GradientDrawable Programmatically
- android kotlin - ActionBar background color programmatically