Image loading and caching library for Android | Set 2

This article is divided into 2 Parts. I am assuming reader has basic knowledge of Android and java.
There are two libraries for caching image in android :

1) Picasso
2) Glide

Glide, like Picasso, can load and display images from many sources, while also taking care of caching and keeping a low memory impact when doing image manipulations. Official Google apps (like the app for Google I/O 2015) are using Glide. In this article, we’re going to explore the functionalities of Glide and why it’s superior in certain aspects.

Introduction

Glide is an Image Loader Library for Android developed by bumptech and is a library that is recommended by Google. It has been used in many Google open source projects including Google I/O 2014 official application. It provides animated GIF support and handles image loading/caching. Animated GIF support is currently not implemented in Picasso.

How to Use Glide Android Library?

1) For using Glide in android project, we have to add dependency in gradle file. So, For adding dependency open app/build.gradle file in the app folder in your Android project and add the following lines inside it.

Note : Add these lines inside dependencies{}

dependencies {
    compile 'com.github.bumptech.glide:glide:3.5.2'
    compile 'com.android.support:support-v4:22.0.0'
}

Glide also needs Android Support Library v4, please don’t forget to import support-v4 to your project like above as well. But it is not kind of a problem since Android Support Library v4 is basically needed in every single new-age Android project
Now sync your gradle once again.If you get any type of error then you may check the error on stackoverflow

2) Now add InternetPermission inside Android.Manifestfile. Open the manifest.xml file and add the following line.

users-permission android:name="android.permission.INTERNET"

3) Open the layout file for the main Activity. We need to add an ImageView to the layout. It doesn’t need to be fancy. The following code snippet shows you what I mean.

ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" 

4) Now Navigate to the main Activity file and Add the following code block to the onCreate method.

ImageView imageView = (ImageView) findViewById(R.id.imageView);
 
        Glide.with(context)
             .load("YOUR IMAGE URL HERE")
             .into(imageView);

In the first line we are getting ImageView instance from the layout. And then load the image from the above remote url using Glide library.

Advanced Usage

For any real time application, we must think of all possible cases. In the above code, we just store the image from the server link into the imageView.There are some more cases.

  • Resize
    Glide.with(context)
        .load("YOUR IMAGE URL HERE")
        .override(300, 200)
        .into(imageView);
    

    Here we are using Glide to fetch a remote image and overriding(resizing) it using before displaying it in an image view.

  • Placeholder
    Glide.with(context)
        .load("YOUR IMAGE URL HERE")
        .placeholder(R.drawable.placeholder)
        .into(imageView);
    

    If your application relies on remote assets, then it’s important to add a fallback in the form of a placeholder image. The placeholder image is shown immediately and replaced by the remote image when Glide has finished fetching it.

  • Handling errors
    Glide.with(context)
        .load("YOUR IMAGE URL HERE")
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.imagenotfound)
        .into(imageView);
    

    We already saw how the placeholder method works, but there’s also an error method that accepts a placeholder image. Glide will try to download the remote image three times and display the error placeholder image if it was unable to fetch the remote asset.

  • GIFS Support
    GlideDrawableImageViewTarget imagePreview = new GlideDrawableImageViewTarget(imageView);
        Glide 
                .with(this) 
                .load(url) 
                .listener(new RequestListener() { 
                    @Override 
                    public boolean onException(Exception e, String model, Target target, 
                                               boolean isFirstResource) {                       
                        return false; 
                    } 
     
                    @Override 
                    public boolean onResourceReady(GlideDrawable resource, 
                                                   String model, Target target, 
                                                   boolean isFromMemoryCache, 
                                                   boolean isFirstResource) {
                        return false; 
                    } 
                }) 
                .into(imagePreview);
    
  • Effects
    Blur image :

    Glide.with(context)
        .load("YOUR IMAGE URL HERE")
        .bitmapTransform(new BlurTransformation(context))
        .into(imageView);
    

    Multiple Transform :

    Glide.with(context)
        .load("YOUR IMAGE URL HERE")
        .bitmapTransform(new BlurTransformation(context, 25), 
                         new CropCircleTransformation(context))
        .into(imageView);
    

    Circle crop :

    Glide.with(context)
        .load("YOUR IMAGE URL HERE")
        .bitmapTransform(new CropCircleTransformation(context))
        .into(imageView);
    

    Rounded Corners :

    int radius = 50; 
    int margin = 20;
    
    Glide.with(context)
        .load("YOUR IMAGE URL HERE")
        .bitmapTransform(new 
          RoundedCornersTransformation(context, radius, margin))
        .into(imageView);
    

Reference :
https://github.com/bumptech/glide



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.