Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bikranttripathi opened this issue Mar 10, 2016 · 2 comments
Labels

Comments

@bikranttripathi
Copy link

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 ?

@TWiStErRob
Copy link
Collaborator

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()

@bikranttripathi
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants