In the last tutorial, 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 tutorial, we'll give you an overview over GlideModule
s.
Glide Series Overview
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 AppGlideModule
(Glide 3.x: GlideModule
) interface.
The interface offers you two methods to adjust different components of Glide. In this tutorial, we'll mostly look at the first method: applyOptions(Context context, GlideBuilder builder)
.
Glide 4.x
@GlideModule
public class FutureStudioAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
}
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
}
}
Glide 4.x is aware of your configuration because of the @GlideModule
annotation. You can only use one AppGlideModule
!
If you want to do extensive configurations, we recommend splitting up the customization into separate modules. Each of them should get the @GlideModule
annotation and extend the LibraryGlideModule
interface. Glide will use the AppGlideModule
as the base and add all other LibraryGlideModules
to the configuration.
Glide 3.x
public class SimpleGlideModule implements GlideModule {
@Override public void applyOptions(Context context, GlideBuilder builder) {
// todo
}
@Override public void registerComponents(Context context, Glide glide) {
// todo
}
}
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 3.x 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)
As you can see, the GlideBuilder
object gives you access to significant core components of Glide. Using the approach presented in this tutorial, you can change the disk cache, memory cache and more!
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.
Glide 4.x
@GlideModule
public class FutureStudioAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
RequestOptions options = new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888);
builder.setDefaultRequestOptions(options);
}
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
}
}
Glide 3.x
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 tend to use the higher image quality decoding (even though there is no guarantee). 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 tutorial, 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!