In the last tutorial, you've seen how to declare and configure a Glide module, which adds the image size to the request. This is a very powerful optimization. However, it's important to know that declared Glide modules are always active. By default, you cannot dynamically turn them off or on.
In this tutorial, you'll learn how to register a custom model loader on the fly for single requests. However, this is only possible for Glide 3.x. Glide 4.x removed this functionality.
Glide Series Overview
Customized Image Sizes
Hint: If you haven't read our previous tutorial, make sure you do now. Otherwise the next part might be confusing.
As a short review: usually Glide requests are made with the GlideUrl
class. Last week, we've shown you how to create a new interface, which additionally takes the width and height into consideration:
public interface CustomImageSizeModel {
String requestCustomSizeUrl(int width, int height);
}
We created an implementation of it, which builds the passed image URL plus the dimensions to our Future Studio server.
public static class CustomImageSizeModelFutureStudio implements CustomImageSizeModel {
String baseImageUrl;
public CustomImageSizeModelFutureStudio(String baseImageUrl) {
this.baseImageUrl = baseImageUrl;
}
@Override
public String requestCustomSizeUrl(int width, int height) {
return baseImageUrl + "?w=" + width + "&h=" + height;
}
}
Last, but not least, we had to create the CustomImageSizeUrlLoader
, which passes the width and height to our model implementation:
public static class CustomImageSizeUrlLoader extends BaseGlideUrlLoader<CustomImageSizeModel> {
public CustomImageSizeUrlLoader(Context context) {
super( context );
}
@Override
protected String getUrl(CustomImageSizeModel model, int width, int height) {
return model.requestCustomSizeUrl( width, height );
}
}
Dynamic Use of Model Loaders with .using()
So far, we've declared a Glide module with the code above. Glide will use it for every single request. If you don't want that, deactivate your Glide module by removing it from the AndroidManifest.xml
. We can do that because Glide offers the .using()
method to specify a model for a single request:
String baseImageUrl = "https://futurestud.io/images/example.png";
CustomImageSizeModel customImageRequest = new CustomImageSizeModelFutureStudio( baseImageUrl );
Glide
.with( context )
.using( new CustomImageSizeUrlLoader( context ) )
.load( customImageRequest )
.into( imageView1 );
As you can see above, we're creating a CustomImageSizeModelFutureStudio
object for our image in order to load it in its optimal size. Since we've not declared the CustomImageSizeModel
interface in a Glide module, we have to specify it in the line above with .using( new CustomImageSizeUrlLoader( context ) )
. Glide will now use this model for this request only. All other requests, even if they also are made with a CustomImageSizeModel
instance, are not affected.
Outlook
In this tutorial, you've learned how to specify the request model usage for a specific request. If you don't want to or cannot use Glide modules in the AndroidManifest
this is an easy option for you.
With this tutorial, we're finishing our deep dive into Glide modules and their usages.