How can I repeat a transition forever?
public void repeat(){
new CountDownTimer(2000, 1000){
public void onTick(long millisUntilFinished){
transition.startTransition(2000);
}
public void onFinish() {
transition.resetTransition();
repeat();
}
}.start();
}
I know that this question is old, but I would like to help other users with other possible solution.
AnimationDrawable
is the correct way when you have more than 2 images, but when you have only two images, you can use TransitionDrawable
as well, as you said. I'll show you how to do it.
In first time, I'll use a timer in my onCreate()
.
Timer MyTimerImage = new Timer();
Then, I'll create a MyTimerTask class (within the parent Activity class) to run my own code.
public class MyTimerTask extends TimerTask {
LinearLayout myIDLinearLayout= (LinearLayout) findViewById(R.id.myIDofLinearLayout);
TransitionDrawable MyTrans = (TransitionDrawable) myIDLinearLayout.getBackground();
int i = 0;
@Override
public void run() {
runOnUiThread(new Runnable(){
@Override
public void run() {
i++;
if (i%2==0) { //
MyTrans.startTransition(myTransitionTimeinms);
}else{
MyTrans.reverseTransition(myTransitionTimeinms);
}
}});
}
}
And finally, I'll create a MyTimerTask variable to run my timer. This code goes below the creation of the Timer().
MyTimerTask MyTimer = new MyTimerTask();
MyTimerImage.schedule(MyTimer, myDelay, myPeriod);
In this case, schedule method has 3 parameters: first one indicates our TimerTask task, second one is the delay to run the TimerTask in ms, and the third one indicates every period we want to run the code.
Don't forget to include your TransitionFile.xml file in your android:background
.
I hope that it can help to someone.