我有一个ImageView,其中一个图像加载了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);
}
});
当我想要刷新图像时,我只使用新签名再次运行相同的代码。它工作得很好,但是当新的加载开始时,可见图像立即消失。
问题
是否可以将图像保留在ImageView中并在下载新图像后将其替换?
答案 0 :(得分:3)
这是预期的行为
每次拨打.load(x)
时,请在目标及其相关请求上调用.clear()
这就是Glide如何处理其位图池,否则无法知道何时回收位图。
为了实现这一点,你需要在两个目标之间切换,这是核心思想:
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;
答案 1 :(得分:1)
您可以在下次加载时将加载的Drawable设置为占位符,如下所示:
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;
}
});
}