Simon
Simon

Reputation: 509

Setting a listener to frame animation

I have the following XML file to define my frame animation:

<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/youme_blink_frame_0000" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0001" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0002" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0003" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0004" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0005" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0006" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0007" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0008" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0009" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0010" android:duration="40" />
</animation-list>

Then I have the following code:

    Animation mAnim = AnimationUtils.loadAnimation(this, R.anim.blink);
    mAnim.setAnimationListener(this);

    ImageView img = (ImageView)findViewById(R.id.animationImage);
    img.clearAnimation();
    img.setAnimation(mAnim);
    img.startAnimation(mAnim);

This code generates an exception with the error "animation file not found". Is it that a frame animation is not considered to be an animation or I am doing something wrong?

Thanks, Simon

Upvotes: 1

Views: 1203

Answers (1)

Amal
Amal

Reputation: 971

Frame by Frame animation is a AnimationDrawable not Animation. what you do is using it as animation and that is the cause of the exception there is no animation file with that name there is drawable file. to use AnimationDrawable use this code snippet

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

// 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();

for more info about AnimationDrawable please refer to the documentation

Upvotes: 3

Related Questions