I\'m new to Android platform. I\'m using the following to animate a set of 16 \"frames\" using AminationDrawable in my app:
In the XML file I have:
I had the same problem but I managed to solve it when reading the source code of AnimationDrawable by implementing my own AnimationDrawable class that extends AnimationDrawable and override the Run() method and add setDuration() method which allows me to set the duration as follow:
By reviewing the original run method we see that it do the same but by calling scheduleSelf(this, SystemClock.uptimeMillis() + duration); with the duration you specified when adding the frame so I changed it. I also add duration because I use the same for all my frames but you can use array of new duration.
import android.graphics.drawable.AnimationDrawable;
import android.os.SystemClock;
public class MyAnimationDrawable extends AnimationDrawable {
private volatile int duration;//its volatile because another thread will update its value
private int currentFrame;
public MyAnimationDrawable() {
currentFrame = 0;
}
@Override
public void run() {
int n = getNumberOfFrames();
currentFrame++;
if (currentFrame >= n) {
currentFrame = 0;
}
selectDrawable(currentFrame);
scheduleSelf(this, SystemClock.uptimeMillis() + duration);
}
public void setDuration(int duration) {
this.duration = duration;
//we have to do the following or the next frame will be displayed after the old duration
unscheduleSelf(this);
selectDrawable(currentFrame);
scheduleSelf(this, SystemClock.uptimeMillis()+duration);
}
}
It's my first answer so I hope it helps you and it's explained well.