Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView refresh with Glide

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?

like image 406
Tomas Avatar asked Mar 30 '15 19:03

Tomas


1 Answers

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;
like image 58
Teovald Avatar answered Oct 19 '22 22:10

Teovald