I want to grab the original image inside the ImageView for cropping purposes, glide load:
Glide.with(context)
.load(path)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.thumbnail(Glide.with(context).load(thumbPath))
.override(size[0], size[1]).into(imageView);
And inside ImageView I get the original bitmap:
if (getDrawable() instanceof TransitionDrawable) {
bitmap = ((GlideBitmapDrawable((TransitionDrawable)getDrawable()).getDrawable(0)).getBitmap();
} else {
...
}
The issue I'm facing is that sometimes the original image is inside the second drawable in transition drawable and sometimes in the first one.
What is the best practice to get the the original bitmap inside the ImageView.
Can you please elaborate on what you're trying to achieve?
Did you disable the cache to force a crossfade (and hence TransitionDrawable) always?
I want to get hold of the original bitmap inside my ImageView. My Imageview is an extended ImageView which performs cropping and I do the actual cropping on the original size bitmap.
And no I don't disable cache and I don't expect TransitionDrawable all the time:
public Bitmap getOriginalBitmap() {
Bitmap bitmap = null;
if (getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) getDrawable()).getBitmap();
} else if (getDrawable() instanceof TransitionDrawable) {
bitmap = ((GlideBitmapDrawable) ((TransitionDrawable) getDrawable()).getDrawable(1)).getBitmap();
} else if (getDrawable() instanceof GlideBitmapDrawable) {
bitmap = ((GlideBitmapDrawable) getDrawable()).getBitmap();
} else if(getDrawable() instanceof SquaringDrawable) {
bitmap = ((GlideBitmapDrawable)(getDrawable()).getCurrent()).getBitmap();
}
return bitmap;
}
Sometimes the original image is inside the second drawable in transition drawable and sometimes in the first one.
Sadly I have no idea why this would happen. In the code it's always the second: master and 3.x.
What is the best practice to get the the original bitmap inside the ImageView.
The best practice is to not get it. In general I think you shouldn't expect a Bitmap inside a Drawable, what if the Drawable is a ShapeDrawable? If you rethink your approach and draw the parts of the Drawable onto the canvas in onDraw, like ImageView does, you won't have to worry about casting and stuff; and animations (e.g. GIF) will be handled as well.
Another option is to use a .transform() to custom crop the image, which has benefits like RESULT caching and using stock ImageView. You can find examples in FitCenter and CenterCrop Glide classes.
@eicm Yes generally you don't want to care about they type of Drawable you get. You can still change how the Drawable is actually drawn using the Canvas APIs. Or as Robi mentioned, just use a transformation and let Glide figure out how to deal with the various types.
Let us know if we can help further.
So if I want to save the displayed image drawn with Glide - I shouldn't?
@Zoomagic You can, but you have to make sure that the target stays alive while you're doing it, which may not be trivial. I think it's safer, and probably easier to approach it in a different way, see https://github.com/bumptech/glide/issues/459#issuecomment-99960446 or https://github.com/bumptech/glide/issues/751#issuecomment-157414903
Most helpful comment
I want to get hold of the original bitmap inside my ImageView. My Imageview is an extended ImageView which performs cropping and I do the actual cropping on the original size bitmap.
And no I don't disable cache and I don't expect TransitionDrawable all the time: