Android 缓存位图生成的调色板
我正在编写一个应用程序,它在Android 缓存位图生成的调色板,android,android-glide,palette,Android,Android Glide,Palette,我正在编写一个应用程序,它在RecyclerView中显示图像网格。使用Glide v4加载图像,使用MediaStore中该图像的内容Uri 对于每个图像,使用PaletteBitmapTranscoder生成相应的颜色PaletteBitmapTranscoder,这是ResourceTranscoder的自定义实现,如官方建议: 下面是我如何在我的RecyclerView.Adapter中加载图像: GlideApp.with(fragment).`as`(PaletteBitmap::c
RecyclerView
中显示图像网格。使用Glide v4加载图像,使用MediaStore中该图像的内容Uri
对于每个图像,使用PaletteBitmapTranscoder
生成相应的颜色PaletteBitmapTranscoder
,这是ResourceTranscoder
的自定义实现,如官方建议:
下面是我如何在我的RecyclerView.Adapter
中加载图像:
GlideApp.with(fragment).`as`(PaletteBitmap::class.java)
.load(model.iconUri)
.into(object : ImageViewTarget<PaletteBitmap>(imageView) {
override fun setResource(resource: PaletteBitmap?) {
if (resource != null) {
useThatPalette(resource.palette)
super.view.setImageBitmap(resource.bitmap)
}
}
}
GlideApp.with(fragment)。`as`(PaletteBitmap::class.java)
.荷载(iconUri型)
.into(对象:ImageViewTarget(imageView){
覆盖fun setResource(资源:PaletteBitmap?){
if(资源!=null){
使用该调色板(resource.palete)
super.view.setImageBitmap(resource.bitmap)
}
}
}
这是预期的工作
问题
我读到Glide缓存已加载到磁盘和内存缓存中的图像。由于我正在RecyclerView
中加载大量图像,并为每个图像生成一个颜色调色板
,我希望Glide在缓存中存储调色板
的实例,以及相应的位图
看起来Glide已经将调色板位图
实例放在内存缓存中,但我想将它们也放在磁盘缓存中,这样每个调色板
只为每个位图
生成一次
我听说过Glide的ResourceEncoder
,但不知道如何在我的案例中使用它。
是否可以将转码后的资源存储在Glide的磁盘缓存中,如果可能的话,我该怎么做
提前感谢。您是否使用Glide v4?是的,我目前正在使用Glide 4.6.1。
@GlideModule
class MyGlideModule : AppGlideModule() {
override fun isManifestParsingEnabled() = false
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
// Calculate the color Palette associated with the loaded Bitmap
registry.register(
Bitmap::class.java, PaletteBitmap::class.java,
PaletteBitmapTranscoder(glide.bitmapPool)
)
}
}
GlideApp.with(fragment).`as`(PaletteBitmap::class.java)
.load(model.iconUri)
.into(object : ImageViewTarget<PaletteBitmap>(imageView) {
override fun setResource(resource: PaletteBitmap?) {
if (resource != null) {
useThatPalette(resource.palette)
super.view.setImageBitmap(resource.bitmap)
}
}
}