Kadana Kanz
Kadana Kanz

Reputation: 197

How to start a function when animation has ended?

I have two functions that animate two different TextViews but they both seem to start at the same time but I am trying to have the animation of the first function finish first and then start the second function.

public Boolean functionFinished = false;


public void runFunction(){
    firstFunction();
    if(functionFinished = true){
       secondFunction();
    }

}


public void firstFunction(){

        initialCount = (TextView) findViewById(R.id.textView_Fade);

        initialCount.setText("3");
        final Animation out = new AlphaAnimation(1.0f, 0.0f);
        out.setDuration(1000);
        initialCount.startAnimation(out);

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

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if(initialCount.getText().equals("3")){
                    initialCount.setText("2");
                    initialCount.startAnimation(out);
                } else if(initialCount.getText().equals("2")){
                    initialCount.setText("1");
                    initialCount.startAnimation(out);
                } else if (initialCount.getText().equals("1")){
                    initialCount.setText("START!");

                }

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
   functionFinished = true;
}

The second function simply changes its own textView every second counting up from 0.

What did I do wrong / how do I correct this so that the second function runs after the first function has finished? (ie. sets functionFinished to true only when the TextView from the firstFunction is "START!")

Upvotes: 0

Views: 2488

Answers (3)

Divers
Divers

Reputation: 9569

public void runFunction() {
    firstFunction();
}

public void firstFunction() {
    initialCount = (TextView) findViewById(R.id.textView_Fade);

    initialCount.setText("3");
    final Animation out = new AlphaAnimation(1.0f, 0.0f);
    out.setDuration(1000);

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

        @Override
        public void onAnimationEnd(Animation animation) {
            ...
            secondFunction();
        }

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

    initialCount.startAnimation(out);
}

Just call second function from onAnimationEnd. Also keep in mind that you should attach listener before start animation.

Upvotes: 3

Zhli
Zhli

Reputation: 380

Because you assign functionFinished = true at the end of the firstFunction().

And you must misunderstand that initialCount.startAnimation(out) will block the code and firstFunction() completes only after animation ends .

initialCount.startAnimation(out) will not block any code ,and functionFinished = true is run immediately. so secondFunction() will run immediately after firstFunction() finishes instead of animation finishes.

Upvotes: 0

Paresh
Paresh

Reputation: 6857

If your minimum sdk version is 14:-

textView1.animate()
         .alpha(0f)
         .setDuration(400)
         .setAnimationListener(new AnimationListener() { 
          @Override 
          public void onAnimationEnd(Animation arg0) {
               // animate second textview here....
          } 
}); 

Upvotes: 1

Related Questions