老样子,还是延续之前的MonkeyLei:Android关键字边框/背景、关键字高亮 - 完善版本 ,产品需求需要设置关键字背景是渐变效果。(当然也有人说了,可以设置图片方式或者其他方式,都可以啦。。)
直接基于之前的ReplacementSpan修改就行 - 重点是LinearGradient和paint.setShader(linearGradient)的使用
RoundBackgroundGradiantColorSp.java
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.style.ReplacementSpan;
/**
* span包含三部分:shape、文字和距离其他文字的空白
*/
public class RoundBackgroundGradiantColorSp extends ReplacementSpan {
private LinearGradient bgColor;
private int textColor;
private int textSize;
private int mSize, diffSize;
private int radius;
private float originTextH, nowTextH, diffH;
public RoundBackgroundGradiantColorSp(LinearGradient linearGradient, int textColor, int textSize, int radius) {
super();
this.bgColor = linearGradient;
this.textColor = textColor;
this.textSize = textSize;
this.radius = radius;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
//我们需要区分一点,关键字绘制前我们控件文本大小是设置了的。而我们想要关键字不一样大小,
//则需要获取原始大小,然后根据关键字大小来计算边框大小,位置,字体绘制位置等
originTextH = (paint.descent() - paint.ascent());
// 是否需要保证关键字最多为文本大小?
// textSize = textSize > (int) paint.getTextSize() ? (int) paint.getTextSize() : textSize;
// This is important...后续计算均是以我们设置了关键字大小计算的(不然你绘制的时候会想不清楚位置)
paint.setTextSize(textSize);
//绘制整体往上平移,以文本顶部为准
nowTextH = (int) (paint.descent() - paint.ascent());
diffH = (originTextH - nowTextH)/2 - 4; // 平移后超过多了点,少平移一些
// 关键字后面增加一定间距,太挤了不好!增加了间距之后,我们绘制的过程需要减回来,不然边框就绘制出去了!
diffSize = (int) paint.measureText(" ");
return (mSize = (int) (paint.measureText(text, start, end )) + diffSize);
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
int originalColor = paint.getColor();
float defaultStrokeWidth = paint.getStrokeWidth();
Shader defaultShader = paint.getShader();
//绘制渐变矩形
paint.setShader(bgColor);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
//画圆角矩形背景
RectF oval = new RectF(
x + 2.5f,
y + paint.ascent() - 2 - diffH,
x + mSize + radius * 2 - diffSize,
y + paint.descent() + 2 - diffH);
//设置文字背景矩形,x为span其实左上角相对整个TextView的x值,y为span左上角相对整个View的y值。paint.ascent()获得文字上边缘,paint.descent()获得文字下边缘
canvas.drawRoundRect(oval, radius, radius, paint);//绘制圆角矩形,第二个参数是x半径,第三个参数是y半径
// 恢复Shader,不影响字体绘制
paint.setShader(defaultShader);
//画文字
paint.setColor(this.textColor);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(textSize);
paint.setStrokeWidth(defaultStrokeWidth);
canvas.drawText(text, start, end, x + radius, y - diffH, paint);
//将paint复原
paint.setColor(originalColor);
}
注意Shader的恢复,不然字体绘制会不正常!
使用:
/**
* @param color 关键字边框颜色
* @param text 文本
* @param keyword 关键字
* @return
*/
public static SpannableString getGradiantKeyWord(LinearGradient linearGradient, int color, String text, int keySize, String keyword) {
SpannableString s = new SpannableString(text);
Pattern p = Pattern.compile(keyword);
Matcher m = p.matcher(s);
//while (m.find()) {
if (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new RoundBackgroundGradiantColorSp(linearGradient, color, DensityUtil.dip2px(keySize), 6), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return s;
}
设置文本关键字:
//绘制圆角渐变矩形
LinearGradient linearGradient = new LinearGradient(
0, 0, 100, 50,
Color.parseColor("#ff3a91fd"),
Color.parseColor("#ff29b3ff"),
Shader.TileMode.CLAMP);
fta_titleBigTv.setText(HighLightKeyWordUtil.getGradiantKeyWord(
linearGradient, //渐变背景颜色
Color.parseColor("#ffffffff"), //字体颜色
topicListBean.getName().startsWith("精选专题") ? topicListBean.getName() : ("精选专题" + topicListBean.getName()),
13,
"精选专题"));
差不多了。。效果:
至于LinearGradient相关用法,需要的话可以深入去使用实践下,应该问题不大!
我之前有关于Shader渐变的一些总结,可以去参考入门,然后深入。
找个他人的参考: Android开发之渐变色