我可以在TextView中设置一个单个字符来闪烁吗?
使用“spannable”我可以设置颜色,字体重量等但我没有发现任何关于闪烁的信息。
“动画”AFAIK适用于整个视图。
建议任何人?
答案 0 :(得分:3)
您可以开始使用该自定义类:
class FCS extends ForegroundColorSpan implements TimeAnimator.TimeListener {
private TextView tv;
private int[] colors;
private int color;
TimeAnimator animator;
ArgbEvaluator evaluator;
public FCS(TextView tv, int[] colors) {
super(colors[0]);
this.tv = tv;
this.colors = colors;
animator = new TimeAnimator();
animator.setTimeListener(this);
evaluator = new ArgbEvaluator();
animator.start();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(color);
}
@Override
public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) {
float sin = (float) (Math.sin(Math.PI * totalTime / 1000f));
float fraction = sin * sin;
// float fraction = (float) (-Math.cos(2*Math.PI * totalTime / 1000f) + 1) / 2f;
color = (int) evaluator.evaluate(fraction, colors[0], colors[1]);
tv.invalidate();
if (totalTime > 20000) {
animator.end();
}
}
}
测试代码(将其放在Activity#onCreate
方法中):
TextView tv = new TextView(this);
tv.setTextSize(40);
int[] colors = {
tv.getCurrentTextColor(),
Color.RED,
};
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append("Poeta de ");
int start = ssb.length();
ssb.append("magnum stella, dignus sectam!");
ssb.setSpan(new FCS(tv, colors), start, start+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(ssb);
setContentView(tv);