[Android] How to Create Animation Drawable

Berikut cara untuk memasukan animasi GIF ke dalam aplikasi android. Cara ini berguna bagi yang membuat aplikasi tuntunan ibadah, olahraga, materi fisika & kimia, dan aplikasi yang membutuhkan animasi berulang.

Siapkan gambar animasi GIF, kemudian extract setiap frame. Caranya bisa cari di google “extract GIF”.
Contoh wheel0.png, wheel1.png, wheel2.png, wheel3.png, wheel4.png, wheel5.png

Masukan setiap gambar hasil extract tersebut pada folder res/drawable/.
Kemudian buat file animasi.xml pada folder res/drawable/ yang berisi sebagai berikut

<animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    <item android:drawable="@drawable/wheel2" android:duration="50" />
    <item android:drawable="@drawable/wheel3" android:duration="50" />
    <item android:drawable="@drawable/wheel4" android:duration="50" />
    <item android:drawable="@drawable/wheel5" android:duration="50" />
 </animation-list>

Kemudian paste script berikut pada onWindowFocusChanged

// Load the ImageView that will host the animation and
 // set its background to our AnimationDrawable XML resource.
 ImageView img = (ImageView)findViewById(R.id.spin_animation);
 img.setBackgroundResource(R.drawable.animasi);

 // Get the background, which has been compiled to an AnimationDrawable object.
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

 // Start the animation (looped playback by default).
 frameAnimation.start();

Pada layout xml jangan lupa pasang ImageView spin_animation

Download contoh AnimasiGIF

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *