I have created an AnimatedVectorDrawable, it works pretty well, now I am looking for a way to change the animation or hide the view after it finishes. I was hoping there was a listener but it doesn't look like there is. Can someone help?
EDIT
So I found a workaround, but not a very elegant way. What I did was create a thread and poll if the animation is running.
new Runnable() {
public void run() {
while(mLoop) {
if(mAnimatedVectorDrawable.isRunning()) {
Thread.sleep(mPollingInterval);
} else {
mLoop = false;
// TODO what ever
}
}
}
};
If somebody finds a better solution, please share.
Not tried this yet but Android 6.0 has a registerAnimationCallback method from the Animatable2 interface for this
Edit: yep this works in Android 6.0 emulator:
Edit2: looks like they haven't added support for this in the AnimatedVectorDrawableCompat from support lib 23.2+ :(Edit3: it looks like this got added in support lib 25.3.0
Thanks Carson!
It is strange that there is no direct API for getting this. I suppose a workaround that doesn't involve overrides would just be simply to find the animation in your set of animations that takes the most amount of time, then post a delayed runnable to hide the view.
Downside is that it involves hard coding your longest animation time, but as long as you are referencing the same constant value both in the animation XML and in code, it would work correctly, no overrides needed.
I found the same problem. I am trying to animate a circle being drawn and a tick inside.
The best I could do is play with durations. Something like this:
It would be great to have a listener
My first instinct was to take the source code, add some callbacks, and create a custom drawable out of it. Of course, that would have meant no xml support.
It turns out that
AnimatedVectorDrawable
usesVectorDrawable's
private method(s). So, this approach won't work.We could create a simple wrapper class around
AnimatedVectorDrawable
and add callbacks:Your code would look like:
But, this is exactly what you are doing with
setStartDelay
. So I don't know how useful this will be.Edit: All this can also be implemented inside an extended AnimatedVectorDrawable. But, you'll lose xml support altogether.