About Me

My photo
Drum & Bass Producer, Software Developer, Love my Cats

Android : How to Scale a Bitmap

How do you scale a Bitmap in Android?

use this method:

public static Bitmap scaleBitmap(Bitmap photo, float scaleFactor){

System.out.println("before: "+ photo.getHeight() + " * " + photo.getWidth());

int width = photo.getWidth();
int height = photo.getHeight();

float scaleWidth = ((float) photo.getWidth()) * scaleFactor / width;
float scaleHeight = ((float) photo.getHeight()) * scaleFactor / height;

// create a matrix for the manipulation
Matrix matrix = new Matrix();

// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap
Bitmap scaledPhoto = Bitmap.createBitmap(photo, 0, 0, width, height,
matrix, false);

System.out.println("before: "+ scaledPhoto.getHeight() + " * " + scaledPhoto.getWidth());

return scaledPhoto;
}

I hope it helps you when you want to resize a Bitmap in Android