忘了是看谁的博客 大致写法都差不多
复制代码
1
2
3
4
5SpannableString string = new SpannableString(" " + item.getContent()); VerticalImageSpan imageSpan = new VerticalImageSpan(mContext, R.drawable.icon_guang_hot); string.setSpan(imageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); contentText.setText(string);
复制代码
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
35
36
37
38
39
40
41
42
43
44
45/** * 图文拼接居中显示 * @author: wangben * @date: 2020/8/13 */ public class VerticalImageSpan extends ImageSpan { public VerticalImageSpan(Context context, int resourceId) { super(context, resourceId); } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { Drawable b = getDrawable(); canvas.save(); int transY = 0; //获得将要显示的文本高度 - 图片高度除2 = 居中位置+top(换行情况) transY = ((bottom - top) - b.getBounds().bottom) / 2 + top; //偏移画布后开始绘制 canvas.translate(x, transY); b.draw(canvas); canvas.restore(); } @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { Drawable d = getDrawable(); Rect rect = d.getBounds(); if (fm != null) { Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt(); //获得文字、图片高度 int fontHeight = fmPaint.bottom - fmPaint.top; int drHeight = rect.bottom - rect.top; int top = drHeight / 2 - fontHeight / 4; int bottom = drHeight / 2 + fontHeight / 4; fm.ascent = -bottom; fm.top = -bottom; fm.bottom = top; fm.descent = top; } return rect.right; } }
复制代码
1
2
3
4
5
6/** 使用方法 * 在自定义的 LinearGradientFontSpan 中 text不会draw 添加TextView.BufferType.SPANNABLE */ mTvPrice.setText(TextColorGradientUtlis.getGradientSpan("10", Color.BLUE, Color.RED, false), TextView.BufferType.SPANNABLE)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/** * @author: wangben * @date: 2020/9/2 * 工具类 */ object TextColorGradientUtlis { //默认上到下 isLeftToRight 为true 左到右 @JvmStatic fun getGradientSpan(string: String?, startColor: Int, endColor: Int, isLeftToRight: Boolean): SpannableStringBuilder? { val spannableStringBuilder = SpannableStringBuilder(string) val span = LinearGradientFontSpan(startColor, endColor, isLeftToRight) spannableStringBuilder.setSpan(span, 0, spannableStringBuilder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) // 若有需要可以在这里用SpanString系列的其他类,给文本添加下划线、超链接、删除线...等等效果 return spannableStringBuilder } }
复制代码
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58/** * @ClassName: LinearGradientFontSpan * @Description: 文字渐变的Span类 * @Author: wangben * @date: 2020/9/2 * 这段代码是学习 佐手邊倖冨的博客 没有绘画drwa的原因是因为 text需要设置 TextView.BufferType.SPANNABLE 属性 */ public class LinearGradientFontSpan extends ReplacementSpan { // 文字宽度 private int mSize; // 渐变开始颜色 private int startColor = Color.BLUE; // 渐变结束颜色 private int endColor = Color.RED; // 是否左右渐变 private boolean isLeftToRight = true; public LinearGradientFontSpan() { } public LinearGradientFontSpan(int startColor, int endColor, boolean leftToRight) { this.startColor = startColor; this.endColor = endColor; this.isLeftToRight = leftToRight; } @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { mSize = (int) (paint.measureText(text, start, end)); return mSize; } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint){ // 修改y1的值从上到下渐变, 修改x1的值从左到右渐变 LinearGradient lg; if (isLeftToRight) { lg = new LinearGradient(0, 0, mSize, 0, startColor, endColor, Shader.TileMode.REPEAT); } else { lg = new LinearGradient(0, 0, 0, paint.descent() - paint.ascent(), startColor, endColor, Shader.TileMode.REPEAT); } paint.setShader(lg); canvas.drawText(text, start, end, x, y, paint);//绘制文字 } }
最后
以上就是迷你西装最近收集整理的关于TextView小技巧记录下的全部内容,更多相关TextView小技巧记录下内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复