How to Resize a Bitmap in Android?
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
android:id = "@+id/container"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< 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" />
< 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() {
lateinit var imageIV: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imageIV = findViewById(R.id.idIVImage)
val bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.android)
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 {
private ImageView imageIV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageIV = findViewById(R.id.idIVImage);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
imageIV.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 500 , 500 , false ));
}
}
|
Now run your application to see the output of it.
Output:
Last Updated :
12 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...