使用ValueAnimator做一个TextView闪烁不同颜色 [英] Using a ValueAnimator to make a TextView blink different colors

查看:478
本文介绍了使用ValueAnimator做一个TextView闪烁不同颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个 ValueAnimator 来使的TextView 的文本颜色闪烁两次在两个不同的颜色,但我想创建XML中的动画。我找不到任何的例子。任何帮助将AP preciated。

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.

更新

在code以下运行完美。从黑到蓝的颜色变化,蓝黑色,在每一个反向重复序列之间的黑色变为蓝色,蓝黑色与500毫秒。我却试图让这个从一个漫画家xml文件的工作。

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);

使用XML使的TextView 的文本颜色的颜色改变,因为很多次,因为它可以在500毫秒。

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

更新 我想我需要的是关键帧以XML什么OfInt电话是做编程方式模拟。现在想这一点,但至今没有运气。

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.

推荐答案

试试这个:

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();

通过XML试试这个未经测试的方法:* RES /动画/ property_animator.xml *

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>

现在的Java code:

now in Java code:

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
R.anim.property_animator);
set.setTarget(myTextView);
set.start();

这篇关于使用ValueAnimator做一个TextView闪烁不同颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆