滑行 4 - 特定呼叫上的资源解码器



在浏览了所有 Glide 文档和 StackOverflow 问题和答案后,我找不到有关在版本 4 中为单个 Glide 调用应用资源解码器的任何信息。

在 Glide 3 版本中,我们可以这样做:

Glide.with(imagePreview.context)
          .load(mediaItem.path)
          .asBitmap()
          .decoder(decoderWithDownSampleAtMost(imagePreview.context))
          .diskCacheStrategy(DiskCacheStrategy.NONE)
          .skipMemoryCache(false)
          .dontAnimate()
          .into(target)
private fun decoderWithDownSampleAtMost(ctx: Context): GifBitmapWrapperResourceDecoder {
    return GifBitmapWrapperResourceDecoder(
        ImageVideoBitmapDecoder(StreamBitmapDecoder(Downsampler.AT_MOST,
            Glide.get(ctx).bitmapPool,
            DecodeFormat.DEFAULT),
            FileDescriptorBitmapDecoder(ctx)),
        GifResourceDecoder(ctx),
        Glide.get(ctx).bitmapPool)
  }

在版本 4 中,我知道我们可以将AppGlideModule用于自定义ResourceDecoder

@GlideModule
class MyAppGlideModule : AppGlideModule() {
    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry.prepend(String::class.java, Bitmap::class.java, GalleryDecoder(context))
    }
}

但是,这适用于所有 Glide 调用。如何使ResourceDecoder的行为类似于 v3:在单个调用上应用的能力?


更新:在这里咨询Glide Github Issues后,我能够为此制定解决方案 https://github.com/bumptech/glide/issues/3522

所以基本上,我需要创建一个自定义Option并使用它来确定我的自定义ResourceDecoder是否会被触发。这是我的示例:

  • 正常AppGlideModule
@GlideModule
class MyAppGlideModule : AppGlideModule() {
    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry.prepend(Any::class.java, Bitmap::class.java, MainActivity.GalleryDecoder(context, glide.bitmapPool))
    }
}
  • 在我的Activity
class MainActivity : AppCompatActivity() {
    companion object {
        val GALLERY_DECODER: Option<Boolean> = Option.memory("abc")
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        GlideApp.with(this)
            .asBitmap()
            .load("link_or_path_here")
            .apply(option(GALLERY_DECODER, true))
            .into(image_view)
    }
}
  • 我的GalleryDecoder
open class GalleryDecoder(
        private val context: Context,
        private val bitmapPool: BitmapPool
    ) : ResourceDecoder<Any, Bitmap> {
        override fun decode(source: Any, width: Int, height: Int, options: Options): Resource<Bitmap>? {
            return BitmapResource.obtain(BitmapFactory.decodeResource(context.resources, R.drawable.giphy), bitmapPool)
        }
        override fun handles(source: Any, options: Options): Boolean = options.get(GALLERY_DECODER) ?: false
    }

就是这样,如果您不想使用 GalleryDecoder ,只需从 Glide 负载中删除.apply(option(GALLERY_DECODER, true))即可。干杯!

我认为你可以用:

GlideApp.with(mContext)
        // TRY With below line....
        .setDefaultRequestOptions(new GlideOptions().decode(Class<T> class))
        .load(R.drawable.ic_loader)
        .into(imageView);

我认为你必须传递你的类对象。我没有尝试过,但我认为它会起作用。

相关内容

最新更新