Salut,

J'ai un bout de code où je voudrais que quand l'utilisateur clique une première fois sur un bouton, l'animation commence, et ensuite soit l'animation se termine et le jeu se termine, ou soit il clique sur le bouton avant que l'animation se termine et alors l'animation recommence. Mon animation est une progress bar qui diminue au fil du temps.

Voici mon code. Concrètement, c'est le animation.cancel() qui ne marche pas.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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();
 
        }
    });
 
}