1

Colorfade

I am looking for transition animation that:

  • Fades between 3 (or more).

  • Repeats

  • And can be applied to RelativeLayout's background or LinearLayout's bacground

I have tried:

TransitionDrawable and ColorDrawable:

 relativeLayout = (RelativeLayout)findViewById(R.id.screen1);
    ColorDrawable[] color = {new ColorDrawable(Color.parseColor("#7cc57a")), new ColorDrawable(Color.parseColor("#CAC132")) , new ColorDrawable((Color.parseColor("#E79893")))};
    TransitionDrawable trans = new TransitionDrawable(color);

    relativeLayout.setBackgroundDrawable(trans);
    trans.startTransition(7000);

But it only fades between two color and don't repeat...

Is there any code that can solve this? Or should I add some whenCompleted()-method?

1 Answers1

0

Example app and ColorFader class created to this purpose ( animate between many colors ) is available here - https://github.com/maciejsikora/ColorFader.

Simpler solution for this problem but only on 3 colors:

public class ColorChanger {


    private int from_color;
    private int to_color;
    private int currentColor;
    private int interval=1000;//default iterval
    private ValueAnimator colorAnimation;
    private int[] colors=new int[3];

    public ColorChanger(int color1, int color2, int color3){


        colors[0]=color1;
        colors[1]=color2;
        colors[2]=color3;

        this.currentColor=0;

        from_color=colors[0];
        to_color=colors[1];

    }


    public ColorChanger run(final View view){




        colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), from_color, to_color);
        colorAnimation.setDuration(this.interval);

        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
            view.setBackgroundColor((Integer) animator.getAnimatedValue());
        }

        });

        colorAnimation.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {


            from_color=colors[currentColor];



            if (currentColor+1>=colors.length){

                currentColor=0;

            }else{
                currentColor++;
            }

            to_color=colors[currentColor];

            //run again
            run(view);

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

           }
        });


        colorAnimation.start();

        return this;

    }

    public void setIntervalMs( int ms){

        this.interval=ms;

    }

}

Example usage:

ColorChanger colorChanger = new ColorChanger(getResources().getColor(R.color.red),
            getResources().getColor(R.color.blue),
            getResources().getColor(R.color.green));

colorChanger.run(findViewById(R.id.content));
Maciej Sikora
  • 19,374
  • 4
  • 49
  • 50