Fresco: How to set loopcount for AnimatedImage after v1.4.0?

Created on 11 Aug 2017  ·  4Comments  ·  Source: facebook/fresco

Now I aim to play a gif image 5 time.
And I found there is a method getLoopCount for get the number of loops for a animatedImage,
but the question is I don't know how to dynamic set loopCount.
Waiting for your help.

question

Most helpful comment

Ideally, your GIF would be properly formatted and have the loop count set to N.

Otherwise, you can create a modifying backend, something like this:

public class LoopCountModifyingBackend extends AnimationBackendDelegate {

  private int mLoopCount;

  public LoopCountModifyingBackend(
      @Nullable AnimationBackend animationBackend,
      int loopCount) {
    super(animationBackend);
    mLoopCount = loopCount;
  }

  @Override
  public int getLoopCount() {
    return mLoopCount;
  }
}

and then set it to your animated drawable:

final DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setAutoPlayAnimations(true)
    .setUri(uri)
    .setControllerListener(new BaseControllerListener<ImageInfo>() {
      @Override
      public void onFinalImageSet(
          String id,
          @Nullable ImageInfo imageInfo,
          @Nullable Animatable animatable) {
        if (animatable instanceof AnimatedDrawable2) {
          AnimatedDrawable2 animatedDrawable = (AnimatedDrawable2) animatable;
          animatedDrawable.setAnimationBackend(new LoopCountModifyingBackend(animatedDrawable.getAnimationBackend(), 3));
        }
      }
    })
    .build();
simpleDraweeView.setController(controller);

You can also create a custom DrawableFactory that does this directly and then pass it when the image is rendered via PipelineDraweeController.

All 4 comments

Ideally, your GIF would be properly formatted and have the loop count set to N.

Otherwise, you can create a modifying backend, something like this:

public class LoopCountModifyingBackend extends AnimationBackendDelegate {

  private int mLoopCount;

  public LoopCountModifyingBackend(
      @Nullable AnimationBackend animationBackend,
      int loopCount) {
    super(animationBackend);
    mLoopCount = loopCount;
  }

  @Override
  public int getLoopCount() {
    return mLoopCount;
  }
}

and then set it to your animated drawable:

final DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setAutoPlayAnimations(true)
    .setUri(uri)
    .setControllerListener(new BaseControllerListener<ImageInfo>() {
      @Override
      public void onFinalImageSet(
          String id,
          @Nullable ImageInfo imageInfo,
          @Nullable Animatable animatable) {
        if (animatable instanceof AnimatedDrawable2) {
          AnimatedDrawable2 animatedDrawable = (AnimatedDrawable2) animatable;
          animatedDrawable.setAnimationBackend(new LoopCountModifyingBackend(animatedDrawable.getAnimationBackend(), 3));
        }
      }
    })
    .build();
simpleDraweeView.setController(controller);

You can also create a custom DrawableFactory that does this directly and then pass it when the image is rendered via PipelineDraweeController.

Thanks.

now for fresco 2.0.0+ ,how to do this?

works very well!

Was this page helpful?
0 / 5 - 0 ratings