Reputation: 2033
I'm trying to perform frame by frame animation in android. For this task I created an xml file called "anim.xml" like this:
<?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/square0" android:duration="100" />
<item android:drawable="@drawable/square1" android:duration="100" />
<item android:drawable="@drawable/square2" android:duration="100" />
<item android:drawable="@drawable/square3" android:duration="100" />
<item android:drawable="@drawable/square4" android:duration="100" />
<item android:drawable="@drawable/square5" android:duration="100" />
</animation-list>
Then at a frame layout that I have defined, I tried to set it as background and start it at onCreate like this:
FrameLayout imgView = (FrameLayout)findViewById(R.id.frameLayout1);
imgView.setBackgroundResource(R.drawable.anim);
AnimationDrawable anim = (AnimationDrawable) imgView.getBackground();
anim.start();
What I'm experiencing is the first frame only, but what I'm going for is an animation of squares to be on a loop. Do you have any opinions regarding what I have done wrong ?
Cheers.
Upvotes: 1
Views: 252
Reputation: 384
To me, Matt's solution seems a little overpowered for this case. You can simply move your call of the start()
method to the onWindowFocusChanged()
method because at that point the AnimationDrawable
is fully attached to the window and can actually be started. Like this:
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
anim.start();
}
Do all the other stuff in your onCreate()
method and make the AnimationDrawable
a class variable, so you can access it from onWindowFocusChanged()
method.
Upvotes: 4
Reputation: 1414
I've experience issues before when trying to get animations to start in the onCreate method. Try repalcing your last line with something like:
imgView.post(new Runnable()
{
@Override
public void run()
{
anim.start();
}
});
This will essentialy make you animation start after onCreate has executed.
Upvotes: 1