AndroidAndroid UI

Android Tutorial – Glide V4 Implementation

Glide is a widely popular, fast and memory efficient image loading library for Android. Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs.

In the context of image loading also, there are many popular third party libraries for loading and Glide is the most popular among them.

The latest version of Glide library is Glide v4 and it requires API 14 or higher. 4th version is much faster and efficient for image loading and it is highly recommended to use the latest version.

Here i will explain about a sample implementation of Glide v4 library by loading an image from live URL.

Android SDK Requirements

  • Minimum SDK Version – 14 or higher.
  • Compile SDK Version – 26 or higher.

Add Internet permission In Manifest

Add Internet Permission.

<!-- Internet Permission --> <uses-permission android:name="android.permission.INTERNET" />

AndroidManifest.xml

Add Dependencies Into build.gradle

* Add mavenCentral() repository for Glide.
* Add 2 lines of dependency code.

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
    maven { url 'https://maven.google.com' }
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "glidesample.devdeeds.com.glidesampleapplication"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

    //Add following 2 lines of code
    compile 'com.github.bumptech.glide:glide:4.1.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1'

    testCompile 'junit:junit:4.12'
}

Create AppGlideModule Class

As per the official document, The API is only generated when a properly annotated AppGlideModule is found. There can only be one AppGlideModule per application.

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

/**
 * Created by devdeeds.com on 8/9/17.
 */

@GlideModule
public class SampleGlideModule extends AppGlideModule {
}

Add Few Lines To proguard-rules.pro

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
    **[] $VALUES;
    public *;
}

MainActivity Class

Just inflate the ImageView and load the image into it using GlideApp in the onCreate() of the activity.

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

/**
 * Created by devdeeds.com on 8/9/17.
 */

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //The target image
        ImageView imageView = (ImageView) findViewById(R.id.imageView);

        String path = "https://i.imgur.com/GqKwJmS.jpg"; // Replace the url here

        GlideApp.with(this)
                .load(path)
                .centerCrop()
                .transition(DrawableTransitionOptions.withCrossFade()) //Optional
                .skipMemoryCache(true)  //No memory cache
                .diskCacheStrategy(DiskCacheStrategy.NONE)   //No disk cache
                .listener(new RequestListener() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(imageView);
    }
}

Activity XML

Add a ImageView to which image is going to be loaded.

<!--?xml version="1.0" encoding="utf-8"?-->

<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="glidesample.devdeeds.com.glidesampleapplication.MainActivity">


<imageview android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginbottom="0dp" android:layout_marginleft="0dp" android:layout_marginright="0dp" android:layout_margintop="0dp" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" app:srccompat="@mipmap/ic_launcher">
</imageview>
</android.support.constraint.constraintlayout>

Note: If GlideApp is not resolved then rebuild the entire project after completing all the above steps.

Output

device 2017 09 28 202343

About author

Geethu is a teacher by profession, but she likes to research new things and is also interested in writing. Devdeeds is her blog, where she writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

Your email address will not be published. Required fields are marked *