Glide-An Image Loading Library for Android

What is Glide?



  • It is a library that is used to load image from the internet.

  • It is fast and efficient image loading framework for Android.

  • It supports displaying video stills, images, and animated GIFs.


Steps to Use Glide:


Add dependency to your build.gradle.
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
}

Add internet permission
<uses-permission android:name="android.permission.INTERNET"/>

Below code is to load an image to the ImageView using Glide.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="eazy.glideexample.MainActivity">



<ImageView
android:layout_width="400dp"
android:layout_height="400dp"
android:id="@+id/imageview"></ImageView>
</RelativeLayout>


Methods used in glide.



  • with(Context context):- We need to pass context which is necessary for many Android API calls.

  • Load Image from url:-

    • load("http://themarshmallowz.890m.com/images/cupcake.jpg")



  • Load Image from res/drawable:-

    • load(R.drawable.IMAGE_NAME)



  • placeholder(R.drawable.PLACEHOLDER_IMAGE):-display placeholder image while original image is being loaded.

  • error(R.drawable.ERROR_IMAGE):-display image when error occurs while loading original image.


MainActivity.java
package eazy.glideexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity {



ImageView imageview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview= (ImageView) findViewById(R.id.imageview);

Glide.with(this)
.load("http://themarshmallowz.890m.com/images/cupcake.jpg")

.placeholder(R.mipmap.ic_launcher).into(imageview);




}
}

 

glidescreenshot1


In our next post, we'll be looking at how to parse a JSON using retrofit with glide to  display the multiple images in gridview CLICK HERE TO REDIRECT.

IF YOU HAVE ANY SUGGESTIONS OR ANY ERRORS WHILE IMPLEMENTING , FEEL FREE TO COMMENT BELOW.

Comments