Bonjour,
J'ai un souci avec mon code je developpe un jeu sur android studio et je rencontre le souci suivant.
Il y a deuw boutons sur mon appli, un bouton Punch et un Bouton Kick et une Imageview dans laquelle j'anime mes images.
Quand je clique sur le bouton punch, je cree une animation drawable de deux images
quand je clique sur le bouton kick, je cree une animation drawable de deux images egalement.
Le probleme est que quand j'execute ce code avec deux animations drawable, l'application crashe!!
j'ai pourtant utiliser deux imageview differentes pour les deux animations
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class MainActivity extends AppCompatActivity { private Button btnPunch; private Button btnKick; private ImageView imageView1 ; private ImageView imageView2 ; private AnimationDrawable[] animationDrawable = new AnimationDrawable[2]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnPunch = (Button) findViewById(R.id.btnPunch); imageView1 = (ImageView) findViewById(R.id.imageView1); imageView1.setBackgroundResource(R.drawable.animationpunch); animationDrawable[0] = (AnimationDrawable) imageView1.getBackground(); imageView2 = (ImageView) findViewById(R.id.imageView2); imageView2.setBackgroundResource(R.drawable.animationkick); animationDrawable[1] = (AnimationDrawable) imageView2.getBackground(); btnPunch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animationDrawable[1].stop(); animationDrawable[0].setOneShot(true); animationDrawable[0].start(); } }); btnKick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animationDrawable[0].stop(); animationDrawable[1].setOneShot(true); animationDrawable[1].start(); } }); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); //animationDrawable.start(); } }
par contre lorsque j'enleve une animation comme ceci, ca fonctionne bien mais du coup je suis limité a utiliser le bouton punch, je voudrais que ca marche avec les deux boutons
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 public class MainActivity extends AppCompatActivity { private Button btnPunch; private ImageView imageView1 ; private AnimationDrawable[] animationDrawable = new AnimationDrawable[2]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnPunch = (Button) findViewById(R.id.btnPunch); imageView1 = (ImageView) findViewById(R.id.imageView1); imageView1.setBackgroundResource(R.drawable.animationpunch); animationDrawable[0] = (AnimationDrawable) imageView1.getBackground(); btnPunch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animationDrawable[1].stop(); animationDrawable[0].setOneShot(true); animationDrawable[0].start(); } }); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); //animationDrawable.start(); } }
je clique sur bouton punch, ca anime mon animation unch
je clique sur bouton kick, ca anime mon animation kick
une aide?
Partager