Glide – Another useful Android library

What Is Glide?

Glide is another fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

Actually Glide is another popular open-source Android library for image loading and animated GIFs. Glide can load and display media from remote servers or the local file system.

Features / Advantages

  • Animated GIF decoding, can load GIF animation into ImageView.
  • Supports fetching, decoding, and displaying video stills & images.
  • Placeholder can be added before loading the image (Can assign default media).
  • Loads thumbnail (blurred) first and then loads the high resolution image like in Facebook.
  • Fast loading big images from internet.
  • UI will not be freeze.
  • Supports OkHttp and Volley library.
  • Good memory and disk caching mechanisms.
  • WebP support
  • Supports image arbitrary transformations like loading image in circular shape or any other shape.
  • No complex code, simply can use

When we comparing the file size of Glide with other image cache libraries, this is bit big.

How to use Glide on Android?

Now we will see how we can use Glide on Android Project.

First create a new Android project. Now we need to add the glide dependency to the project build.gradle.

Don’t forget to add INTERNET permission in AndroidManifest.xml file otherwise remote image won’t load on image view.

Glide is almost ready to use. Now we need to create ImageView to display the image from server.

activity_main.xml

Now we will see how to load the image using the Glide.

MainActivity.java

Here let’s see what Glide is doing:

with(Context context): we begin the load process by first passing our context into the with() method.

load(String string): the image source is specified as either a directory path, a URI, or a URL.

placeholder(int resourceId): a local application resource id, preferably a drawable, that will be a placeholder until the image is loaded and displayed.

into(ImageView imageView): the target image view into which the image will be placed.

Keep in mind, Glide can also load local images. Just pass either the Android resource id to the file path, or a URI as an argument to the load() method.

Now we have seen how to use Glide, now we are going to see what are the advance options available?

Display error image

While loading image, if error occurs, we want to acknowledge the something went. For that can use error() method.

 

Transform image

If you want to transform image, resize it, crop it. you can do it as below.

 

override(width, height) method will resize the image according to the given width and height. centerCrop() or fitCenter() will scale it as per its behavior.

Once an image has been loaded from the internet, Glide will cache it in memory or on disk as per the instruction, caching the image will reduce the internet usage  and permitting faster retrieval of the image. Normally Glide will first check if an image is available in either memory or on the disk before loading it from the network.

 How to disable caching in Glide?

Sometime you want to avoid the caching of image, simply you can do by calling of .skipMemoryCache(true) method. Keep in mind still image will cache on disk, even you can prevent disk cache by calling diskCacheStrategy(DiskCacheStrategy.NONE) method.

  • DiskCacheStrategy.NONE: no data is saved to cache.
  • DiskCacheStrategy.SOURCE: original data saved to cache.
  • DiskCacheStrategy.RESULT: saves the result of the data after transformations to cache.
  • DiskCacheStrategy.ALL: caches both original data and transformed data.

Hope you get idea about Glide and how to use on your Android app. If you have any questions or suggestions, please put your comments below.

Happy codding….

Add Comment