7

I am using the Glide image loading library and I'm having issues when it comes to resizing bitmaps.

When using the following code:

Glide.with(getActivity())
    .load(images.get(i))
    .asBitmap().centerCrop()
    .into(new SimpleTarget<Bitmap>(1200, 1200) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

        }
    });

Every single bitmap gets resized to the specified dimensions. So, if the image is 400x300, it gets upscaled up to 1200 x 1200 which I do not want. How do I make it so that if the image is smaller than the specified dimensions, it won't resize it?

I'm specifying dimensions because I want every image that's bigger than the specified dimensions to be resized taking into account centerCrop; and then if the image is smaller than the specified dimensions, I don't want it to be resized.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
Jack
  • 2,043
  • 7
  • 40
  • 69
  • The image ScaleType centerCrop will cause your Image to expand from the center so that each side is as big, or bigger than, the boundaries of the View it's laid out in. I'm not a glide user, but I'd assume removing that centerCrop() after asBitmap() -might- help you --Edit: You're also setting the size to be 1200x1200, so you're going to have to change that somehow too – Cruceo Apr 18 '16 at 18:04
  • @Guardanis I've tried that, and it causes it just to completely ignore any dimensions that have been specified. – Jack Apr 18 '16 at 18:06
  • why do you specify the dimensions when you don't want it to be stretched? what are you trying to achieve? – headsvk Apr 24 '16 at 08:58
  • @headsvk I'm specifying dimensions because I want every image that's bigger than the specified dimensions to be resized taking into account centerCrop; and then if the image is smaller than the specified dimensions, I don't want it to be resized. – Jack Apr 24 '16 at 11:36

1 Answers1

7

I want every image that's bigger than the specified dimensions to be resized taking into account centerCrop; and then if the image is smaller than the specified dimensions, I don't want it to be resized.

You can obtain this behaviour with a custom transformation:

public class CustomCenterCrop extends CenterCrop {

    public CustomCenterCrop(BitmapPool bitmapPool) {
        super(bitmapPool);
    }

    public CustomCenterCrop(Context context) {
        super(context);
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        if (toTransform.getHeight() > outHeight || toTransform.getWidth() > outWidth) {
            return super.transform(pool, toTransform, outWidth, outHeight);
        } else {
            return toTransform;
        }
    }
}

and then use it like this:

Glide.with(getActivity())
    .load(images.get(i))
    .asBitmap()
    .transform(new CustomCenterCrop(getActivity()))
    .into(new SimpleTarget<Bitmap>(1200, 1200) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

        }
    });
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94