android - 有没有办法将 BackgroundColorSpan 设置为 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 不是视图,所以我不能按传统方式对其进行动画处理,对吧?
更新
我尝试通过在第一个内部执行以下代码来设置此动画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();
推荐阅读
- javascript - 填写用户名和姓氏并转到下一页
- javascript - 订阅返回未定义的变量
- firebase - Java.Lang.NullPointerException:尝试调用虚拟方法'void com.google.firebase.auth.FirebaseAuth.zza
- r - 在长格式中找到不同的因子值
- sql - 如何在加入/下推到外部服务器之前强制评估子查询
- react-native - 如何为每个屏幕多堆栈导航创建自定义标题选项反应原生?
- python - 如何声明一个目标函数(最小化)及其对 Python 的上限和下限的约束?
- python - 标记保留在引号内的字符串
- identityserver4 - 接收错误:方法“FindIdentityResourcesByScopeNameAsync”没有实现
- c# - System.Convert.ToDouble(decimal) 给出两个不同的答案