1

I have a TextView which changes color based on value given by a SeekBar:

if ((inOne >= 70 && inOne <= 90) && (inTwo >= 40 && inTwo <= 60)) {
            tvRes.setText("LOW");
            tvRes.setBackgroundColor(Color.parseColor("#73539E"));
            rlO.setBackgroundColor(Color.parseColor("#9973539E"));
        }
        if ((inOne >= 70 && inOne <= 90) && (inTwo >= 61 && inTwo <= 80)) {
            tvRes.setText("IDEAL");
            tvRes.setBackgroundColor(Color.parseColor("#679800"));
            rlO.setBackgroundColor(Color.parseColor("#99679800"));
        }
        if ((inOne >= 70 && inOne <= 90) && (inTwo >= 81 && inTwo <= 90)) {
            tvRes.setText("PRE-HIGH");
            tvRes.setBackgroundColor(Color.parseColor("#967400"));
            rlO.setBackgroundColor(Color.parseColor("#99967400"));
        }

inOne and inTwo are SeekBar values.

It works 100% without any issue. What I was looking to do, instead of just changing the color, I want it to animate fade from one color to the next. How can I achieve it?

I was looking at this example: SO Example wasn't too clear on it.

Community
  • 1
  • 1
Si8
  • 9,141
  • 22
  • 109
  • 221

1 Answers1

1

I recently made a helper class to animate between colours, and you can find it here. Perhaps it can be of some use?

As for how to use it, it is very similar in usage to other Animators:

ColorAnimator.ofColor(view, "color", Color.BLACK, Color.WHITE).start();

for a background color, use a ViewBackgroundWrapper (EDIT or the new convenience method):

ColorAnimator.ofBackgroundColor(tvRes, Color.parseColor("#679800")).start();

This would animate the first background color in your code snippet.

Alex Curran
  • 8,818
  • 6
  • 49
  • 55
  • Wow... might just work... Can I use color.xml color names instead of the pre defined colors? Can you provide an example from the code I provided? thanks. – Si8 Jan 02 '14 at 01:55
  • Yup, you can use any color returned as an int (from resources, or Color.parse()). Added an example. – Alex Curran Jan 02 '14 at 11:00
  • 1
    no, you're not doing something wrong - I can't get it working either! Give me a while, and I'll get back to you :) – Alex Curran Jan 02 '14 at 19:50
  • PHEW :)... I thought I was doing it wrong... lol... I will wait! :) – Si8 Jan 02 '14 at 20:13
  • Any idea when you might have this completed? :)P – Si8 Jan 24 '14 at 17:32
  • Hi... Any update on if you have it working? I will accept your answer because you helped. – Si8 Jan 31 '14 at 19:16