0

I am trying to figure out how on earth i can start and stop animation in my Android App.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    stopOneButton = (Button) findViewById(R.id.buttonStopOne);
    stopTwoButton = (Button) findViewById(R.id.buttonStopTwo);
    stopThreeButton = (Button) findViewById(R.id.buttonStopThree);
    startButton = (Button) findViewById(R.id.buttonStart);

    firstImage = (ImageView) findViewById(R.id.imageView1);

    firstImage.setBackgroundResource(R.drawable.spin_animation);

    frameAnimation = new AnimationDrawable();
    AnimationDrawable frameAnimation = (AnimationDrawable) firstImage.getBackground();
//  frameAnimation.start();

    firstImage.post(new Starter());

    stopOneButton.setOnClickListener(this);
    stopTwoButton.setOnClickListener(this);
    stopThreeButton.setOnClickListener(this);
    startButton.setOnClickListener(this);

}

class Starter implements Runnable {

    public void run() {
        frameAnimation.start();
    }

}

This is pretty much everything i have, and im starting from scratch without binding any buttons to the functionality, but the image is not spinning around at all.

This is my XML file for the drawable:

<?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/image1" android:duration="500" />
    <item android:drawable="@drawable/image2" android:duration="500" />
    <item android:drawable="@drawable/image3" android:duration="500" />
    <item android:drawable="@drawable/image4" android:duration="500" />
 </animation-list>

More code for buttons:

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.buttonStopOne:

        break;

        case R.id.buttonStopTwo:
        break;

        case R.id.buttonStopThree:
        break;

        case R.id.buttonStart:
            frameAnimation.start();
        break;
    }
} 

Can anyone determine what im doing wrong?

JavaCake
  • 4,075
  • 14
  • 62
  • 125

1 Answers1

0

You have to apply the animation to a View. So assuming firstImage() is the animation you want to animate, call firstImage.startAnimation(frameAnimation). Then to end it call firstImage.clearAnimation().

EDIT:

Ok. Now that I see what you're doing, I'm guessing the Runnable you're using is never being called. What you can try is wait until the view has been fully inflated and start the animation in that. Something like this:

firstImage = (ImageView) findViewById(R.id.imageView1);
final AnimationDrawable frameAnimation = (AnimationDrawable) firstImage.getBackground();
ViewTreeObserver treeObserver = firstImage.getViewTreeObsver();
treeObvserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
   @Override
   public void onGlobalLayout(){
      frameAnimation.start();
   }
}

Now, I haven't tried this, but what I'm thinking will happen is when the view is fully inflated and visible, the animation will start.

Side-note: It's also a handy way to figure out when the views have their dimensions.

Community
  • 1
  • 1
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • I find that a bit confusing, does `frameAnimation.start();`not start my animation? – JavaCake Feb 22 '12 at 18:34
  • Another way to start an animation is to call `setAnimation` on the `View`, pass in the animation, then call `start()` on the animation itself. So in your case it would be `firstimage.setAnimation(frameAnimation);` somewhere in the set. Then when you want the animation to play called `frameAnimation.start()`. I'm almost certain the reason it's not animating for you is that the animation is not referencing any view. – DeeV Feb 22 '12 at 18:38
  • Oh wait. I think I see what you're doing. You're creating a drawable with an animation-list and setting it as a background. I'm sorry. This answer won't help. My other guess is your runnable is never being called. I'll edit my answer because I think I have an idea. – DeeV Feb 22 '12 at 18:51
  • Using post just adds a runnable to the View's message queue. It doesn't automatically get called. The edit in my code *should* ensure that it will always get called automatically. If not please tell me because I haven't tried. – DeeV Feb 22 '12 at 18:59
  • Thanks DeeV. But what if we instead assume that i want to implement the start and stop by button press? Then it should no longer be an issue? – JavaCake Feb 22 '12 at 19:01
  • If by button press, then implement an `onClickListener()` on the button and call `frameAnimation.start()` within that, so yes. – DeeV Feb 22 '12 at 19:08
  • Please check my last piece of code! Thats the implementation with the button. Nothing happens when i press the start button. – JavaCake Feb 22 '12 at 19:11
  • Replace `AnimationDrawable frameAnimation = (AnimationDrawable) firstImage.getBackground();` with just `frameAnimation = (AnimationDrawable) firstImage.getBackground();` and remove `frameAnimation = new AnimationDrawable();` You're not calling `start()` on the global reference. – DeeV Feb 22 '12 at 19:17
  • Thanks for your attempt! Its starting to piss me off :-) – JavaCake Feb 22 '12 at 19:40