Glide - 无法停止gif onClick-获取TransitionDrawable而不是Animate / GifDrawable [英] Glide - Cannot stop gif onClick- Getting TransitionDrawable instead of Animate/GifDrawable

查看:411
本文介绍了Glide - 无法停止gif onClick-获取TransitionDrawable而不是Animate / GifDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用container recyclerview在Imageview中加载gif图像。
目前,recyclerview只有1个gif,其他的是位图。
我正在加载gif为

I am loading gif image in Imageview with container recyclerview. Currently the recyclerview has only 1 gif and other are bitmaps. I am loading gif as

Glide.with(context).
load("https://media.giphy.com/media/TcKmUDTdICRwY/giphy.gif").
asGif().
override(params.width, params.height).
diskCacheStrategy(DiskCacheStrategy.RESULT).
placeholder(R.drawable.placeholder).
error(R.drawable.error).
listener(new RequestListener<Uri, GifDrawable>() {
        @Override
        public boolean onException(Exception e, Uri model, Target<GifDrawable> target, boolean isFirstResource) {
                 return false;
        }

        @Override
        public boolean onResourceReady(GifDrawable resource, Uri model, Target<GifDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
               resource.stop(); //Stoping gif animation on complete download
                return false;
               }
        }).
        into(feed.imgFeed);

我想要按需gif加载即当gif图像完全下载时我不想启动它当用户点击它时,它就是自己的。
在点击事件中,我正在检查可绘制实例,如果它是Animated / GifDrawable以启动动画。
但是我收到了不允许动画的TransitionDrawable。

I want on Demand gif loading i.e When gif image is completely downloaded i don't want to start it on it's own rather when user clicks it. In click event i am checking drawable instance if it's Animated/GifDrawable to start the animation. But i am receiving TransitionDrawable that does not allow animation.

推荐答案

这是另一种方法:


  • 使用 .diskCacheStrategy(SOURCE)可以显示快速GIF。

  • 一旦你有 SOURCE 缓存,就可以用 .asBitmap()强制显示第一帧。

  • SOURCE 还会确保在加载第一帧和动画时,文件不会被下载两次。

  • onClick 加载具有不同参数的相同图像,即 .asGif() to 启动动画如果您有多个 GIF ,这可以节省大量内存和处理。

  • Use .diskCacheStrategy(SOURCE) to have fast GIF display.
  • Once you have SOURCE cache you can display the image with .asBitmap() forcing the first frame to be displayed.
  • SOURCE will also make sure when loading the first frame and the animation the file won't be downloaded twice.
  • In onClick load the same image with different params, that is .asGif() to "start the animation" this saves a lot of memory and processing if you have multiple GIFs.

这里我们代码:

final Uri uri = Uri.parse("https://media.giphy.com/media/TcKmUDTdICRwY/giphy.gif");
final BitmapRequestBuilder<Uri, GlideDrawable> thumbRequest = Glide
        .with(context)
        .load(uri)
        .asBitmap() // force first frame for Gif
        .transcode(new BitmapToGlideDrawableTranscoder(context), GlideDrawable.class)
        .override(params.width, params.height)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .placeholder(R.drawable.image_placeholder)
        .error(R.drawable.image_error)
        .fitCenter();

thumbRequest.into(feed.imgFeed);

feed.imgFeed.setOnClickListener(new OnClickListener() { 
    @Override public void onClick(View v) {
        Glide
                .with(context)
                .load(uri) // load as usual (Gif as animated, other formats as Bitmap)
                .override(params.width, params.height)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.image_placeholder)
                .error(R.drawable.image_error)
                .thumbnail(thumbRequest)
                .dontAnimate()
                .into(feed.imgFeed);
    }
});

这篇关于Glide - 无法停止gif onClick-获取TransitionDrawable而不是Animate / GifDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆