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

Difficulty controlling position/state #195

Closed
teddis opened this issue Aug 2, 2015 · 2 comments
Closed

Difficulty controlling position/state #195

teddis opened this issue Aug 2, 2015 · 2 comments

Comments

@teddis
Copy link

teddis commented Aug 2, 2015

I'm finding it impossible to control the position (frame) and state (running/stopped) of a gif animation in a listview item. I have multiple states depending on whether an item is sending, sent, or static (for a chat application).

For sending state, I just want to show frame 0 and stop:

GifDrawable gd = new GifDrawable(mContext.getAssets(), file);
imageView.setImageDrawable(gd);
gd.stop();

but instead it plays despite my calling gd.stop() and/or gd.pause(). perhaps an invalidate causes the animation to continue even though I've set it to stop?

Sent state works and plays fine (I wish there was a completion callback).

For static state, it should show final frame, but I get a blip until the final frame, as though it's playing a few frames prior, then stops or plays the whole animation uber quick and stops. I can't be sure.

GifDrawable gd = new GifDrawable(mContext.getAssets(), file);
imageView.setImageDrawable(gd);
gd.seekToFrame(gd.getNumberOfFrames() - 1);
gd.stop();

Can you offer any advice or is this a possible bug I've discovered? Thanks.

@koral--
Copy link
Owner

koral-- commented Aug 4, 2015

It looks like both scenarios are caused by the same bug - when GifDrawable#setVisible() is called (for example originating from ImageView#setImageDrawable()) then animation is started there without checking if drawable has been previously stopped.
I'll fix that ASAP.

koral-- added a commit that referenced this issue Aug 9, 2015
@koral--
Copy link
Owner

koral-- commented Aug 9, 2015

I rethought relationship between [Drawable#setVisible()](https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html#setVisible%28boolean, boolean%29) (behavior of AnimationDrawable is used now) and (re)starting animation and realized that situation is slightly different.

  • When rendering of next frame is triggered on drawing the current one (which is default behavior) there is no need to do anything when drawable visibility is changing since pausing mechanism is already provided OOTB (simply frames are not rendered when drawable is not drawn)
  • When rendering is not triggered on draw but is performed even if drawable is not drawn (may be achieved using GifDrawableBuilderl#setRenderingTriggeredOnDraw()) then visibility of the drawable may be meaningful (drawable may be visible but not drawn and vice versa). Maybe it will be good idea to add option to configure whether visibility should be taken into account or not but it is not the topic of this issue.

So to sum up GifDrawable#setVisible() has been changed to not affect animation when rendering is triggered on draw, so your snippets will now work.

PS GifDrawable#seekToFrame() is asynchronous so if you are seeking through large number of frames (each subsequent frame must be rendered due to nature of the GIF animations) subsequent frames may be visible in UI. If only last one must be shown then you may use something like this:

                gd.stop();
                gd.seekToFrameAndGet(gd.getNumberOfFrames() - 1);
                gifImageView.setImageDrawable(gd);

or even this:

                GifDrawable gd = new GifDrawable(mContext.getAssets(), file);
                gd.stop();
                imageView.setImageBitmap(gd.seekToFrameAndGet(gd.getNumberOfFrames() - 1));

GifDrawable#seekToFrameAndGet() is synchronous so no intermediate frames will be visible but on the other hand it may take some time to complete for which UI thread is blocked.

@koral-- koral-- closed this as completed Aug 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants