Open In App

How to Resize a Bitmap in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

ImageViews are used within the android application to display different types of images. Many times images are displayed within the ImageView using bitmap instead of drawable files. In this article, we will take a look at How to Resize Bitmap in the android application to resize our image to be displayed within our ImageView. 

Note: This Android article covers in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--on below line we are creating
        a text for heading of our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idIVImage"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="4dp"
        android:text="Resize Bitmap in Android"
        android:textAlignment="center"
        android:textColor="@color/purple_200"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--on below line we are creating an image view
         for displaying a bitmap in it-->
    <ImageView
        android:id="@+id/idIVImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
  
</RelativeLayout>


Step 3: Add image to your drawable folder

Copy your image which you want to add and then navigate to app>res>drawable right-click on it and simply click on the paste to add the image to your drawable folder. 

Step 4: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // on the below line we are creating a variable for the image view
    lateinit var imageIV: ImageView
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // on below line we are initializing variables with ids.
        imageIV = findViewById(R.id.idIVImage)
  
        // on below line we are getting bitmap from image in drawable folder
        val bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.android)
  
        // on below line we are creating a scaled bitmap by specifying 
        // height and width for it.
        // on below line we are setting that bitmap to our image view.
        imageIV.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 500, 500, false))
  
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
  
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // on below line we are creating
    // a variable for our image view. 
    private ImageView imageIV;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // on below line we are initializing variables with ids.
        imageIV = findViewById(R.id.idIVImage);
  
        // on below line we are getting bitmap from image in drawable folder
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
  
        // on below line we are creating a scaled bitmap by specifying 
        // height and width for it.
        // on below line we are setting that bitmap to our image view.
        imageIV.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 500, 500, false));
  
    }
}


Now run your application to see the output of it. 

Output:

Output

 



Last Updated : 12 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads