Snake
Snake

Reputation: 14648

How to continue processing after animation ended?

Here is the situation. My Activity is running, and I call a function to start an animation. Once this animation finishes, I need to wait for another 2 seconds, then continue the process normally. What do you suggest I do?

The OnAnimationEnd does not help my case by the time I reach the end of iteration, my process has already returned from the calling function and continued processing. What do you think I do? Do thread.sleep (duration of animation + 2 sec)? Thanks

EDIT: OK I thought maybe I should explain what I am doing in code I have the following code

Public class myclassActivity extends Activity{
..
.
.
.
 public void runGame(){

  player1.startAnimating();
  player2.start Animating();
  player3.startAnimating();
  updateStatus();
  runGame();
 }

}

So as you can see my activity keeps calling startAnimating for each player. In which some work and then animation for 1 second is done.

I don't want player2.startAnimation to start untill the player1.startAnimation is completed and so on for the rest of the code. Currently what happens is that the code executes fully and rerun again while the animation is still running. Pleasee any help on this?

Upvotes: 1

Views: 2292

Answers (3)

VSC
VSC

Reputation: 272

observe this code it is help full you.

public class PlayersActivity extends Activity {
ImageView I1,I2;
Button B;
Animation T1,T2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    I1=(ImageView)findViewById(R.id.i1);
    I2=(ImageView)findViewById(R.id.i2);

    B=(Button)findViewById(R.id.b1);

    B.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            T1=new TranslateAnimation(0,100,0,0);
            T1.setDuration(1000);
            I1.startAnimation(T1);
            T1.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) {
                    T2=new TranslateAnimation(0,100,0,0);
                    T2.setDuration(1000);
                    T2.setStartOffset(2000);
                    I2.startAnimation(T2);
                    T2.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) {
                            T1.setStartOffset(2000);
                            I1.startAnimation(T1);

                        }
                    });

                }
            });


        }
    });

}

}

Suppose you will start this two methods after completed player3, you are placing player3 onAnimationEnd(){ updateStatus(); runGame();}

Upvotes: 2

Kurosawa Hiroyuki
Kurosawa Hiroyuki

Reputation: 1267

how about passing caller instance by

player1.startAnimating(this)

and then call method like

myclassActivity#animationFinished(this) 

on finishing animation of player1/2? then

void animationFinished(Player p) {
   if (p==player1) {
       player2.start();
   } else if (p==player2) {
       player3.start();
   }
}

or something

Upvotes: 0

asktomsk
asktomsk

Reputation: 2298

Use postDelayed function to run something after exact delay. For example (2 sec delay):

LinearLayout.postDelayed(new Runnable() { 
    public void run() { 
      //Do something
    } 
}, 2000);

Upvotes: 0

Related Questions