Je voulais ajouter une animation image par image à ma place, j'ai donc utilisé la référence formelle et trouvé l'objet AnimationDrawable. est ici le lien: https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.htmlfichier de ressources dessinable avec sélecteur <animation-list> reconnu comme StateListDrawble
je définissais l'animation dans un fichier XML placé dans le dossier drawable, voici le code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<animation-list android:id="@+id/exAnim" android:oneshot="true">
<item android:drawable="@drawable/explosion1" android:duration="50"/>
<item android:drawable="@drawable/explosion2" android:duration="50"/>
<item android:drawable="@drawable/explosion3" android:duration="50"/>
<item android:drawable="@drawable/explosion4" android:duration="50"/>
<item android:drawable="@drawable/explosion5" android:duration="50"/>
<item android:drawable="@drawable/explosion6" android:duration="50"/>
<item android:drawable="@drawable/explosion7" android:duration="50"/>
<item android:drawable="@drawable/explosion8" android:duration="50"/>
<item android:drawable="@drawable/explosion9" android:duration="50"/>
<item android:drawable="@drawable/explosion10" android:duration="50"/>
<item android:drawable="@drawable/explosion11" android:duration="50"/>
<item android:drawable="@drawable/explosion12" android:duration="50"/>
<item android:drawable="@drawable/explosion13" android:duration="50"/>
<item android:drawable="@drawable/explosion14" android:duration="50"/>
<item android:drawable="@drawable/explosion15" android:duration="50"/>
</animation-list>
</selector>
i définit également un ImageView qui accueillera l'animation dans la mise en page XML:
<ImageView
android:onClick="startIt"
android:id="@+id/fire"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_below="@+id/btn_game"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Et puis je définissais une méthode qui devrait démarrer l'animation lorsque l'utilisateur clique sur ImageView:
public void startIt(View v){
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView img = (ImageView)findViewById(R.id.fire);
img.setImageResource(R.drawable.explode_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getDrawable();
}
Lorsque je tente de cliquer sur le ImageView cette exception est levée:
Caused by: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Merci à tous les assistants.
Toujours la même chose :(, merci quand même! –