1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public class RoundBackgroundColorSpan extends ReplacementSpan {
private int bgColor;
private int textColor;
public RoundBackgroundColorSpan(int bgColor, int textColor) {
super();
this.bgColor = bgColor;
this.textColor = textColor;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
//设置宽度为文字宽度加16dp
return ((int)paint.measureText(text, start, end)+Util.px2Dp(16));
}
@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();
paint.setColor(this.bgColor);
//画圆角矩形背景
canvas.drawRoundRect(new RectF(x,
top+ Util.px2Dp(3),
x + ((int) paint.measureText(text, start, end)+ Util.px2Dp(16)),
bottom-Util.px2Dp(1)),
Util.px2Dp(4),
Util.px2Dp(4),
paint);
paint.setColor(this.textColor);
//画文字,两边各增加8dp
canvas.drawText(text, start, end, x+Util.px2Dp(8), y, paint);
//将paint复原
paint.setColor(originalColor);
}
}
|