我想使用 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/