Search code examples
androiddrawableanimated-gifandroid-glide

How to load GIF image from drawable folder into ImageView using glide?


I have to show load GIF image from drawable to ImageView using glide.I have tried with following.

Glide.with(LoginActivity.this)
                .load(getResources().getDrawable(R.drawable.bg_gif)).asGif()
                .crossFade()
                .into(relativeLayout);

But isn't seem working and it's working when I'm place image in raw folder.but the problem is I have to use different dimension images.

Any help would be greatly appreciated.


Solution

  • You can try to use Ion it works fine for me. Ion

    Using Ion

     Ion.with(imgView)
    .error(R.drawable.default_image)
    .animateGif(AnimateGifMode.ANIMATE)
    .load("file:///android_asset/animated.gif");
    

    Using Glide

    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
    Glide.with(this).load(R.raw.sample_gif).into(imageViewTarget);
    

    Another Approach

    For Glide 3.0 you need to set asGif() earlier:

    Glide.with(context)
        .load(imageUrl)
        .asGif()
        .placeholder(R.drawable.loading2)
        .crossFade()
        .into(imageView);
    

    Keep in mind that just using load() will load either a GIF or a Bitmap depending on the type of the data. Unless you want your load to fail if the given url is not a gif, you don't need to specify asGif()