During your Android developments, it’s almost sure you have already loaded images from the Web for your application. To achieve that, you can use the classic HttpUrlConnection based stack. This solution is functional but you will need some efforts to make fast calls and add some caching features. Better solution is probably to use an Image Loader Library that offers you a complete solution out of the box.

Glide is the solution recommended by Google. Like said on their website, it’s a fast and efficient solution to manage media and load images. Glide wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface. It lets you to fetch, decode and display video, images and animated GIFs. API offered to developers is flexible and allow them to plugin in to almost any network stack. Whereas it uses a custom HttpUrlConnection based stack, Glide includes utility libraries plug in to Google’s Volley Library or Square’s OkHttp library.

Installation

First step is to install Glide for your project. Open your build.gradle file and add the following dependencies :

compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.android.support:support-v4:19.1.0'

Load and image with Glide

First usage of Glide is to load an image from the Web. For example, you can download the S.Saurel’s logo from that url : http://www.ssaurel.com /tmp/logo_ssaurel.png into an image :

ImageView logo = (ImageView) findViewById(R.id.logo); Glide.with(this) .load("http://www.ssaurel.com/tmp/logo_ssaurel.png") .into(logo);

Change the default Bitmap Format

By default, Glide sets default Bitmap Format to RGB_565 which is not the best quality. However, it’s used because it consumes just 50% memory footprint compared to ARGB_8888 which is the best quality format. If default quality is ok for you, you have nothing to change on Glide configuration. But, if you want to use a better quality, you can switch Bitmap Format to ARGB_8888 by creating a new class extending GlideModule like that :

public class GlideConfiguration implements GlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { // Apply options you wish to the Glide Builder builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); } }

Then, you need to define this specific configuration module as meta-data inside your Android Manifest with the following line :

<meta-data android:name="com.ssaurel.myapp.configuration.GlideConfiguration" android:value="GlideModule"/>

Use Glide to load Images on ListView

Sometimes, you have to use image loading for items of a ListView. Glide is also able to achieve that. For example, you can make that on your getView method :

@Override public View getView(int position, View recycled, ViewGroup container) { final ImageView myImageView; if (recycled == null) { myImageView = (ImageView) inflater.inflate(R.layout.my_item, container, false); } else { myImageView = (ImageView) recycled; } String url = listUrls.get(position); Glide.with(context) .load(url) .centerCrop() .placeholder(R.drawable.loading_drawable) .crossFade() .into(myImageView); return myImageView; }

In that code, you have noticed some of the methods offered by Glide like placeholder to display a temporary image waiting targeted image be loaded or like centerCrop to center and crop the image. You can also apply transformations with transform method. The complete documentation of Glide will let you to discover all the features in depth.

Disk caching

One of the biggest advantages to use an image loader library like Glide is the disk caching management that is implemented by default. By default, Glide is going to put cache the image to the exact resolution you fetch. So, you can change that by choosing an other cache strategy by applying diskCacheStrategy method during your call. You can also opt for disk caching or memory caching.

Conclusion

Glide is a powerful and efficient image loader library that you need on your Android project. Compared to others libraries of that kind like Picasso, Glide has the advantage to support GIF Animation and to be designed to work perfectly with Activity and Fragments lifecycle. Thus, animation of a GIF will be automatically paused and resumed along with Activity and Fragment’s state.