Show GIF file with Glide (image loading and caching library)
Asked Answered
C

14

75

I try to show a GIF image as loading placeholder in image view - with Glide Library:

Glide.with(context)
    .load(ImageUrl())
    .placeholder(R.drawable.loading2)
    .asGif()
    .crossFade()
    .into(image);

I try to show this file

loading2.gif

but get this error :

Error:(54, 86) error: cannot find symbol method asGif()

How can I show GIF file with Glide in a imageView?

Cosette answered 26/6, 2015 at 21:20 Comment(1)
Looks like maybe your missing the right version of Glide? Is really strange that does not find the symbol also try to o a clean.Indreetloire
F
124

The above answer didn't work for me. The below code works.

ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(R.raw.sample_gif).into(imageViewTarget);
Fifi answered 19/1, 2016 at 7:26 Comment(6)
I'm not sure why, but this really is what worked for me, while asGif() just made the whole thing disappear.Findley
Can you please explain the purpose of GlideDrawableImageViewTarget ?Sophiesophism
this answer just shows the gif file. it doesn't work like placeholder. how can i use this answer to show gif file when image is loading like placeholder?Ethridge
@Sophiesophism i followed 'Plaid' app's source code by Nick Butcher. I tried everything from Glide doc but none worked.Fifi
@Sophiesophism you can find the source code of the Plaid application here: github.com/nickbutcher/plaidFarkas
DrawableImageViewTarget imageViewTarget = new DrawableImageViewTarget(mProgressBar);Pernell
B
64

For Glide 4.+

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(this).asGif().load(R.raw.image_gif).into(imageView);
Bevins answered 12/6, 2017 at 7:3 Comment(2)
Thank you very much, Glide is so much better than Ion, because Ion keeps lagging my sound probably because Ion takes more memory or whatever but it glitches my sound, also I prefer Glide syntax it feels more reasonable for me glad I found Glide to solve my problem. :)Asterism
I can't able to see the gif ImageView imageView = (ImageView) v.findViewById(R.id.gif); Glide.with(this).asGif().load(R.raw.steam4).into(imageView); code :<ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/gif" >Incorporeity
D
22

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()

Diane answered 30/6, 2015 at 1:14 Comment(0)
P
18

I had this same problem. You can resolve it by moving where the asGif() is located. It must come directly before .load().

So change:

Glide.with(context)
    .load(ImageUrl())
    .placeholder(R.drawable.loading2)
    .asGif()  
    ....

to

Glide.with(context)
     .asGif()         
     .load(ImageUrl())
     .placeholder(R.drawable.loading2)
    ....
Pronunciamento answered 24/1, 2017 at 0:5 Comment(2)
Please specify that your solution can be used with Glide 4+Brander
@marc: thanks it working but error and placeholder is not workingAbert
A
9

Here is another example. I have observed that Glide doesn't play Gif automatically sometimes. This solution worked for me:

In the example, imageView is one of the members in the RecyclerView's ViewHolder class.

Glide.with(itemView.getContext())
    .asGif()
    .load(thumbPath)                             
    .placeholder(ResourcesCompat.getDrawable(context.getResources(), 
                     R.drawable.image_loading_placeholder, null))
    .centerCrop()
    .into(new ImageViewTarget<GifDrawable>(imageView) {
         @Override
         protected void setResource(@Nullable GifDrawable resource) {
             imageView.setImageDrawable(resource);
         }
     });

The method .asGif() needs to be used earlier or it will not be recognized

Abominate answered 6/8, 2019 at 6:17 Comment(1)
Wow. Thannks @Ram lyer.Compunction
D
5

Glide automatically plays animated gif files, no need of extra method

Use below code

Glide.with(this)
   .load(R.drawable.logo)
   .into(imageView);
Depressor answered 6/2, 2017 at 10:43 Comment(2)
You should really add some explanation as to why this should work - you can also add code as well as the comments in the code itself - in its current form, it does not provide any explanation which can help the rest of the community to understand what you did to solve/answer the question. This is especially important for an older question and the questions that already have answers.Creditable
The new glide version simply plays animated gifDepressor
I
4

Use GlideDrawableImageViewTarget. This code works for me.

GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);

Glide.with(this).load(R.drawable.logo_gif).into(imageViewTarget);
Iraqi answered 13/1, 2018 at 17:55 Comment(0)
G
3

From Glide version v4 :

Dependency:

implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

Visit this link To create GlideApp class:

You can directly give drawable reference on GlideApp

GlideApp.with(this).load(R.drawable.ic_loader).into(mBinding.progressBar);

If you get any error, please let me know.

Gallic answered 1/12, 2018 at 11:24 Comment(1)
Hey pratik, would you suggest about resizing gif for saving as file. i tried asGif() and then convert the resource to gif but it simply ignore the new size that it it keeps the original file while it works fine for static image. I have asked the question here #71452866Bayou
T
3

put your gif in raw folder and the use this below code also add the below dependency in your gradle

        //for glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
   ImageView layoutAnim;

 //load gif file to imageView where layoutanim is my imageView

    Glide.with(this).asGif().load(R.raw.backloader).into(layoutAnim);

  
Trivia answered 2/6, 2021 at 10:18 Comment(0)
C
1

I'm using Glide version 4.11 and for me it did work setting .asGif() right after Glide.with(context).

But I had another problem, I've also set .dontAnimate() when loading gif into ImageView. Apparently this doesn't work, so I had to remove .dontAnimate() from my setup, and everything worked after that change.

Here is issue for more info https://github.com/bumptech/glide/issues/3395

Candent answered 3/9, 2020 at 13:13 Comment(0)
G
0

This solution is kind of work around

<RelativeLayout
    android:layout_width="wrap_content"
    android:id="@+id/ImageContainer"
    android:background="#fff"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/loadingImageView"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/mainImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:layout_centerInParent="true"
     />
</RelativeLayout>
  1. instead of using one, use 2 ImageViews

  2. Put loading gif in first ImageView using the solution given by @Preetam

    ImageView loadingImageView = (ImageView) findViewById(R.id.loadingImageView);
    GlideDrawableImageViewTarget loadingImageViewTarget = new GlideDrawableImageViewTarget(loadingImageView);
    Glide.with(this).load(R.raw.spinner).into(loadingImageViewTarget);  
    
  3. Load Image from network in the second ImageView (Order is important if you don't want to hide the loading gif once the image from network is loaded)

    ImageView mainImageView = (ImageView) findViewById(R.id.mainImageView);
    GlideDrawableImageViewTarget mainImageViewTarget = new GlideDrawableImageViewTarget(mainImageView);
    Glide.with(this).load(imageUrl).into(mainImageViewTarget);
    
Gorizia answered 12/8, 2016 at 16:15 Comment(0)
A
0

Here is The Best Way

 Glide.with(a).load(item[position])
            .thumbnail(Glide.with(a).load(R.drawable.preloader))
            .fitCenter()
            .crossFade()
            .into(imageView);
Alkmaar answered 9/10, 2016 at 18:48 Comment(1)
Why downvoted the answer? It works for me, is due to double Glide.with(...Brander
Q
0

Try this. It worked for me and will work for your code too.It works for both images as well as gifs too.

ImageView imageView = (ImageView) findViewById(R.id.test_image); 
    GlideDrawableImageViewTarget imagePreview = new GlideDrawableImageViewTarget(imageView);
    Glide
            .with(this)
            .load(url)
            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {                       
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    return false;
                }
            })
            .into(imagePreview);
}
Questionary answered 17/2, 2017 at 22:47 Comment(0)
I
0
Instead of GlideDrawableImageViewTarget use DrawableImageViewTarget     
ImageView imageView =findViewById(R.id.imageView);
     DrawableImageViewTarget imageViewTarget = new DrawableImageViewTarget (imageView);
     Glide.with(this).load(R.raw.image_loader).into(imageViewTarget);

    library: implementation 'com.github.bumptech.glide:glide:4.8.0'
        annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
Idden answered 29/11, 2018 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.