Glide android: 如何缓存 byteArray 图片?
Glide android: How to cache a byteArray image?
这是我的代码,用于将已下载的 base64 字符串显示到图像视图中(存在于 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
}
当我滚动列表视图时,一切正常(上下)。现在,当我单击一个项目时,我有一个 highlight(View view,int position) 方法,它只更改视图的背景,从而调用 getView(...)。然后重绘所有视图(正常行为)。问题是它从头开始重新绘制图像,就好像在 all.The 的图像上没有应用缓存功能一样,效果是每次我 click/tap 在列表的一个项目上,图像闪烁,我不想要那种效果。
起初我以为是动画问题,后来发现要禁用任何动画,我应该添加 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变量没有进一步处理吗?
谁能给我灯笼点亮
这是我的代码,用于将已下载的 base64 字符串显示到图像视图中(存在于 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
}
当我滚动列表视图时,一切正常(上下)。现在,当我单击一个项目时,我有一个 highlight(View view,int position) 方法,它只更改视图的背景,从而调用 getView(...)。然后重绘所有视图(正常行为)。问题是它从头开始重新绘制图像,就好像在 all.The 的图像上没有应用缓存功能一样,效果是每次我 click/tap 在列表的一个项目上,图像闪烁,我不想要那种效果。
起初我以为是动画问题,后来发现要禁用任何动画,我应该添加 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变量没有进一步处理吗?
谁能给我灯笼点亮