有没有一种方法可以将 BackgroundColorSpan 设置为 SpannableString?
Is there a way a way to animate setting BackgroundColorSpan to SpannableString?
当我向该 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);
}
});
但它根本没有为跨度设置背景颜色。
最后我做了@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();
当我向该 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);
}
});
但它根本没有为跨度设置背景颜色。
最后我做了@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();