Tomas Tomas - 18 days ago 6x
Android Question

ImageView refresh with Glide

I have one ImageView and one image loaded in it with Glide:

.into(new GlideDrawableImageViewTarget(ImageView) {
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);

and when I want refresh the image, I run this same code again only with new signature. It's working perfectly, but when new loading is started, the visible image is gone immediately.


Is possible keep the image in ImageView and replace it after new image is downloaded?


That's the expected behavior.
Each time you call .load(x), Glide call .clear() on the target and its associated request.
That's how Glide is able to handle its pool of Bitmaps, otherwise it would have no way to know when to recycle a Bitmap.
In order to implement this, you need to switch between two Targets, here is the core idea :

    public <T> void loadNextImage(@NonNull T model,
                                  @NonNull BitmapTransformation... transformations) {
        //noinspection MagicNumber
        int hash = model.hashCode() + 31 * Arrays.hashCode(transformations);
        if (mLastLoadHash == hash) return;
        mLastLoadHash = hash;

Target mCurrentTarget;

private class DiaporamaViewTarget extends ViewTarget<ImageView, Bitmap> {

        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            mLoadedDrawable = new BitmapDrawable(mImageView.getResources(), resource);
           // display the loaded image 
            mCurrentTarget = mPreviousTarget;