I found a little problem using glide with .listener or a GlideDrawableImageViewTarget when my ImageView has visibility gone or invisible. The listener's callback methods are never called (only onLoadStarted):
Glide.with(mContext)
.load(randomUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(R.drawable.glide_placeholder)
.dontAnimate()
.into(new GlideDrawableImageViewTarget(mImageView) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
super.onResourceReady(resource, animation);
//never called
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
//never called
}
});
What is the size of the ImageView declared in XML? Glide needs to know how big an image the target wants. And does so using a layout listener if the size is not ready yet. Having a hidden ImageView doesn't go through a layout to save processor cycles for obvious reasons, so it may not have a size yet.
You can try .override(w, h) to specify the size yourself or use explicit sizing (100dp).
The image's size was match_parent for both width and height.
Thanks for your response.
Thanks for your question and answer~ It's solve my problem which I want to save a ViewGroup to a Bitmap with ImageView loading by Glide on background task. When I fix the width and height for this ImageView, I can get the listener callback!
We encountered this issue now multiple times.
Shouldn't Glide always call the callback when it has loaded the image whether or not the image view is visible?
@svenjacobs when the view is not visible, the load can't even start, so a completion-callback can never be reached. Glide needs a size before it can progress to loading an image, non-visible views don't have valid sizes.
good!!
Thanks for your response.
Just to clarify, this should only apply to Views set to GONE. Views set to INVISIBLE should still have a valid size, and Glide should be able to properly load images for them. @TWiStErRob, can you confirm this?
@ahuang13 invisible won't work either: https://github.com/bumptech/glide/blob/v4.7.1/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java#L512
Changing alpha from 0 to 1 is workaround. This works fine.
I've encountered the same issue where I wanted to implement this:
ImageView Gone -> Call glide to load the image -> If image Loaded then show the ImageView, if failed stay hidden
The workaround that worked for me was :
imageView.show()
Glide.with(imageView)
.load(url)
.dontAnimate()
.into(object : DrawableImageViewTarget(this) {
override fun onLoadStarted(placeholder: Drawable?) {
super.onLoadStarted(placeholder)
imageView.gone()
}
override fun setResource(resource: Drawable?) {
super.setResource(resource)
resource?.let {
imageView.show()
}
}
})
Most helpful comment
Changing alpha from 0 to 1 is workaround. This works fine.