Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 缓存位图生成的调色板_Android_Android Glide_Palette - Fatal编程技术网

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)
            }
        }
}