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

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

问题描述

我想使用 ValueAnimator 使 TextView 的文本颜色在两种不同颜色之间闪烁两次,但我想在 XML 中创建动画.我找不到任何例子.任何帮助将不胜感激.

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.

更新

下面的代码完美无缺.颜色从黑色变为蓝色、蓝色变为黑色、黑色变为蓝色以及蓝色变为黑色,每次反向重复之间间隔 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" /> 

代码

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/animator/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 代码中:

now in Java code:

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

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

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