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.
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)
}
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))
}
}
However, this applies to all Glide calls. How can I make ResourceDecoder
behave like v3: the ability to apply on individual call?
UPDATE:
I'm able to draw out a solution for this one after consulting on Glide Github Issues
here https://github.com/bumptech/glide/issues/3522
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
}
That's it, if you don't want to use GalleryDecoder
, just remove .apply(option(GALLERY_DECODER, true))
from Glide load. Cheers!
I think that you can do with :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With