Glide — Customize Glide with Modules

Last week, we've looked at how you can set various network stacks for Glide to load your images. Internally, the integration libraries for the network stacks are nothing else than declaring a GlideModule, which is a way of significantly customize Glide's behavior. In this blog post, we'll give you an overview over GlideModules.


Glide Series Overview

  1. Getting Started
  2. Advanced Loading
  3. ListAdapter (ListView, GridView)
  4. Placeholders & Fade Animations
  5. Image Resizing & Scaling
  6. Displaying Gifs & Videos
  7. Caching Basics
  8. Request Priorities
  9. Thumbnails
  10. Callbacks: SimpleTarget and ViewTarget for Custom View Classes
  11. Loading Images into Notifications and AppWidgets
  12. Exceptions: Debugging and Error Handling
  13. Custom Transformations
  14. Custom Animations with animate()
  15. Integrating Networking Stacks
  16. Customize Glide with Modules
  17. Glide Module Example: Accepting Self-Signed HTTPS Certificates
  18. Glide Module Example: Customize Caching
  19. Glide Module Example: Optimizing By Loading Images In Custom Sizes
  20. Dynamically Use Model Loaders
  21. How to Rotate Images
  22. Series Roundup
  23. Advanced ListViews With Images
  24. App Release Preparation
  25. How to Choose the Best Caching Preference
  26. How to Ignore URL Query Parameters In Cache Lookup (soon)
  27. Network-Dependent Image Loading (soon)

Glide Modules

Glide modules are an abstract way of globally changing the way Glide behaves. If you need access to the GlideBuilder, which creates the Glide instance you're working with, this is the way to go. In order to customize Glide, you'll need to implement a public class against the GlideModule interface:

public class SimpleGlideModule implements GlideModule {  
    @Override public void applyOptions(Context context, GlideBuilder builder) {
        // todo
    }

    @Override public void registerComponents(Context context, Glide glide) {
        // todo
    }
}

The interface offers you two methods to adjust different components of Glide. In this blog post, we'll mostly look at the first method: applyOptions(Context context, GlideBuilder builder).

So you know you'll have to create an extra class to customize Glide. Next up is to declare this class globally, so Glide knows that it should load and use it. Glide will scan the AndroidManifest.xml for meta declarations of Glide modules. Thus, you've to declare the just created Glide module in the AndroidManifest.xml within the <application> tag:

<manifest

    ...

    <application>

        <meta-data
            android:name="io.futurestud.tutorials.glide.glidemodule.SimpleGlideModule"
            android:value="GlideModule" />

        ...

    </application>
</manifest>  

Make sure that you change the android:name property to your package name + class name, so it's referenced correctly. That's it! You won't need to add another line to your code. If you want to deactivate the Glide Module, just remove it from the AndroidManifest.xml. The Java class can stay for later use. It won't be loaded or used when it's not referenced in the AndroidManifest.xml.

The way Glide expects you to implement the customization modules has an advantage: you can declare multiple Glide modules at the same time. Glide will go (in no particular order) through all the declared modules. Since you currently cannot define the order, make sure that the customizations don't cause conflicts!

GlideBuilder

Alright, you're aware of how to customize Glide using the Glide modules. Now let's look at the first method of the interface: applyOptions(Context context, GlideBuilder builder). The method gives you the GlideBuilder object as a variable. The method also has void as a return type, so all you can do in this method is call the available methods on the GlideBuilder:

  • .setMemoryCache(MemoryCache memoryCache)
  • .setBitmapPool(BitmapPool bitmapPool)
  • .setDiskCache(DiskCache.Factory diskCacheFactory)
  • .setDiskCacheService(ExecutorService service)
  • .setResizeService(ExecutorService service)
  • .setDecodeFormat(DecodeFormat decodeFormat)

As you can see, the GlideBuilder object gives you access to significant core components of Glide. Using the approach presented in this blog post, you can change the disk cache, memory cache and more!

We'll look at more advanced components later, but for now we pick the easiest change: .setDecodeFormat(DecodeFormat decodeFormat).

Example Usage: Increase Glide's Image Quality

There are two main approaches to decode images on Android: ARGB8888 and RGB565. The first uses 4 bytes for each pixel, the latter only two bytes for each pixel. ARG8888 has the advantage of a higher image quality and the ability to store an alpha channel. While Picasso uses ARGB8888, Glide defaults to the lesser quality of RGB565. The good news for Glide users: you can change the decode format using the Glide module approach!

You simply need to implement a GlideModule, like we've shown you above, and then call builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888) with the correct enum value.

public class SimpleGlideModule implements GlideModule {  
    @Override public void applyOptions(Context context, GlideBuilder builder) {
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
    }

    @Override public void registerComponents(Context context, Glide glide) {
        // nothing to do here
    }
}

If you've followed our steps correctly, Glide will now use the higher image quality decoding. The way to change other aspects of Glide's behavior follow the same pattern. The code in the registerComponents() will look a little bit different, but we'll look at it soon.

Summary

In this blog post, you've learned the basics of Glide modules. You should have a feeling when they can be useful and how you can implement them. If you still need more information, see the resources linked below. Especially if you need more clarification on how to utilize Glide modules in library projects and/or are using ProGuard, make sure to review the content given there.

Since it's a pretty complex topic, we'll go through another practical example next week: how to use a GlideModule to enable access to images behind servers with self-signed HTTPS certificates (which would not work by default) using the registerComponents() method!


Resources

GlideModules on Glide's Github

Explore the Library

Find interesting tutorials and solutions for your problems.

Miscellaneous