J'essaie de comprendre comment je peux démarrer et arrêter l'animation dans mon application Android.Démarrage et arrêt de l'animation
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
stopOneButton = (Button) findViewById(R.id.buttonStopOne);
stopTwoButton = (Button) findViewById(R.id.buttonStopTwo);
stopThreeButton = (Button) findViewById(R.id.buttonStopThree);
startButton = (Button) findViewById(R.id.buttonStart);
firstImage = (ImageView) findViewById(R.id.imageView1);
firstImage.setBackgroundResource(R.drawable.spin_animation);
frameAnimation = new AnimationDrawable();
AnimationDrawable frameAnimation = (AnimationDrawable) firstImage.getBackground();
// frameAnimation.start();
firstImage.post(new Starter());
stopOneButton.setOnClickListener(this);
stopTwoButton.setOnClickListener(this);
stopThreeButton.setOnClickListener(this);
startButton.setOnClickListener(this);
}
class Starter implements Runnable {
public void run() {
frameAnimation.start();
}
}
C'est à peu près tout ce que j'ai, et im à partir de zéro sans se lier les boutons à la fonctionnalité, mais l'image ne tourne pas autour du tout.
Ceci est mon fichier XML pour le drawable:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/image1" android:duration="500" />
<item android:drawable="@drawable/image2" android:duration="500" />
<item android:drawable="@drawable/image3" android:duration="500" />
<item android:drawable="@drawable/image4" android:duration="500" />
</animation-list>
code plus pour les boutons:
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonStopOne:
break;
case R.id.buttonStopTwo:
break;
case R.id.buttonStopThree:
break;
case R.id.buttonStart:
frameAnimation.start();
break;
}
}
Quelqu'un peut-il déterminer ce qui im faire le mal?
Je trouve cela un peu confus, ne 'frameAnimation.start();' ne démarre pas mon animation? – JavaCake
Une autre façon de démarrer une animation est d'appeler 'setAnimation' sur' View', de passer dans l'animation, puis d'appeler 'start()' sur l'animation elle-même. Donc dans votre cas ce serait 'firstimage.setAnimation (frameAnimation);' quelque part dans l'ensemble. Puis, quand vous voulez que l'animation joue, 'frameAnimation.start()'. Je suis presque certain que la raison pour laquelle il n'anime pas est que l'animation ne fait référence à aucune vue. – DeeV
pouvez-vous signaler une erreur dans mon code actuel? – JavaCake