Thursday, February 20, 2020

android kotlin ktx - Drawable to bitmap example

MainActivity.kt

package com.example.jetpack

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // Image source
        // https://pixabay.com/photos/anemone-blue-flower-blossom-bloom-2396299/

        // Get drawable from resource
        val drawable:Drawable? = ContextCompat.getDrawable(this,R.drawable.blue_flower)

        // Convert drawable to bitmap
        val bitmap:Bitmap? = drawable?.toBitmap()

        // Convert drawable to bitmap with specific width height
        val bitmap2:Bitmap? = drawable?.toBitmap(
            drawable.intrinsicWidth/25, // width
            drawable.intrinsicHeight/25 // height
        )

        button.setOnClickListener {
            bitmap?.apply { imageView.setImageBitmap(this) }
        }

        button2.setOnClickListener {
            bitmap2?.apply { imageView.setImageBitmap(this) }
        }
    }
}
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"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:backgroundTint="#008000"
        android:text="Drawable To Bitmap"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:backgroundTint="#4B5320"
        android:text="Drawable To Bitmap With Size"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_marginTop="16dp"
        android:scaleType="fitCenter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        tools:srcCompat="@tools:sample/avatars[7]" />
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies[add]

// android ktx
implementation 'androidx.core:core-ktx:1.2.0'
More android examples