2015-11-24 1 views
0

J'ai travaillé sur un petit jeu et j'ai besoin d'une animation de barre de progression, qui commence quand je touche un bouton pour la première fois et si je touche à nouveau le bouton avant l'animation terminé la barre de progression doit réinitialiser.Android: annuler() sur Animation ne fonctionne pas

Dans mon code animation.start(); fonctionne bien, mais le animation.cancel() ne semble pas fonctionner.

French Version of this Question

Mon code d'activité contenant la ligne qui ne fonctionne pas:

public class MainActivity extends Activity { 

    Button b_bleu; 
    PrgressBar bar1; 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     b_bleu.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      ObjectAnimator animation = ObjectAnimator.ofInt(bar1, "progress", 100, 0); 
      animation.setDuration(5000); 
      animation.addListener(new Animator.AnimatorListener() { 
       @Override 
       public void onAnimationStart(Animator animator) { 

       } 

       @Override 
       public void onAnimationEnd(Animator animator) { 
        //application se termine 
       } 

       @Override 
       public void onAnimationCancel(Animator animator) { 

       } 

       @Override 
       public void onAnimationRepeat(Animator animator) { 

       } 
      }); 

      animation.cancel(); 
      animation.start(); 
     } 
    }); 
} 
+0

Pouvez-vous élaborer sur «ne semble pas fonctionner»? Qu'attendez-vous qu'il se passe? Qu'est-ce qui se passe réellement? – alanv

Répondre

0

Je pense que le problème est que animation.cancel() n'a pas d'effet puisque vous appelez animation.start(); juste après.

Essayez quelque chose comme ceci:

Déclarez un champ:

private boolean mStarted = false; 

ensuite la cassette dans callbacks d'animation et vérifier sa valeur:

animation.addListener(new Animator.AnimatorListener() { 
    @Override 
    public void onAnimationStart(Animator animation) { 
     mHasStarted = true; 
    } 

    @Override 
    public void onAnimationEnd(Animator animation) { 
     mHasStarted = false; // reset the field value so that it can be started again onClick once it has ened 
    } 

    @Override 
    public void onAnimationCancel(Animator animation) { 
     mHasStarted = false; 
    } 
}); 

if (mStarted) { 
    animation.cancel(); 
} else { 
    animation.start(); 
} 

Hope this helps!

+0

merci pour votre réponse, mais je trouve une solution avec animation.setAutoCancel (false) :) si quelqu'un veut le code, demandez-moi;) –