Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Animation List Changing Dynamically

I have an animation.xml with 4 frames.

<?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/male01a" android:duration="4000" />
    <item android:drawable="@drawable/male01b" android:duration="1100" />
    <item android:drawable="@drawable/male01c" android:duration="1100" />
    <item android:drawable="@drawable/male01d" android:duration="300" />
</animation-list>

Is there anyway I can use some sort of variable here for each drawable? Much like using "@string/animation1" where animation1 points to "@drawable/male01a" in Strings.xml. But I need some way to change the drawables, say to male02a, in the code.

like image 734
cerealspiller Avatar asked Aug 24 '26 22:08

cerealspiller


1 Answers

You can dynamically add frames to your animation using the method addFrame().

Example:

ImageView imview = (ImageView) findViewById(R.id.ivImage);
AnimationDrawable ad = (AnimationDrawable) imview.getDrawable();
Drawable frame = Drawable.createFromPath("your image path");
ad.addFrame(frame, 200);
like image 145
Derzu Avatar answered Aug 26 '26 23:08

Derzu