7

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

Glide.with(ImageView.getContext())
    .load(url)
    .dontAnimate()
    .placeholder(R.drawable.placeholder)
    .signature(stringSignature)
    .into(new GlideDrawableImageViewTarget(ImageView) {
        @Override
        public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
            super.onResourceReady(drawable, anim);
            progressBar.setVisibility(View.GONE);
        }
    });

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.

Question

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

Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47
Tomas
  • 4,652
  • 6
  • 31
  • 37

2 Answers2

4

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;
        Glide.with(mContext).load(model).asBitmap().transform(transformations).into(mCurrentTarget);
        mLastLoadHash = hash;
    }

Target mCurrentTarget;



private class DiaporamaViewTarget extends ViewTarget<ImageView, Bitmap> {

        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            mLoadedDrawable = new BitmapDrawable(mImageView.getResources(), resource);
           // display the loaded image 
            mCurrentTarget = mPreviousTarget;
Teovald
  • 4,369
  • 4
  • 26
  • 45
  • 2
    note : IIRC one case is not handled : the onStop / onStart of the widget It causes crashes when the device comes back from sleep. It should be easy to patch though. I plan to write a better version of this class when glide 4 comes out – Teovald Oct 14 '15 at 13:20
  • Hello @Teovald, do you have a idea, how to use this in recyclerview adapter for multiple images? I tried some modifications, but nothing was worked for me :-( – Tomas Nov 28 '15 at 02:40
  • You want to directly use this as a RecyclerView item ? It seems tricky and costly. I guess you should take special care of keeping track of the last loaded image in a cell. btw, I am waiting for the official release of Glide 4 in order to rewrite this adapter a little bit more cleanly. When I wrote this, my min API was 7, I could use more efficient binding logic with minAPI 16. – Teovald Nov 28 '15 at 07:54
  • Not directly, but something similar ;) I agree with your logic, but how to do that? And why Glide need to call clear() before loading and not after, in the time when new bitmap is prepared to attach? – Tomas Nov 28 '15 at 11:47
  • >why Glide need to call clear() before loading and not after, in the time when new bitmap is prepared to attach? The main use case that Glide tries to solve is image loading in lists. It makes sense, this this is by very far the most common use case. In a list, when you bind a viewholder, you always clean the state of the imageView, so there is no point in keeping the bitmap in memory. Another advantage is that it allows you to reuse this exact memory space with inbitmap. – Teovald Nov 30 '15 at 18:42
  • It is up to you to build a target adapter to ViewHolders, this is really not a problem I am trying to solve in that gist. It should be a good starting point though. – Teovald Nov 30 '15 at 18:43
  • @Teovald did you ever finished writing this? I am having same problem with Glide 4. – c0dehunter Aug 05 '20 at 08:48
  • 1
    @PrimožKralj you can look there : https://gist.github.com/FrancoisBlavoet/f8db7faa9ae6328be327. – Teovald Aug 05 '20 at 18:34
  • I wrote this a while ago, probably for Glide 3 at the time. But it should be easy enough to use it as a basis for a glide4/kt impl – Teovald Aug 05 '20 at 18:34
1

You can set loaded Drawable as placeholder in next loading, like this:

    private Drawable placeholder = ContextCompat.getDrawable(ctx, R.drawable.placeholder);

    public void loadImage(String url, ImageView imageView) {
        Glide.with(imageView.getContext())
            .load(url)
            .placeholder(placeholder)
            .into(new GlideDrawableImageViewTarget(imageView) {
                @Override
                public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
                    super.onResourceReady(drawable, anim);
                    placeholder = drawable;
                }
            });
    }
Anrimian
  • 4,257
  • 4
  • 22
  • 30