Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I repeat a transition forever?

I have a transition looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/divider"/>
    <item android:drawable="@drawable/divider_active"/>

</transition>

and Code looking like this:

View divider = v.findViewById(R.id.divider);
if (divider != null) {
  TransitionDrawable transition = (TransitionDrawable) divider.getBackground();
  transition.startTransition(2000);
}

My problem is, I don't know how to repeat this transition forever, so I can create a pulsing effect.

Edit:

To make things clear: The code gets executed when creating a view (listitem), so loops are no solution.

like image 890
CPlusPlus Avatar asked Dec 20 '11 21:12

CPlusPlus


People also ask

How do I add infinite animation to CSS?

To create infinite animations in CSS, we will use the property animation-iteration-count: infinite;.


1 Answers

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.

like image 106
Rogerabino Avatar answered Oct 24 '22 16:10

Rogerabino