Incorrect background on Lollipop for a GIF displayed asBitmap()
Let's say I want to load a static version of this GIF into an ImageView:

I am currently using this:
Glide.with(getContext())
.load(image.getUrl())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(R.drawable.ic_placeholder)
.into(imageView);
As a result, the image is properly loaded on a device / emulator pre-lollipop (with transparent background) but it will be drawn with a white background on Lollipop.
Some other gifs will even be displayed with a purple, blue or black background. Like this one:

note: If i load them .asGif() the background will be transparent as expected
Is there any workaround to get a transparent background on Lollipop?
There's a couple of potential causes here. Images with black backgrounds may be caused by an incorrect Bitmap configuration, you can test by either switching to ARGB_8888 globally, or by using the format() method.
Another potential problem is that when we re-use Bitmaps, we're somehow failing to clear out the background of the image. You can test this by passing in BitmapPoolAdapter to the GlideBuilder class Glide uses for configuration to temporarily disable Bitmap re-use and see if that fixes the problem.
Finally, it's also possible that Android's BitmapFactory class is decoding some of these images with backgrounds whereas our GifDecoder class is not. asGif() will force the image to be decoded using our GifDecoder, asBitmap() will force the image to be decoded as a single frame using BitmapFactory, and using neither asBitmap() nor asGif() will decode GIFs with more than one frame using our GifDecoder and single frame GIFs with BitmapFactory.
tldr: try forcing ARGB_8888, then try disabling Bitmap reuse via BitmapPoolAdapter, then try just decoding the images with BitmapFactory#decodeStream directly.
If you can let me know what happens after each of those steps and/or post urls to the original images so I can (eventually) get to testing them myself, I'd bet we can make progress. Thanks for the report!
Thank you @sjudd for you help. I tried these different solutions.
- Forcing ARGB_8888 via format() or globally doesn't not help
- Disabling the Bitmap reuse does not seem to help. I also tried to only load a single image, so the library should not need to reuse the bitmap.
I am not sure to understand how can I decode it with BitmapFactory#decodeStream. But I assume the problem comes from the Android BitmapFactory because my problem only happen on Lollipop and works fine on KitKat / Jelly Bean.
Example of urls: White background instead of transparent Blue background instead of transparent
@lynfogeek what he meant is to skip Glide and just manually load the image in onCreate for example:
imageView.setImageBitmap(BitmapFactory.decodeStream(...));
If you have wrong backgrounds this way, then it's a framework/gif file bug.
When you say Lollipop, is it 5.0, 5.0.x or 5.1?
@TWiStErRob thank you. The background is wrong using BitmapFactory#decodeStream on Android Kitkat and Lollipop but it works fine with older versions.
Tested with more versions to be more precise:
- :white_check_mark: 4.1.1
- :white_check_mark: 4.3
- :x: 4.4.4
- :x: 5.0
- :x: 5.1
@lynfogeek It does indeed look like an issue with BitmapFactory. If you can attach a minimal sample app that you used to test those various platform versions, I'll do my best to pass it along.
For now, you can use asGif() as a workaround. If you don't want the images to be animated, you can extend GlideDrawableImageViewTarget, override onResourceReady(), and always call stop() on the drawable after calling super. You could also look in to using a ResourceTranscoder to transform the GifDrawable into a simple Bitmap.
Actually don't worry about the sample app, I'll look in to it.
Internal bug is b/20567643
I'm going to close this for now since I don't think we want to go to the effort (and take the performance hit) of working around the issue.
If it seriously affects you, you can work around the problem yourself using asGif() to force the image to be decoded with Glide's GifDecoder.
i got the same issue in SDK v3.6.0
Try working around it by https://github.com/bumptech/glide/issues/423#issuecomment-96232571 (second paragraph).
I HAVE THE SAME ISSUE. Glide load image,There is a blue background.
@tjych Did you try second paragraph of https://github.com/bumptech/glide/issues/423#issuecomment-96232571?
I load image , not gif , There is a blue background, My English is not good .
If the format is PNG the black background won't be displayed.