user1157751
user1157751

Reputation: 2457

How to make Android Buttons show animation on itself when clicked

I'm trying to make a Button that will play a animation-list when pressed.

I have made a animation.xml shown here:

<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/one" android:duration="500" />
   <item android:drawable="@drawable/two" android:duration="500" />
   <item android:drawable="@drawable/three" android:duration="500" />
   <item android:drawable="@drawable/four" android:duration="500" />
</animation-list>

Also for the onClick function:

b_button.setBackgroundResource(R.drawable.animation);
AnimationDrawable frameAnimation = (AnimationDrawable) b_button.getBackground();
frameAnimation.setOneShot(true);
frameAnimation.start();

When I press the button, the animation would be played, however, for just once, after when I press it the second time, it will not play. Furthermore, how do I change the button back to it's original state? Meaning the grey default look of the button.

Any help is appreciated, Thanks in advance

Upvotes: 0

Views: 1970

Answers (1)

JohnCookie
JohnCookie

Reputation: 671

  if (frameAnimation.isRunning()) {
      frameAnimation.stop();
  } else {
      frameAnimation.stop();
      frameAnimation.start();
  }

try is this works(with OneShot and without OneShot)? The animation will stop at last frame, so you can add a in xml which drawable is the grey default look thx :)

Upvotes: 1

Related Questions