Wednesday, February 19, 2020

android kotlin ktx - Scale bitmap keep aspect ratio example

MainActivity.kt

package com.example.jetpack

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.scale
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.math.roundToInt


class MainActivity : AppCompatActivity() {

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

        // image source
        //https://pixabay.com/photos/flower-flowers-quince-bush-spring-4853422/

        // get bitmap from drawable resource
        val bitmap = BitmapFactory.decodeResource(resources,R.drawable.flower)

        button.setOnClickListener {
            imageView2.setImageBitmap(bitmap.scaleByWidth(700))
        }

        button2.setOnClickListener {
            imageView2.setImageBitmap(bitmap.scaleByHeight(700))
        }
    }
}


fun Bitmap.scaleByWidth(newWidth:Int):Bitmap{
    val ratio:Float = this.width.toFloat() / this.height.toFloat()
    val newHeight = (newWidth/ratio).roundToInt()
    return this.scale(newWidth,newHeight,false)
}

fun Bitmap.scaleByHeight(newHeight:Int):Bitmap{
    val ratio:Float = this.height.toFloat() / this.width.toFloat()
    val newWidth= (newHeight/ratio).roundToInt()
    return this.scale(newWidth,newHeight,false)
}
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">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="250dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:scaleType="fitCenter"
        android:src="@drawable/flower"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars[4]" />
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        tools:srcCompat="@tools:sample/avatars[13]" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:text="Scale By Width 700px"
        android:backgroundTint="#4B3621"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:text="By Height 700px"
        android:backgroundTint="#4B3621"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies[add]

// android ktx
implementation 'androidx.core:core-ktx:1.2.0'
build.gradle [add]

android {
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
}
More android examples