android - 使用 ValueAnimator 使 TextView 闪烁不同的颜色

标签 android animation xamarin.android

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

更新

下面的代码完美运行。颜色从黑色变为蓝色、蓝色变为黑色、黑色变为蓝色、蓝色变为黑色,每次反向重复之间的间隔为 500 毫秒。然而,我试图让这个从动画 xml 文件中工作。

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 毫秒内尽可能多地更改。

更新 我认为我需要的是在 xml 中模仿 OfInt 调用以编程方式执行的关键帧。现在正在尝试,但到目前为止没有运气。

最佳答案

试试这个:

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*

<set >

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

现在在 Java 代码中:

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

关于android - 使用 ValueAnimator 使 TextView 闪烁不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15582434/

相关文章:

android - libspotify 在 Android 上因 SIGSEGV 而失败

python - 使用 matplotlib 可视化数据

iphone - 动画过渡在横向方向上无法正常工作

android - xamarin android 推送通知只工作一次

c# - 使用 Azure SQL Server 创建 Web API

android - 如何在 Android 中为自定义 View 绘制边框?

java - 使用 Hashmap 调试 foreach 时出错

android - 如何在点击下一个键盘键时将控制权传递给android中的下一个edittext?

swift - 如何从容器 View 中为父 View Controller 中的约束更改设置动画? ( swift 4)

Android 应用程序中的 C# .dll 库