• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps how to change speed animation at run-time?

kimsangyoo

Lurker
Dec 17, 2011
2
0
i have a list sprites in drawable folder. One xml file with:
PHP:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/fire_00000" android:duration="400" />
    <item android:drawable="@drawable/fire_00001" android:duration="400" />
    <item android:drawable="@drawable/fire_00002" android:duration="400" />
    <item android:drawable="@drawable/fire_00003" android:duration="400" />
    <item android:drawable="@drawable/fire_00004" android:duration="400" />
</animation-list>

i used AnimationDrawable to run sprites on ImageView, now. i want to set duration properties by hand, if i have a seekbar to change this. How can i do that? Tell me
sr, my English gammar is bad! :eek:
 
Have a look at the documentation of AnimationDrawable.

You have a getFrame(index) Method that returns a single Frame. You further have a addFrame(drawable, duration) method that adds a frame to the Animation. Unfortunately, there is no "setDuration()" method.

You could however create a copy of the AnimationDrawable and adjust the duration of each frame. The code could look something like this

Code:
public AnimationDrawable resampleAnimation(AnimationDrawable input, float factor)
{
    AnimationDrawable output = new AnimationDrawable();

    for(int i = 0 ; i < input.getNumberOfFrames() ; i++)
    {
        int old_duration = input.getDuration(i);
        output.addFrame(input.getFrame(i), old_duration * factor);
    }

    output.setOneShot(input.isOneShot());
    return output;
}

In case you try that code, please tell us if it worked :)
 
Upvote 0
I tested the code of herosgame. The AnimationDrawable changes the frame duration in the debugger but the speed of AnimationDrawable is the same.

myAnimationDrawable = resampleAnimation(myAnimationDrawable ,100);
myAnimationDrawable = (AnimationDrawable)imageView.getDrawable();
myAnimationDrawable .setCallback(imageView);
myAnimationDrawable .start();

The speed is still the same. What's the problem?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones