0

I have one drawable animation from png, and android:oneshot="true" because I don't want the animation play constantly, but only when I activate it. Problem is it plays only once and when I try myAnimation.play(); it doesn't play again.

I've tried to myAnimation.stop(); and play again but it makes the animation stop before animation ends.

Same thing happens when I start the animation with myAnimation.run();, though I don't know the difference.

//in onCreate() method
imageView = findViewById(R.id.imageView);
imageView.setBackgroundResource(R.drawable.animation_drawable);
myAnimation = (AnimationDrawable) imageView.getBackground();

//Triggers in somewhere else in a thread
myAnimation.start();
//animation_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/animation_drawable" android:oneshot="true">
    <item android:drawable="@drawable/kapali" android:duration="0"/>
    <item android:drawable="@drawable/acik" android:duration="500"/>
    <item android:drawable="@drawable/kapali" android:duration="0"/>
</animation-list>

zarez
  • 65
  • 1
  • 9

1 Answers1

3

In your animation_drawable.xml you have android:oneshot="true", remove it OR change it to false.

Try using

myAnimation.setOneShot(false);

before the start() method.

And when you want to stop the animation use

myAnimation.stop();

For your case, after stopping the animation(OR set oneshot=true), to restart the animation, use

myAnimation.setVisible(/*visible=*/true,/*restart=*/true);

You can check the documentation for this method here.

Akhil Soman
  • 2,077
  • 2
  • 17
  • 30
  • When I `myAnimation.setOneShot(false);` or change it to false in the xml file, animation plays continuosly which I don't want. I need the animation to play once and stop when I call it. It does play and stop but only one time can't play the animation later. – zarez Aug 16 '19 at 12:13
  • Is there any listener to detect when animation ends for the drawable animations? Then I should stop the animation after it ends, but now I can't detect when it ends. I also tried to `TimeUnit.MILLISECONDS.sleep(500);` because myAnimation's duration is 500ms but during that time everything has stopped. – zarez Aug 16 '19 at 12:29
  • 2
    Ok, it's solved. Thank you very much. I added `myAnimation.setVisible(true, true);` and before `myAnimation.start();` I did `myAnimation.setOneShot(true);` but in the xml `android:oneshot=false`. I was trying to solve this since 2 days ago along with other stuff. Thank you. – zarez Aug 16 '19 at 12:33