final View view = inflater.inflate(R.layout.example, container, false); Context context = getActivity().getApplicationContext(); final int initialColor = ContextCompat.getColor(context, R.color.primary); final int finalColor = ContextCompat.getColor(context, R.color.accent); ValueAnimator anim = ValueAnimator.ofFloat(0, 1); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float position = animation.getAnimatedFraction(); int blended = blendColors(initialColor, finalColor, position); view.setBackgroundColor(blended); } }); anim.setRepeatCount(Animation.INFINITE); anim.setRepeatMode(Animation.REVERSE); anim.reverse(); anim.setDuration(2000).start(); .... private int blendColors(int from, int to, float ratio) { final float inverseRatio = 1f - ratio; final float r = Color.red(to) * ratio + Color.red(from) * inverseRatio; final float g = Color.green(to) * ratio + Color.green(from) * inverseRatio; final float b = Color.blue(to) * ratio + Color.blue(from) * inverseRatio; return Color.rgb((int) r, (int) g, (int) b); }//метод для того, чтобы использовались только initialColor и finalColor, а не все цвета 'радуги' 

Update: Laid out the full function of changing the color of the view , suddenly someone thread will come in handy.

PS: Connect compile 'com.nineoldandroids:library:2.4.0' so that this function works the same everywhere

This function smoothly changes the color of the View in one direction only from initialColor to finalColor and abruptly redrawn.

How to make it initialColor smoothly from finalColor to initialColor ?

  • one
    Try anim.setRepeatCount(Animation.REVERSE); - Yuriy SPb
  • @YuriSPb nope, everything is also steep and not infinite. And for some reason, only three times ... - iFr0z
  • one
    And if so: anim.setRepeatCount(Animation.INFINITE); anim.setRepeatMode(Animation.REVERSE); anim.setRepeatCount(Animation.INFINITE); anim.setRepeatMode(Animation.REVERSE); ? - JuriySPb
  • @YuriySPb Now it works fine and beautiful !!! Thank you very much! True, the strange thing is that when I wrote anim.setRepeatCount(Animation.INFINITE); anim.reverse(); anim.setRepeatCount(Animation.INFINITE); anim.reverse(); , then so fine does not work. I understand reverse(); Animation.REVERSE; reverse(); Animation.REVERSE; - are these different things? - iFr0z
  • one
    Yes, these are different things. The first shows in which direction to play the repeated animation at the next iteration, and the second - plays the animation in reverse order from the moment the method is called. It does not have to affect repetition, it seems. Those. one-time action. - JuriySPb

1 answer 1

In addition to setting an infinite repetition of animation, you can set the type of repetition animation in REVERSE

 anim.setRepeatCount(Animation.INFINITE); anim.setRepeatMode(Animation.REVERSE);