Android设置TextView高亮字体、字体颜色渐变
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b5a72cf99db3cc34ff6ee1b15a96cbdc.png#pic_center)
渐变效果图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/deae58a01c3880a016ab61475f02a27f.png#pic_center)
实现的工具类TextFontUtils
:
public class TextFontUtils {
public static String HIGHLIGHT_COLOR = "#0fc264";
public static void setHighlightFont(String regexStr, String targetStr, TextView textView) {
targetStr = targetStr.replaceAll(regexStr, "<font color='" + HIGHLIGHT_COLOR + "'>" + regexStr + "</font>");
textView.setText(Html.fromHtml(targetStr));
}
public static void setGradientFont(TextView textView, String startColor, String endColor){
LinearGradient gradient = new LinearGradient(0, 0, 0,
textView.getPaint().getTextSize(),
Color.parseColor(startColor), Color.parseColor(endColor),
Shader.TileMode.CLAMP);
textView.getPaint().setShader(gradient);
textView.invalidate();
}
}