Glide: Resizing Image loaded from File using SimpleTarget<Bitmap>(resizeWidth, resizeHeight) not working

Created on 10 Mar 2016  路  2Comments  路  Source: bumptech/glide

I have an image stored in the sdcard whose dimension is 300 x 300. I try to load it using the code below and try to resize it but the loaded image has its original size.

File file = new File("sdcard/image.jpg");
Glide.with(this).load(Uri.fromFile(file)).asBitmap()
         .into(new SimpleTarget<Bitmap>(105, 105) {
                 @Override
                 public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        Log.i("MainActivity", "Bitmap Dimen : " + resource.getWidth()); // Excepting 105 but returns 300
                        // Do something with the resized sourceaa
                    }
                });

Am I missing something here or doing something wrong ?

question

Most helpful comment

Sizing the image usually has two phases:

  1. Decoding/Downsampler read image from stream with inSampleSize
  2. Transforming/BitmapTransformation take the Bitmap and match the exact target size

The decoding is always needed and is included in the flow, the default case is to match the target size with the "at least" downsampler, so when it comes to the transformation the image can be downsized more without quality loss (each pixel in the source will match at least 1.0 pixels and at most ~1.999 pixels) this can be controlled by asBitmap().at least|atMost|asIs|decoder(with downsampler)

The transformation and target size is automatic by default, but only when using a ViewTarget. When you load into an ImageView the size of that will be detected even when it has match_parent. Also if there's no explicit transformation there'll be one applied from scaleType. Thus results in a pixel perfect Bitmap for that image, meaning 1 pixel in Bitmap = 1 pixel on screen resulting in the best possible quality with the best memory usage and fast rendering (because there's no pixel mapping needed when drawing the image).

With a SimpleTarget you take on these responsibilities by providing a size on the constructor or via override() or implementing getSize if the sizing info is async-ly available only.

To fix your load add a transformation: .fitCenter|centerCrop(), your current applied transformation is .dontTransform()

All 2 comments

Sizing the image usually has two phases:

  1. Decoding/Downsampler read image from stream with inSampleSize
  2. Transforming/BitmapTransformation take the Bitmap and match the exact target size

The decoding is always needed and is included in the flow, the default case is to match the target size with the "at least" downsampler, so when it comes to the transformation the image can be downsized more without quality loss (each pixel in the source will match at least 1.0 pixels and at most ~1.999 pixels) this can be controlled by asBitmap().at least|atMost|asIs|decoder(with downsampler)

The transformation and target size is automatic by default, but only when using a ViewTarget. When you load into an ImageView the size of that will be detected even when it has match_parent. Also if there's no explicit transformation there'll be one applied from scaleType. Thus results in a pixel perfect Bitmap for that image, meaning 1 pixel in Bitmap = 1 pixel on screen resulting in the best possible quality with the best memory usage and fast rendering (because there's no pixel mapping needed when drawing the image).

With a SimpleTarget you take on these responsibilities by providing a size on the constructor or via override() or implementing getSize if the sizing info is async-ly available only.

To fix your load add a transformation: .fitCenter|centerCrop(), your current applied transformation is .dontTransform()

Sweet. This is exactly what I was looking for.

I used .fitCenter() as you suggested and I was able to get the expected result.

Thank you reply.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PatrickMA picture PatrickMA  路  3Comments

kenneth2008 picture kenneth2008  路  3Comments

FooBarBacon picture FooBarBacon  路  3Comments

r4m1n picture r4m1n  路  3Comments

sergeyfitis picture sergeyfitis  路  3Comments