有没有一种方法来动画文本颜色变化(从任何颜色变为白色)?文本颜色动画
我想出的唯一变种是将两个文本视图(使用相同的文本)放在一个地方,然后淡出顶部的一个,所以底部的一个(具有白色)将变得可见。
P.S.我放弃了2个TextView的变体,因为它看起来很奇怪(边缘不平滑,因为屏幕上有很多这样的元素,所以它实际上滞后于滚动)。我做了什么,是一个疯狂的黑客,使用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;
}
}
}
谢谢。我实际上并没有实现我所说的方法(只是想到了它)。非常感谢,我会尝试你的方法:) – 2011-01-11 13:47:04