有没有办法动画文本颜色变化(从任何颜色变为白色)?
我想出的唯一变体是将两个文本视图(具有相同的文本)放在一个地方,然后淡化顶部的一个,因此底部的一个(具有白色)将变得可见.
PS我废弃了2个TextViews的变体,因为它看起来很奇怪(边缘不平滑,因为我在屏幕上有很多这样的元素,它真的落后于滚动).我做了什么,是一个疯狂的黑客,使用Thread和setTextColor(也强制重绘textview)来动画.
由于我只需要2种颜色变化(从红色到白色,从绿色到白色),我硬编码了它们之间的值和所有过渡颜色.所以这是它的样子:
public class BlinkingTextView extends TextView {
public BlinkingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void animateBlink(final boolean red) {
if (animator != null) {
animator.drop();
}
animator = new Animator(this, red);
animator.start();
}
public void clearBlinkAnimation() {
if (animator != null) {
animator.drop();
}
}
private Animator animator;
private final static class Animator extends Thread {
public Animator(final TextView textView, final boolean red) {
this.textView = textView;
if (red) {
SET_TO_USE = RED;
} else {
SET_TO_USE = GREEN;
}
}
private TextView textView;
private final int[] SET_TO_USE;
private final static int[] RED = {
-2142396,
-2008754,
-1874854,
-1740697,
-1540490,
-1405563,
-1205099,
-1004634,
-804170,
-669243,
-469036,
-334879,
-200979,
-67337,
-1
};
private final static int[] GREEN = {
-6959821,
-6565826,
-6106293,
-5646758,
-5055894,
-4530309,
-3939444,
-3283042,
-2692177,
-2166592,
-1575728,
-1116193,
-656660,
-262665,
-1
};
private boolean stop;
@Override
public void run() {
int i = 0;
while (i < 15) {
if (stop) break;
final int color = SET_TO_USE[i];
if (stop) break;
textView.post(new Runnable() {
@Override
public void run() {
if (!stop) {
textView.setTextColor(color);
}
}
});
if (stop) break;
i++;
if (stop) break;
try {
Thread.sleep(66);
} catch (InterruptedException e) {}
if (stop) break;
}
}
public void drop() {
stop = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Rom*_*nok 48
您可以使用新的Property Animation Api进行颜色动画:
Integer colorFrom = getResources().getColor(R.color.red);
Integer colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
textView.setTextColor((Integer)animator.getAnimatedValue());
}
});
colorAnimation.start();
Run Code Online (Sandbox Code Playgroud)
为了与Android 2.x向后兼容,请使用Jake Wharton的Nine Old Androids库.
ozn*_*nus 32
最简单的解决方案是使用Object Animators:
ObjectAnimator colorAnim = ObjectAnimator.ofInt(yourTextView, "textColor",
Color.RED, Color.GREEN);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.start();
Run Code Online (Sandbox Code Playgroud)
虽然我还没有找到完全不同的方法,但我尝试使用 TextSwitcher (带有淡入淡出动画)来创建颜色变化效果。A是一种字面上在两个(内部)s 之间进行动画处理的TextSwitcher类型。您是否在不知不觉中手动实施了同一个系统?;) 它为您管理更多的过程,因此您可能会发现它更容易使用(特别是如果您想尝试更多复杂的动画)。我将创建新的子类和一些方法,例如可以设置新颜色然后触发动画。然后可以将动画代码移至主应用程序之外。ViewSwitcherTextViewTextSwitchersetColour()
TextView放入切换器的两个开关的手柄TextView并setText()在它们之间调用动画如果您已经在使用,ViewSwitcher那么我认为没有更简单的方法来实现这一点。
| 归档时间: |
|
| 查看次数: |
19048 次 |
| 最近记录: |