标签:android
有没有办法动画文本颜色变化(从任何颜色变为白色)?
我想出的唯一变体是将两个文本视图(具有相同的文本)放在一个地方,然后淡化顶部的一个,因此底部的一个(具有白色)将变得可见.
附:我废弃了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;
}
}
}
解决方法:
虽然我没有找到一个完全不同的方法,但我尝试使用TextSwitcher(使用淡入淡出动画)来创建颜色变化效果. TextSwitcher是一种ViewSwitcher,它可以在两个(内部)TextView之间进行动画制作.您是否在不知不觉中手动实施了相同的系统? ;)它为您管理更多的过程,因此您可能会发现它更容易使用(特别是如果您想尝试更多涉及的动画).我会创建TextSwitcher的新子类和一些方法,例如setColour()可以设置新颜色然后触发动画.然后可以将动画代码移动到主应用程序之外.
>确保在放入切换台的两个TextView上保留一个句柄
>更改其他TextView的颜色并调用setText()以在它们之间设置动画
如果您已经在使用ViewSwitcher,那么我认为没有更简单的方法来实现它.
标签:android