Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android wait till animation is completed

I am moving an image and I want to play a sound file after the object's animation is completed
The image moves but I tried using Threads to wait until a duration but it didn't work.

Animation animationFalling = AnimationUtils.loadAnimation(this, R.anim.falling);
iv.startAnimation(animationFalling);
MediaPlayer mp_file = MediaPlayer.create(this, R.raw.s1);
duration = animationFalling.getDuration();
mp_file.pause();
new Thread(new Runnable() {
    public void run() {
        try {
            Thread.sleep(duration);
            mp_file.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
  }).start();

Thanks.

like image 204
Kamran224 Avatar asked Oct 20 '25 16:10

Kamran224


1 Answers

you can register a delegate for the animation:

animationFalling.setAnimationListener(new AnimationListener() {

     @Override
     public void onAnimationStart(Animation animation) {    
     }

     @Override
     public void onAnimationRepeat(Animation animation) {
     }

     @Override
     public void onAnimationEnd(Animation animation) {
           // here you can play your sound
     }
);

you can read more about the AnimationListener here

like image 141
Blackbelt Avatar answered Oct 23 '25 07:10

Blackbelt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!