9

When I load the image into the ImageView with Glide, it shrinks the image. When I rotate or reopen the same activity it has the right size. I already tried possible duplicate. I am already passing fix size to the ImageView inside the xml and I tried to override the size as well. centerCrop() and fitCenter() has no effect on the shrink. I also tried to debug the image size. When it is shrinked or not it returns same width and height.

View:

<ImageView
      android:id="@+id/thumbnail"
      android:layout_width="@dimen/episode_detail_img_width"
      android:layout_height="@dimen/episode_detail_img_height"
      android:layout_marginTop="@dimen/episode_detail_img_margin_top" />

Glide

Glide.with(getActivity())
            .load(url)
            .placeholder(R.drawable.thumb_placeholder)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(thumbnail);
Community
  • 1
  • 1
parohy
  • 2,070
  • 2
  • 22
  • 38
  • 1
    try to remove this code for now : .placeholder(R.drawable.thumb_placeholder) .centerCrop() .diskCacheStrategy(DiskCacheStrategy.ALL) and pls clear cache memory before run. – Dharvik shah Jul 29 '16 at 12:30
  • When I use placeholder, it shrinks the image. All other options are good. Any idea why would that happen? I sure I have the right resources for every drawable res. – parohy Jul 29 '16 at 12:39
  • 1
    okay gotcha. put placeholder image larger then ur ImageView height and weight. or any color. – Dharvik shah Jul 29 '16 at 12:41
  • 1
    I was using placehodler resource which had 400x200px size. I replaced it with 400x400 placehodler and it fixed the issue. Thank you for you help! – parohy Jul 29 '16 at 12:48

4 Answers4

14

I am glade you got your answer. Just for more description of your problem is :

Glide calculated your ImageView height and width before starting a load of an image but when an image gets load it replace your placeholder drawable. so if your placeholder drawable height or weight is small, Glide will render downloaded bitmap with placeholder's dimension.

So, for no lag in different screen size and layout, I advise using high dimension drawable in a placeholder.

And one more hack is that use color in a placeholder because color has no height or width, so render in the whole area of the view.

Dentor
  • 590
  • 2
  • 15
Dharvik shah
  • 1,969
  • 12
  • 27
1

You have defined placeHolder() method which will be loaded at initial time and when glide loads the image based on the given url then glide replace the Loaded Image with placeHolder image. which will be appear into imageView. this loaded image set into imageView based on these specified method.

like: cropCenter(), fitCenter() etc.

don't use cropCenter() it crops the image from center and fit it into your imageView so image quality will go off.

use fitCenter() method.

<ImageView
            android:id="@+id/conversation_team_photo"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:scaleType="fitCenter"
             />

and I have defined bast way to use glide here best way to use glide.

Community
  • 1
  • 1
Abdul Rizwan
  • 3,904
  • 32
  • 31
1

It can be fix by:

1) add .dontAnimate()

2) find a placeholder that matches the image's ratio

Explanation: https://github.com/bumptech/glide/issues/1295

The first time the image loads the default .crossFade() kicks in and your placeholder aspect ratio is not matching the loaded images' ratio. The second time the image is loaded from memory cache and the animations are skipped in that case, resulting in just loading the image properly. You can disable animations via .dontAnimate(), or force a fade-in animation .animate(R.anim.fade_in), or find a placeholder that matches the image's ratio (i.e. square in your case, because of 512x512).

Jakub S.
  • 5,580
  • 2
  • 42
  • 37
  • Thank you for the snippet from docs. It is usefull :) But we wanted to have the animation in place so the dontAnimate flag would not help uz in this case. This is one of older projects so don’t remember... but the same aspect ratio placeholder was the way in this case :) – parohy Jan 07 '18 at 16:24
0

In comparison to Picasso, Glide is much more efficient memory-wise. Glide automatically limits the size of the image it holds in cache and memory to the ImageView dimensions

Clean Your Project and try this Code :

Glide
  .with(context)
  .load(url)
  .override(400, 200) // resizes the image to these dimensions (in pixel).does not respect aspect ratio
  .into(thumbnail);
blkrt
  • 297
  • 1
  • 6