1

I would like to resize an image (Bitmap) with a minimum width and height and keep the ratio.

For now I tryed some methods but I didnt get a correct result.

The closest way was with this method:

Bitmap.createScaledBitmap(bitmapToScale, newWidth, newHeight, false);

and there is the result:

croped image

croped image

The image is indeed resized but only the last column and row are dulicated to the end (right and bottom)

Do anyone as a good method to resize an image on Android ?

(PS: I can't use xml scale option cose I have to transform the image after the crop and neither Glide or picasso)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sanders248
  • 51
  • 1
  • 7
  • It's not issue with resize but certain operation over image make it distort at bottom. – QuokMoon Oct 24 '17 at 11:55
  • @pskink no I can't, cose I need to transform my image after the crop – Sanders248 Oct 24 '17 at 13:41
  • it's an imageview with a fitCenter scaletype, the crop is done programatically. The code I use: Bitmap.createScaledBitmap(bitmapToScale, width * scaling, height * scaling, true); then : Bitmap.createBitmap(scaledBitmap, 0, 0, newWidth, newHeight); – Sanders248 Oct 24 '17 at 14:47

1 Answers1

2

Bitmap.createScaledBitmap is the correct way to scale a bitmap. The result you are getting might be the result of improper implementation while displaying it. Such result usually occurs if you are using BitmapShader.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • thanks for your answer but I dont use BitmapShader, and the image is displayd with glide 4.0: I only do a Bitmap.createScaledBitmap(bitmapToScale, (int) (bitmapToScale.getWidth() * scaling), (int) (bitmapToScale.getHeight() * scaling), true); for scaling, then : Bitmap.createBitmap(scaledBitmap, 0, 0, (int) newWidth, (int) newHeight); to crop – Sanders248 Oct 24 '17 at 12:15
  • You are right ! BitmapShader break my croped Image. I didnt look at the good place Thanks :) – Sanders248 Oct 24 '17 at 15:53