How can I repeat a transition forever?

爷,独闯天下 提交于 2019-12-23 07:28:00

问题


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.


回答1:


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




回答2:


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.




回答3:


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

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

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



回答4:


  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.



来源:https://stackoverflow.com/questions/8582410/how-can-i-repeat-a-transition-forever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!