15

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.

CPlusPlus
  • 479
  • 5
  • 14

4 Answers4

7

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.

Advait Saravade
  • 3,029
  • 29
  • 34
Rogerabino
  • 71
  • 1
  • 3
3
public void repeat(){   
    new CountDownTimer(2000, 1000){

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

        public void onFinish() {
            transition.resetTransition();
            repeat();
        }
    }.start();
}
RBT
  • 24,161
  • 21
  • 159
  • 240
David
  • 31
  • 1
  • Please add some context around your code snippet or it might be difficult for users to understand your approach. – RBT Jun 06 '16 at 21:39
1

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

Nolesh
  • 6,848
  • 12
  • 75
  • 112
-4

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.

Reed
  • 14,703
  • 8
  • 66
  • 110
  • 1
    Looping is not working, because it blocks. This divider is part of a listitem in a listview. I execute this code when I create the view. – CPlusPlus Dec 21 '11 at 11:29
  • Try using a thread? That might be a little complicated because you can't update the UI thread from a worker thread, but it may be doable. You would have to use a Handler as well for communication. I guess I don't really know where for you to go next. – Reed Dec 21 '11 at 11:40
  • That's really to complicated I think. I thought of animations, but I found nothing comparable in the web :/ – CPlusPlus Dec 21 '11 at 11:56
  • I understand and agree. Good luck, I guess. I don't know how to use animations or `TransitionDrawable` or I might be of more help. Sorry. :) – Reed Dec 21 '11 at 12:29