Glide - 无法停止gif onClick-获取TransitionDrawable而不是Animate / GifDrawable

时间:2016-03-17 12:51:28

标签: android imageview gif android-glide

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

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。

2 个答案:

答案 0 :(得分:2)

Here is an alternative approach :

  • 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.

Here us code :

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

答案 1 :(得分:0)

确定

我不得不改变加载GIF的步骤,因为即使加载第一帧也需要花费很多时间并且需要1 mb数据(任意值),因此用户将浪费5 mb来加载5 gif的帧。 因此改变了流程,从服务器获取2个图像版本

  1. Gif,实际图片
  2. Gif的第一帧,100 kb Max
  3. 在图像点击上加载Gif也能正常工作并保存数据。