First of all, thanks alot for creating such a great library. I am loving its performance. But I am noticing a strange behaviour when trying to retrieve bitmap from the drawable. Look at the function below named getBitmap()
In most of the cases, getDrawable returns instance of GlideBitmapDrawable but in some cases it returns instance of SquaringDrawable. Is there a way to retrieve bitmap from it? And why does it return different instance of Drawable for same function call? Am I missing something?
public class BaseImageView extends ImageView {
public interface Listener {
void onImageLoaded();
}
public BaseImageView(Context context) {
super(context);
}
public BaseImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BaseImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
Bitmap image = null;
public Bitmap getBitmap() {
if (image != null) return image;
Drawable drawable = this.getDrawable();
if (drawable != null) {
if (drawable instanceof GlideBitmapDrawable) {
GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) this.getDrawable();
image = bitmapDrawable.getBitmap();
} else if (drawable instanceof SquaringDrawable) {
//TODO dont know what to do here
return null;
} else {
BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();
image = bitmapDrawable.getBitmap();
}
}
return image;
}
public void setImageUrl(String url) {
Glide.with(getContext()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(this);
Picasso.with(this.getContext()).load(url).into(this);
// Glide.with(getContext()).load(url).into(this);
}
}
I was calling getBitmap from onDraw() function which gets repeatedly called. Since the bitmap would only be available once the image is downloaded and loaded in imageview, any calls to get bitmap before that led to getting one of several (random) Drawable instances eg. GlideBitmapDrawable, TransitionDrawable, BitmapDrawable, etc. So, I simply returned null if bitmapDrawable is not instance of BitmapDrawable or GlideBitmapDrawable.
public Bitmap getBitmap() {
if (image != null) return image;
Drawable drawable = this.getDrawable();
if (drawable != null) {
if (drawable instanceof GlideBitmapDrawable) {
GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) this.getDrawable();
image = bitmapDrawable.getBitmap();
} else if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();
image = bitmapDrawable.getBitmap();
} else return null;
}
return image;
}
@dipendrapkrl Thank you for the solution!
I improved and made it shorter a bit so the method can be directly called on ImageView.
public Bitmap getBitmap() {
Drawable drawable = this.getDrawable();
if (drawable instanceof GlideBitmapDrawable) {
return ((GlideBitmapDrawable) this.getDrawable()).getBitmap();
} else if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) this.getDrawable()).getBitmap();
}
return null;
}
Just don't forget to put asBitmap() in Glide.with(imageView.getContext()).load(url).asBitmap().into(imageView);
Most helpful comment
@dipendrapkrl Thank you for the solution!
I improved and made it shorter a bit so the method can be directly called on
ImageView.Just don't forget to put
asBitmap()inGlide.with(imageView.getContext()).load(url).asBitmap().into(imageView);