问题描述
在浏览了所有 Glide 文档和 StackOverflow 问答后,我找不到任何关于在版本 4 中为单个 Glide 调用应用资源解码器的信息.
After going through all Glide documentation and StackOverflow questions and answers, I cannot find any information regarding applying resource decoder for a single Glide call in version 4.
在 Glide 3 版本中,我们可以这样做:
In version Glide 3, we can do this:
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
And in version 4, I know we can use AppGlideModule
for custom 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:能够应用于单个调用?
However, this applies to all Glide calls. How can I make ResourceDecoder
behave like v3: the ability to apply on individual call?
更新:在咨询 Glide Github Issues
此处 https://github.com/bumptech/glide/issues/3522
所以基本上,我需要创建一个自定义的 Option
并使用它来确定是否会触发我的自定义 ResourceDecoder
.这是我的示例:
So basically, I need to create a custom Option
and use it to determine whether my custom ResourceDecoder
will be triggered. Here is my sample:
- 普通
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))
.干杯!
That's it, if you don't want to use GalleryDecoder
, just remove .apply(option(GALLERY_DECODER, true))
from Glide load. Cheers!
推荐答案
我认为你可以这样做:
GlideApp.with(mContext)
// TRY With below line....
.setDefaultRequestOptions(new GlideOptions().decode(Class<T> class))
.load(R.drawable.ic_loader)
.into(imageView);
我认为你必须传递你的类对象.我还没有尝试过,但我认为它会起作用.
I think you have to pass your class object. I haven't tried but I think it will work.
这篇关于Glide 4 - 特定调用的 ResourceDecoder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!