The string in my TextView is divided into spans of three letters in each (Triplets) in runtime as I add more letters to this TextView. And I set four different background colors to those triplets cyclically:
void color(TextView textView) {
String sequenceColored = textView.getText().toString();
SpannableString ss = new SpannableString(sequenceColored);
int iter = 0;
if (textView.getId() == R.id.sequence) {
for (int i = 0; i < sequenceColored.length(); i += 3, iter++) {
if (iter == 0) {
ss.setSpan(new BackgroundColorSpan(Color.argb(123, 255, 136, 0)), i, i + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (iter == 1) {
ss.setSpan(new BackgroundColorSpan(Color.argb(123, 255, 187, 51)), i, i + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (iter == 2) {
ss.setSpan(new BackgroundColorSpan(Color.argb(123, 0, 153, 204)), i, i + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (iter == 3) {
ss.setSpan(new BackgroundColorSpan(Color.argb(123, 170, 102, 204)), i, i + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
iter = -1;
}
}
}
}
So, the question is: is it possible to animate this background color change, make it slowly and nicely appear from no background color?
SpannableString is not a View, so I can't animate it traditionally, right?
Update
I tried to set this animation up by executing the folowing code inside the first inner if:
ValueAnimator animation = ValueAnimator.ofInt(0, 123);
animation.start();
final int finalI = i;
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ss.setSpan(new BackgroundColorSpan(Color.argb((int)animation.getAnimatedValue(), 255, 136, 0)), finalI, finalI + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
});
But it does not set background color to the span at all.