这是我的代码,用于将已下载的 Base64 字符串显示到 ImageView 中(存在于 ListView 中):
RequestOptions requestOptions = new RequestOptions();
requestOptions.override((int) Converter.pxFromDp(this.context, (float) ICON_SIZE),(int) Converter.pxFromDp(this.context, (float) ICON_SIZE));
requestOptions.dontAnimate();
requestOptions.fitCenter();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
if (!eventItem.getVulnerabilityIcon().isEmpty() ) {
Glide.with(context)
.asBitmap()
.load(Base64.decode(base64String, Base64.DEFAULT))
.apply(requestOptions)
.into(new BitmapImageViewTarget(holder.iconVul) { })
;
} else {
Glide.with(context).clear(holder.iconVul); // tell Glide that it should forget this view
holder.iconVul.setImageResource(R.drawable.defaultIcon); // manually set "unknown" icon
}
当我滚动 ListView 时,一切都很好(向上和向下)。现在,当我单击一个项目时,我有一个突出显示(View view,intposition)方法,该方法仅更改 View 的背景,从而调用 getView(...)。然后它重新绘制所有 View (正常行为)。问题是它从头开始重新绘制图像,就好像根本没有在图像上应用缓存功能一样。效果是每次我单击/点击列表中的某个项目时,图像都会闪烁,我不希望那个效果。
起初我以为这是一个动画问题,然后我发现,要禁用任何动画,我应该添加 dontAnimate()。然后,我浏览了 glide ( com.github.bumptech.glide:glide:4.0.0-RC0 ) 文档并阅读了以下内容:
/**
* From: RequestBuilder Class in com.github.bumptech.glide:glide:4.0.0-RC0
* Returns a request to load the given byte array.
*
* <p> Note - by default loads for bytes are not cached in either the memory or the disk cache.
* </p>
*
* @param model the data to load.
* @see #load(Object)
*/
public RequestBuilder<TranscodeType> load(@Nullable byte[] model) {
return loadGeneric(model).apply(signatureOf(new ObjectKey(UUID.randomUUID().toString()))
.diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true /*skipMemoryCache*/));
}
现在我的问题是,有没有办法强制 Glide 在第一次加载后缓存字节数组图像?
P.S:英语不是我的第一语言,所以请为我糟糕的语法道歉。
最佳答案
更改 Glide 调用的 .into() 部分有效:
RequestOptions requestOptions = new RequestOptions();
requestOptions.override(iconWidth,iconHeight);
requestOptions.dontAnimate();
requestOptions.fitCenter();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
if (!eventItem.getVulnerabilityIcon().isEmpty() ) {
Glide.with(context)
.asBitmap()
.load(Base64.decode(base64String, Base64.DEFAULT))
.apply(requestOptions)
.into(new SimpleTarget<Bitmap>(iconWidth, iconHeight) {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
finalHolder.iconVul.setImageBitmap(resource);
}
});
} else {
Glide.with(context).clear(holder.iconVul); // tell Glide that it should forget this view
holder.iconVul.setImageResource(R.drawable.defaultIcon); // manually set "unknown" icon
}
现在它不再闪烁了。有人可以向我解释一下有什么区别吗? 1-是不是因为我使用了SimpleTarget,所以Glide最终缓存了结果? 2- 是否因为transition变量没有被进一步处理?
请有人照亮我的灯笼。
关于android - Glide 机器人 : How to cache a byteArray image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44310895/