Tuesday, January 31, 2017

Using glide to show GIF in ImageView

As you know glide is an image loading and caching library for Android focused on smooth scrolling. Glide is a 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. You can use Glide to load GIF image from drawable or URL.

1.build.gradle

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

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#e6e6e6"
    android:gravity="center"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

3.MainActivity.java

Load gif image from drawable :
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = (ImageView) findViewById(R.id.image);
        Glide.with(this).load(R.drawable.loader).asGif().into(imageView);

        /*Or use below code*/
        /*GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
        Glide.with(this).load(R.drawable.loader).into(imageViewTarget);*/
    }
}

Load gif image from Internet :
Glide.with(this)
                .load("https://i.stack.imgur.com/h6viz.gif")
                .crossFade()
                .into(imageView);
**Note**
At the first run, it will take some time to load the image.

1 comments so far


EmoticonEmoticon