Glide 4 - 特定呼叫的ResourceDecoder

问题描述 投票:8回答:1

在浏览了所有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 load中删除.apply(option(GALLERY_DECODER, true))即可。干杯!

java android android-glide
1个回答
2
投票

我认为你可以做到:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.