Glide: load drawable but don't scale placeholder

Fernando picture Fernando · Aug 26, 2015 · Viewed 19k times · Source

Is there a way to use Glide to assign a placeholder but keep this image to its original scale? I have an ImageView with variable size (depending on the incoming image) which I set before calling Glide.with().load().into() and I want to use a placeholder for it but don't want the placeholder to be resized to the size of the ImageView, I want it to keep its original size.

So far I couldn't find a way to do this.

Answer

TWiStErRob picture TWiStErRob · Oct 1, 2015

There's a known Glide issue of placeholders distorting loaded images and vice versa. However I think you are not affected by that.

It sounds like what you want is to show the placeholder drawable with scaleType="center" and you want the loaded image to be scaleType="fitCenter". Here's how you express that in Glide. Add android:scaleType="center" in the XML and use the following Glide load line:

Glide.with(...).load(...).placeholder(R.drawable....).fitCenter().into(imageView);

The trick is that the placeholder is set via setImageDrawable() so the ImageView will just display it as usual, but you tell Glide to use the FitCenter explicitly which will fit the loaded image nicely within the ImageView's laid out size via a Transformation and then set it via setImageDrawable(). Since the fitted image is a perfect fit, center will just draw the image covering the whole area of the view.

You can also use .centerCrop() the same way.

If something is wrong you can try .asBitmap() and .dontAnimate(), they help most of the time in one way or another.