Glide - Cannot stop gif onClick- Getting TransitionDrawable instead of Animate/GifDrawable

帅比萌擦擦* 提交于 2019-12-13 07:12:59

问题


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

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.


回答1:


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



回答2:


Ok

I had to change the steps to load gifs as even loading first frame was taking much time and was taking 1 mb data (arbitary value) so user would waste 5 mb just to load frame of 5 gifs. Hence changed the flow,taking 2 image version from server

  1. Gif,Actual image
  2. First frame of Gif,100 kb Max

Loading Gif on image click works and saves data as well.



来源:https://stackoverflow.com/questions/36061091/glide-cannot-stop-gif-onclick-getting-transitiondrawable-instead-of-animate-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!