Reputation: 142
I am using glide to load a gif in my android project.
Glide.with(getActivity()).load(mainDrawable).asGif().into(mainDrawableView);
This keeps the gif on repeat, how can one stop the gif from looping?
Upvotes: 2
Views: 5370
Reputation: 3095
For stop glide gif animation use dontAnimate()
For stop Animation
Glide.with(this).load(R.drawable.123).dontAnimate().into(imgGif);
For start Animation
Glide.with(this).asGif().load(R.drawable.123).into(imgGif);
Upvotes: 0
Reputation: 1068
This is what worked for me. Glide V4
Glide.with(this).asGif().load(/*your gif url*/).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
resource.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
//do whatever after specified number of loops complete
}
});
return false;
}}).into(imageView);
Upvotes: 3
Reputation: 1722
setLoopCount in GifDrawable inside RequestListener
import static com.bumptech.glide.load.resource.gif.GifDrawable.LOOP_INTRINSIC;
Glide.with(context).asGif().listener(getRequest()).load(R.raw.gif_capture).into(imgProgress);
public RequestListener<GifDrawable> getRequest() {
return new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(LOOP_INTRINSIC);
return false;
}
};
}
Upvotes: 0
Reputation: 608
Github post has a solution to add request listener & stop the animation in onResourceReady() method.
However, this didn't work for me. May be because in my case resource was local. I ended up with following not so bad workaround:
Export still image from Gif using Gimp or Photoshop. Load still image into ImageView when you want to stop animation. Load original Gif when you want to show animation. Of course this workaround is only good enough for small & local files.
Upvotes: 1