Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a ValueAnimator to make a TextView blink different colors

Tags:

I want to use a ValueAnimator to make a TextView's text color blink twice between two different colors but I want to create the Animation in XML. I cannot find any examples. Any help will be appreciated.

Update

The code below works perfect. The color changes from black to blue, blue to black, black to blue, and blue to black with 500ms in between each reverse repeat. I'm however trying to get this to work from an animator xml file.

ValueAnimator colorAnim = ObjectAnimator.OfInt(objectToFlash, "textColor", (int)fromColor, (int)toColor); colorAnim.SetDuration(500); colorAnim.SetEvaluator(new ArgbEvaluator()); colorAnim.RepeatCount = 3; colorAnim.RepeatMode = ValueAnimatorRepeatMode.Reverse; 

xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"         android:propertyName="textColor"                 android:duration="500"         android:valueFrom="@color/black"         android:valueTo="@color/ei_blue"         android:repeatCount="3"         android:repeatMode="reverse" />  

Code

ValueAnimator anim = (ObjectAnimator)AnimatorInflater.LoadAnimator(Activity, Resource.Animator.blinking_text); anim.SetTarget(objectToFlash); 

Using xml causes the color of the TextView's text color to change as many times as it can within 500ms.

Update I think what I need are Keyframes to mimic in xml what the OfInt call is doing programmatically. Trying this now but no luck so far.

like image 823
wheels53 Avatar asked Mar 23 '13 01:03

wheels53


1 Answers

Try this:

private static final int RED = 0xffFF8080; private static final int BLUE = 0xff8080FF;  ValueAnimator colorAnim = ObjectAnimator.ofInt(myTextView, "backgroundColor", RED, BLUE);         colorAnim.setDuration(3000);         colorAnim.setEvaluator(new ArgbEvaluator());         colorAnim.setRepeatCount(ValueAnimator.INFINITE);         colorAnim.setRepeatMode(ValueAnimator.REVERSE);         colorAnim.start(); 

Or try this untested method via xml: *res/animator/property_animator.xml*

<set >  <objectAnimator     android:propertyName="backgroundColor"     android:duration="3000"     android:valueFrom="#FFFF8080"     android:valueTo="#FF8080FF"     android:repeatCount="-1"     android:repeatMode="reverse" /> </set> 

now in Java code:

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.anim.property_animator); set.setTarget(myTextView); set.start(); 
like image 148
M-WaJeEh Avatar answered Sep 28 '22 03:09

M-WaJeEh