Android Animationdrawable and Knowing When Animation Ends

How do I wait for AnimationDrawable to end before continuing code?

Try:

YourAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }

@Override
public void onAnimationEnd(Animation animation) {
//Ended
}

@Override
public void onAnimationRepeat(Animation animation) { }
});

EDIT: Oops. Didn't read the drawable part. Here's a recursive function I use to check if all frames are over:

 private void checkIfAnimationDone(AnimationDrawable anim){
final AnimationDrawable a = anim;
int timeBetweenChecks = 300;
Handler h = new Handler();
h.postDelayed(new Runnable(){
public void run(){
if (a.getCurrent() != a.getFrame(a.getNumberOfFrames() - 1)){
checkIfAnimationDone(a);
} else{
Toast.makeText(getApplicationContext(), "ANIMATION DONE!", Toast.LENGTH_SHORT).show();
}
}
}, timeBetweenChecks);
};

You could also log the time when you start the animation, and add the duration (which you know before hand) to it to get the ending time. However, this can be inaccurate, as setting and starting the animation takes a bit of time as well.

Is there a way to listen for animation end in AnimatedVectorDrawables

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 uses VectorDrawable's private method(s). So, this approach won't work.

We could create a simple wrapper class around AnimatedVectorDrawable and add callbacks:

public class AVDWrapper {

private Handler mHandler;
private Animatable mDrawable;
private Callback mCallback;
private Runnable mAnimationDoneRunnable = new Runnable() {

@Override
public void run() {
if (mCallback != null)
mCallback.onAnimationDone();
}
};

public interface Callback {
public void onAnimationDone();
public void onAnimationStopped();
}

public AVDWrapper(Animatable drawable,
Handler handler, Callback callback) {
mDrawable = drawable;
mHandler = handler;
mCallback = callback;
}

// Duration of the animation
public void start(long duration) {
mDrawable.start();
mHandler.postDelayed(mAnimationDoneRunnable, duration);
}

public void stop() {
mDrawable.stop();
mHandler.removeCallbacks(mAnimationDoneRunnable);

if (mCallback != null)
mCallback.onAnimationStopped();
}
}

Your code would look like:

final Drawable drawable = circle.getDrawable();
final Animatable animatable = (Animatable) drawable;

AVDWrapper.Callback callback = new AVDWrapper.Callback() {
@Override
public void onAnimationDone() {
tick.setAlpha(1f);
}

@Override
public void onAnimationStopped() {
// Okay
}
};

AVDWrapper avdw = new AVDWrapper(animatable, mHandler, callback);
//animatable.start();
avdw.start(2000L);

tick.setAlpha(0f);
//tick.animate().alpha(1f).setStartDelay(2000).setDuration(1).start();

// One wrapper is sufficient if the duration is same
final Drawable drawable2 = tick.getDrawable();
final Animatable animatable2 = (Animatable) drawable2;
animatable2.start();

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.

Android, How can I know that the animation is finished?

On your animation object call this code:

am1.setAnimationListener(new AnimationListener() {    
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}

@Override
public void onAnimationEnd(Animation animation) {
// Pass the Intent to switch to other Activity

}
});

Android AnimationDrawable creation and looping

you cannot start and stop like you did one after another , the animation happens to be an aysnc process.

You will have to find another way , for example to create a listener for when the animationdrawable is out of frames , then re invoke the animation .

Take a look here for hints .
Android AnimationDrawable and knowing when animation ends

Frame Animation callback when animation is complete in Android

No there is no way to know when the FrameAnimation ends.

Refer this: question

i suggest this method : here

android animation is not finished in onAnimationEnd

Here is the actual bug related to this issue http://code.google.com/p/android-misc-widgets/issues/detail?id=8

This basically states that the onAnimationEnd method doesn't really work well when an AnimationListener is attached to an Animation

The workaround is to listen for the animation events in the view to which you were applying the animation to
For example if initially you were attaching the animation listener to the animation like this

mAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
//Functionality here
}
});

and then applying to the animation to a ImageView like this

mImageView.startAnimation(mAnimation);

To work around this issue, you must now create a custom ImageView

public class MyImageView extends ImageView {

and then override the onAnimationEnd method of the View class and provide all the functionality there

@Override
protected void onAnimationEnd() {
super.onAnimationEnd();
//Functionality here
}

This is the proper workaround for this issue, provide the functionality in the over-riden View -> onAnimationEnd method as opposed to the onAnimationEnd method of the AnimationListener attached to the Animation.

This works properly and there is no longer any flicker towards the end of the animation. Hope this helps.



Related Topics



Leave a reply



Submit