在浏览了所有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))
即可。干杯!
我认为你可以做到:
GlideApp.with(mContext)
// TRY With below line....
.setDefaultRequestOptions(new GlideOptions().decode(Class<T> class))
.load(R.drawable.ic_loader)
.into(imageView);
我想你必须传递你的类对象。我没有尝试,但我认为它会起作用。