CPlusPlus
CPlusPlus

Reputation: 479

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.

Upvotes: 15

Views: 6624

Answers (4)

Reed
Reed

Reputation: 14974

EDIT: This is a bad answer, but I'm leaving it in case the discussion is helpful to anyone. I probably still deserve downvotes lol

      View divider = v.findViewById(R.id.divider);
      boolean loop = true;
          while (loop) {
              TransitionDrawable transition = (TransitionDrawable) divider.getBackground();
              transition.resetTransition(); //I don't really know if this is necessary
              transition.startTransition(2000);
              //you'll want to stop SOME time.
              if (wantToStop){
                  loop = false
              }
          }

That may or may not work properly. I say that because during the 2 seconds transition, the while loop will continue to loop, startTransition would get called again, and again, and again, and again. You could use a condiitonal statement to check the current time and check if the last started time is less than 2000.

This may not be the best, but a while loop is a good place to start.

If you need, I could attempt to give a better code example that will manage the while loop better, but you should try it first.

Upvotes: -4

Rogerabino
Rogerabino

Reputation: 71

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.

Upvotes: 7

David
David

Reputation: 31

public void repeat(){   
    new CountDownTimer(2000, 1000){

         public void onTick(long millisUntilFinished){
             transition.startTransition(2000);
        }

        public void onFinish() {
            transition.resetTransition();
            repeat();
        }
    }.start();
}

Upvotes: 3

Nolesh
Nolesh

Reputation: 7018

I think you should use AnimationDrawable instead of TransitionDrawable: http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

Upvotes: 1

Related Questions