Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the AppGlideModule in Xamarin.Android?

Using the Glide library on Xamarin.Android, I was hoping someone could shed some light on how to use the AppGlideModule. According to the documentation, I need to register my custom ModelLoader using the AppGlideModule.

Here is a link to the example in the Glide documentation: http://bumptech.github.io/glide/tut/custom-modelloader.html#writing-the-modelloader

Here is my custom AppGlideModule class:

public class MyCustomGlideModule : AppGlideModule
{
    public override void ApplyOptions(Context context, GlideBuilder builder)
    {
        base.ApplyOptions(context, builder);
    }

    public override void RegisterComponents(Context context, Glide glide, Registry registry)
    {
        registry.Prepend(
            Java.Lang.Class.FromType(typeof(Java.IO.OutputStream)),
            Java.Lang.Class.FromType(typeof(Drawable)),
            new MyCustomImageStreamModelLoaderFactory()
        );
    }
}

I don't know if it's necessary, but if you'd like to see the classes I made for the ModelLoader, please let me know in the comments.

like image 285
hellaandrew Avatar asked Nov 07 '22 12:11

hellaandrew


1 Answers

Old question but here is the trick. You will need to create an Android/Java library that contains an AppGlideModule wrapper. This library contains nothing else and is simply used to generate the GlideApp class. It needs to contain a static instance of your final AppGlideModule. Basically, it will look like this :

@GlideModule
public class XamarinGlideModule extends AppGlideModule {
    public static AppGlideModule InjectedModule;

    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        if(InjectedModule != null) {
            InjectedModule.registerComponents(context, glide, registry);
        }
    }
}

You will then need to wrap this library in an Android binding library. Nothing is worthy of mention in this step, simply drop your built AAR in the binding project, add the matching version of the Glide Nuget and build.

You can then add a reference to that binding library in your app project. In your Android Application class, you will need to setup the InjectedModule static property to inject your Xamarin implementation. You must do this before any call to Glide, something similar to this :

public override void OnCreate()
{
    base.OnCreate();

    XamarinGlideModule.InjectedModule = new MyLoaderModule();
    var temp = GlideApp.Get(this); // Init Glide, it will register your Xamarin module
}
like image 149
Sylvain Gravel Avatar answered Nov 14 '22 22:11

Sylvain Gravel