Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide: load drawable but don't scale placeholder

Tags:

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.

like image 706
Fernando Avatar asked Aug 26 '15 19:08

Fernando


People also ask

What is Glide placeholder?

Glide allows users to specify three different placeholders that are used under different circumstances: placeholder. error.

What is Bumptech?

GitHub - bumptech/glide: An image loading and caching library for Android focused on smooth scrolling.

How do I change the default picture in Glide Android?

Add a template column in the Glide data editor, containing the image or url of the default picture. Then, add an if/then/else column in the data editor : if “user image” is empty then “default image” else “user image”.


1 Answers

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.

like image 86
TWiStErRob Avatar answered Oct 08 '22 12:10

TWiStErRob