Skip to content Skip to sidebar Skip to footer

Glide - Cannot Stop Gif OnClick- Getting TransitionDrawable Instead Of Animate/GifDrawable

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('ht

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

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


Post a Comment for "Glide - Cannot Stop Gif OnClick- Getting TransitionDrawable Instead Of Animate/GifDrawable"