Skip to content Skip to sidebar Skip to footer

Repeating Animation With Timer

I have two animations xml file, i use these in java class. I want my animation repeats 1000 msec. I write this code but when i run this program, animation not repeat. I use Timer i

Solution 1:

What about to use use Handler, see below:

privateintmSampleDurationTime=1000; // 1 secprivatebooleancontinueToRun=true;

HandlermHandler=newHandler();

mHandler.postDelayed(mRunnable, mSampleDurationTime);

where mRunnable is your task:

privatefinalRunnablemRunnable=newRunnable() {

 //...publicvoidrun() {

   // do your stuff here, like update// this block of code you going to reach every  secondif(continueToRun == true){
    mHandler.postDelayed(mRunnable, mSampleDurationTime);
   }

 }
    ...
};

First time you call postDelayed and invoke new Runnable(). After, if you want to continue, call the same method into run()

Solution 2:

why not just use handler.postDelayed(runnable, delay) instead of using a timer?

Also, try using animation1.startNow() after setting the animation on the button, and call btn.invalidate() after setting the animation to redraw the view

Post a Comment for "Repeating Animation With Timer"