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 ?
anim.setRepeatCount(Animation.REVERSE);- Yuriy SPb ♦anim.setRepeatCount(Animation.INFINITE); anim.setRepeatMode(Animation.REVERSE);anim.setRepeatCount(Animation.INFINITE); anim.setRepeatMode(Animation.REVERSE);? - JuriySPb ♦anim.setRepeatCount(Animation.INFINITE); anim.reverse();anim.setRepeatCount(Animation.INFINITE); anim.reverse();, then so fine does not work. I understandreverse(); Animation.REVERSE;reverse(); Animation.REVERSE;- are these different things? - iFr0z