【问题标题】:Show GIF file with Glide (image loading and caching library)使用 Glide 显示 GIF 文件(图像加载和缓存库)
【发布时间】:2015-09-13 22:44:51
【问题描述】:

我尝试在图像视图中显示 GIF 图像作为加载占位符 - 使用 Glide Library

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

我尝试显示这个文件

loading2.gif

但得到这个错误:

错误:(54, 86) 错误: 找不到符号方法 asGif()

如何在 imageView 中使用 Glide 显示 GIF 文件?

【问题讨论】:

  • 看起来您可能缺少正确版本的 Glide?真是奇怪,没找到符号也试试o个干净。

标签: android imageview gif android-glide


【解决方案1】:

上面的答案对我不起作用。下面的代码有效。

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

【讨论】:

  • 我不知道为什么,但这确实 对我有用,而 asGif() 只是让整个事情消失了。
  • 你能解释一下 GlideDrawableImageViewTarget 的用途吗?
  • 这个答案只显示了 gif 文件。它不像占位符那样工作。当图像像占位符一样加载时,我如何使用这个答案来显示 gif 文件?
  • @Jaydev 我关注了 Nick Butcher 的“Plaid”应用程序源代码。我尝试了 Glide doc 中的所有内容,但都没有成功。
  • @Jaydev 你可以在这里找到 Plaid 应用的源代码:github.com/nickbutcher/plaid
【解决方案2】:

对于 Glide 4.+

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(this).asGif().load(R.raw.image_gif).into(imageView);

【讨论】:

  • 非常感谢,Glide 比 Ion 好太多了,因为 Ion 一直落后于我的声音,可能是因为 Ion 占用更多内存或其他什么但它使我的声音出现故障,而且我更喜欢 Glide 语法,感觉更合理对我来说很高兴我找到 Glide 来解决我的问题。 :)
  • 我看不到 gif ImageView imageView = (ImageView) v.findViewById(R.id.gif); Glide.with(this).asGif().load(R.raw.steam4).into(imageView);代码:
【解决方案3】:

对于 Glide 3.0,您需要提前设置 asGif():

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

请记住,仅使用 load() 将根据数据类型加载 GIF 或位图。如果给定的 url 不是 gif,除非您希望加载失败,否则您不需要指定 asGif()

【讨论】:

    【解决方案4】:

    我遇到了同样的问题。您可以通过移动 asGif() 所在的位置来解决它。它必须直接出现在 .load() 之前。

    所以改变:

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

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

    【讨论】:

    • 请说明您的解决方案可以与 Glide 4+ 一起使用
    • @marc:感谢它工作,但错误和占位符不起作用
    【解决方案5】:

    这是另一个例子。我观察到 Glide 有时不会自动播放 Gif。这个解决方案对我有用:

    在示例中,imageView 是 RecyclerView 的 ViewHolder 类中的成员之一。

    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);
             }
         });
    

    .asGif()方法需要提前使用,否则无法识别

    【讨论】:

    • 哇。谢谢@Ram lyer。
    【解决方案6】:

    使用 GlideDrawableImageViewTarget。这段代码对我有用。

    GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
    
    Glide.with(this).load(R.drawable.logo_gif).into(imageViewTarget);
    

    【讨论】:

      【解决方案7】:

      Glide自动播放动画gif文件,无需额外方法

      使用下面的代码

      Glide.with(this)
         .load(R.drawable.logo)
         .into(imageView);
      

      【讨论】:

      • 你真的应该添加一些解释为什么这应该工作 - 你也可以添加代码以及代码本身中的 cmets - 在当前的形式中,它没有提供任何可以帮助的解释社区的其他人了解您为解决/回答问题所做的工作。这对于较早的问题和已经有答案的问题尤其重要。
      • 新的滑翔版本只是播放动画 gif
      【解决方案8】:

      从 Glide v4 版本开始:

      依赖:

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

      访问this link创建GlideApp类:

      你可以直接在GlideApp上给drawable参考

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

      如果您有任何错误,请告诉我。

      【讨论】:

        【解决方案9】:

        我使用的是 Glide 4.11 版,对我来说,它确实在 Glide.with(context) 之后设置了 .asGif()

        但是我还有一个问题,我在加载 gif 到 ImageView 时也设置了.dontAnimate()。显然这不起作用,所以我不得不从我的设置中删除.dontAnimate(),并且在更改之后一切正常。

        这里是更多信息的问题https://github.com/bumptech/glide/issues/3395

        【讨论】:

          【解决方案10】:

          把你的 gif 文件放在 raw 文件夹中,使用下面的代码还可以添加下面的代码 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);
          
            
          

          【讨论】:

            【解决方案11】:

            这是最好的方法

             Glide.with(a).load(item[position])
                        .thumbnail(Glide.with(a).load(R.drawable.preloader))
                        .fitCenter()
                        .crossFade()
                        .into(imageView);
            

            【讨论】:

            • 为什么对答案投了反对票?它对我有用,是由于双重Glide.with(...
            【解决方案12】:

            试试这个。它对我有用,也适用于您的代码。它也适用于图像和 gif。

            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);
            }
            

            【讨论】:

              【解决方案13】:

              这个解决方案是一种解决方法

              <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. 不要使用一个,而是使用 2 个 ImageView

              2. 使用@Preetam给出的解决方案将加载gif放入第一个ImageView

                ImageView loadingImageView = (ImageView) findViewById(R.id.loadingImageView);
                GlideDrawableImageViewTarget loadingImageViewTarget = new GlideDrawableImageViewTarget(loadingImageView);
                Glide.with(this).load(R.raw.spinner).into(loadingImageViewTarget);  
                
              3. 在第二个 ImageView 中从网络加载图像 (如果您不想隐藏加载 gif 一次,顺序很重要 网络图片已加载)

                ImageView mainImageView = (ImageView) findViewById(R.id.mainImageView);
                GlideDrawableImageViewTarget mainImageViewTarget = new GlideDrawableImageViewTarget(mainImageView);
                Glide.with(this).load(imageUrl).into(mainImageViewTarget);
                

              【讨论】:

                【解决方案14】:
                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'
                

                【讨论】:

                  猜你喜欢
                  • 2017-02-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-06-05
                  • 2017-07-01
                  • 2015-06-26
                  • 2018-11-21
                  相关资源
                  最近更新 更多