6

What is the best way of resizing a bitmap?

Using

options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mandy_moore, options);

or

Bitmap resizedbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
John Victor
  • 615
  • 4
  • 9
  • 22

3 Answers3

13

Use this to create sampled image maintaining same aspect ration as orignal image was. This is best way to resize.

options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mandy_moore, options);
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);

This will scale the bitmap in provided w and h. This may distort the bitmap.

Use according to your need.

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
DcodeChef
  • 1,550
  • 13
  • 17
  • @Vincent Mimoun-Prat any idea on how to keep the quality of the bitmap even on reducing the size? – pallavi Jun 24 '15 at 11:35
13

What is the best way of resizing a bitmap?

It depends the flexibility you need:

options.inSampleSize = N; means that you will obtain an image which is N times smaller than the original. Basically, the decoder will read 1 pixel every N pixel.

Use that option if you don't need a particular size for your bitmap but you need to make it smaller in order to use less memory. This is particularly useful for reading big images.

Bitmap.createScaledBitmap on the other hand give you more control: you can indicate precisely the final dimension of the bitmap, ...

The best is to use a combination of both method:

  1. Determine the maximal value of inSampleSize you can use to decode the source image efficiently (such that the decoded image is still bigger than the final size you need)
  2. Use Bitmap.createScaledBitmap to precisely control the resulting size
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
-1

the method createScaledBitmap produce low quality images

take suggestion from this post bitmap image quality

Community
  • 1
  • 1
StarsSky
  • 6,721
  • 6
  • 38
  • 63