Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set loopcount for AnimatedImage after v1.4.0? #1863

Closed
zewenwang opened this issue Aug 11, 2017 · 4 comments
Closed

How to set loopcount for AnimatedImage after v1.4.0? #1863

zewenwang opened this issue Aug 11, 2017 · 4 comments
Labels

Comments

@zewenwang
Copy link

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.

@oprisnik
Copy link
Contributor

oprisnik commented Aug 16, 2017

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.

@zewenwang
Copy link
Author

Thanks.

@GrossGhost
Copy link

now for fresco 2.0.0+ ,how to do this?

@qiaogaojian
Copy link

works very well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants