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)
// get the bitmap from assets
val bitmap = assetsToBitmap("flower103.jpg")
bitmap?.apply {
// set original bitmap to first image view
imageView.setImageBitmap(this)
// set inverted colors bitmap to second image view
invertColors()?.apply {
imageView2.setImageBitmap(this)
}
}
}
}
// extension function to get a 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 invert bitmap colors
fun Bitmap.invertColors(): Bitmap? {
val bitmap = Bitmap.createBitmap(
width,
height,
Bitmap.Config.ARGB_8888
)
val matrixInvert = ColorMatrix().apply {
set(
floatArrayOf(
-1.0f, 0.0f, 0.0f, 0.0f, 255.0f,
0.0f, -1.0f, 0.0f, 0.0f, 255.0f,
0.0f, 0.0f, -1.0f, 0.0f, 255.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f
)
)
}
val paint = Paint()
ColorMatrixColorFilter(matrixInvert).apply {
paint.colorFilter = this
}
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"
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="250dp"
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: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="250dp"
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:text="Inverted Colors Bitmap"
app:layout_constraintEnd_toEndOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
</androidx.constraintlayout.widget.ConstraintLayout>

- android kotlin - ImageView grayscale filter
- android kotlin - Bitmap color filter
- android kotlin - Drawable color programmatically
- android kotlin - Circle drawable programmatically
- android kotlin - Set CardView elevation
- android kotlin - Change checked RadioButton text color
- android kotlin - Create TextView programmatically
- android kotlin - Coroutines with timeout or null
- android kotlin - Coroutines with ViewModel LiveData
- android kotlin - Coroutines download image from url
- android kotlin - Coroutines start undispatched
- android kotlin - Canvas draw point
- android kotlin - Canvas draw arc between two points
- android kotlin - Canvas draw text rotate
- android kotlin - Canvas draw multiline text