概述
忘了是看谁的博客 大致写法都差不多
SpannableString 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);
/**
* 图文拼接居中显示
* @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;
}
}
/**
使用方法
* 在自定义的 LinearGradientFontSpan 中 text不会draw 添加TextView.BufferType.SPANNABLE
*/
mTvPrice.setText(TextColorGradientUtlis.getGradientSpan("10", Color.BLUE, Color.RED, false), TextView.BufferType.SPANNABLE)
/**
* @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
}
}
/**
* @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小技巧记录下所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复