当我向此TextView中添加更多字母时,我的TextView中的字符串在运行时中每个(小标题)被分成三个字母的范围。我周期性地为这些三胞胎设置了四种不同的背景色:
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;
}
}
}
}
因此,问题是:是否可以为这种背景颜色变化设置动画,使其在没有背景颜色的情况下缓慢而美观地显示?
SpannableString不是View,所以我传统上不能设置动画,对吧?
更新
我尝试通过在第一个内部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);
}
});
但是它根本没有将背景色设置为跨度。
答案 0 :(得分:0)
最后,我做了@CommonsWare建议的内容。
首先,我创建了一个自定义MutableBackgroundColorSpan
:
public class MutableBackgroundColorSpan extends BackgroundColorSpan {
private int mAlpha = 255;
private int mBackgroundColor;
public MutableBackgroundColorSpan(int alpha, int color) {
super(color);
mAlpha = alpha;
mBackgroundColor = color;
}
public MutableBackgroundColorSpan(Parcel src) {
super(src);
mBackgroundColor = src.readInt();
mAlpha = src.readInt();
}
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(mBackgroundColor);
dest.writeFloat(mAlpha);
}
@Override
public void updateDrawState(TextPaint ds) {
ds.bgColor = getBackgroundColor();
}
/**
* @param alpha from 0 to 255
*/
public void setAlpha(int alpha) {
mAlpha = alpha;
}
public void setBackgroundColor(int backgroundColor) {
mBackgroundColor = backgroundColor;
}
public float getAlpha() {
return mAlpha;
}
@Override
public int getBackgroundColor() {
return Color.argb(mAlpha, Color.red(mBackgroundColor), Color.green(mBackgroundColor), Color.blue(mBackgroundColor));
}
}
然后,我初始化了一个自定义Property
private static final Property<MutableBackgroundColorSpan, Integer> MUTABLE_BACKGROUND_COLOR_SPAN_FC_PROPERTY =
new Property<MutableBackgroundColorSpan, Integer>(Integer.class, "MUTABLE_BACKGROUND_COLOR_SPAN_FC_PROPERTY") {
@Override
public void set(MutableBackgroundColorSpan span, Integer value) {
span.setBackgroundColor(value);
}
@Override
public Integer get(MutableBackgroundColorSpan span) {
return span.getBackgroundColor();
}
};
执行其功能的实际代码是:
MutableBackgroundColorSpan span = new MutableBackgroundColorSpan(123, Color.WHITE);
ss.setSpan(span, i, i + 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(span, MUTABLE_BACKGROUND_COLOR_SPAN_FC_PROPERTY, Color.WHITE, Color.rgb(r, g, b));
objectAnimator.setDuration(100);
objectAnimator.setEvaluator(new ArgbEvaluator());
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//refresh
textView.setText(ss);
}
});
objectAnimator.start();