有沒有一種方法來動畫文本顏色變化(從任何顏色變爲白色)?文本顏色動畫
我想出的唯一變種是將兩個文本視圖(使用相同的文本)放在一個地方,然後淡出頂部的一個,所以底部的一個(具有白色)將變得可見。
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