public class StringStyleUtils {
public static SpannableString format(Context context, String text, int style) {
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(new TextAppearanceSpan(context, style), 0, text.length(),
0);
return spannableString;
}
}
mTextSwitcher.setFactory(() -> {
TextView textView = new TextView(this);
textView.setTextAppearance(this, R.style.WebTitle);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.postDelayed(() -> textView.setSelected(true), 1738);
return textView;
});
mTextSwitcher.setInAnimation(this, android.R.anim.fade_in);
mTextSwitcher.setOutAnimation(this, android.R.anim.fade_out);
if (mTitle != null) setTitle(mTitle);
}设置textview跑马灯效果,在style中设置具体值。
public class KeywordUtil {
/**
* 关键字高亮变色
*
* @param color
* 变化的色值
* @param text
* 文字
* @param keyword
* 文字中的关键字
* @return
*/
public static SpannableString matcherSearchTitle(int color, String text,
String keyword) {
SpannableString s = new SpannableString(text);
Pattern p = Pattern.compile(keyword);
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(color), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return s;
}
/**
* 多个关键字高亮变色
*
* @param color
* 变化的色值
* @param text
* 文字
* @param keyword
* 文字中的关键字数组
* @return
*/
public static SpannableString matcherSearchTitle(int color, String text,
String[] keyword) {
SpannableString s = new SpannableString(text);
for (int i = 0; i < keyword.length; i++) {
Pattern p = Pattern.compile(keyword[i]);
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(color), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return s;
}
}