参考文章
Android之自定义TextView学习笔记
Android Canvas的drawText()和文字居中方案
该控件解决了什么问题
TextView只显示一行,后台提供10个文字,但是你只想显示5个,剩余的部分用...来表示
可以自定义哪些?
1、文本大小
2、文字颜色
3、文字信息
使用
textViewSingleLineEllipsis0.setStringText("我要显示有100个文字,但是我只显示10个文字,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚,德玛西亚");
textViewSingleLineEllipsis0.setMaxShowLength(10);
textViewSingleLineEllipsis0.update();
<com.gxx.widgetlibrary.textview.TextViewSingleLineEllipsis
android:id="@+id/text_show_textview_single_ellipis_0"
android:layout_width="wrap_content"
app:text_ellipsis_length="10"
app:text_ellipsis_textsize="@dimen/sp_18"
app:text_ellipsis_color="@color/app_theme_color_sysm"
android:layout_height="wrap_content"/>
attributes
<declare-styleable name="TextViewSingleLineEllipsis">
<attr name="text_ellipsis_textvalue" format="string"/>
<attr name="text_ellipsis_length" format="integer"/>
<attr name="text_ellipsis_color" format="color"/>
<attr name="text_ellipsis_textsize" format="dimension"/>
</declare-styleable>
代码
public class TextViewSingleLineEllipsis extends View {
private int textColor = 0;
private float textSize = 0;
private int maxShowLength = 5;
private String stringText = "";
private Paint mPaint = new Paint();
public TextViewSingleLineEllipsis(Context context) {
this(context, null);
}
public TextViewSingleLineEllipsis(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextViewSingleLineEllipsis(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextViewSingleLineEllipsis);
maxShowLength = typedArray.getInt(R.styleable.TextViewSingleLineEllipsis_text_ellipsis_length, 5);
textSize = typedArray.getDimension(R.styleable.TextViewSingleLineEllipsis_text_ellipsis_textsize, getResources().getDimension(R.dimen.sp_14));
textColor = typedArray.getColor(R.styleable.TextViewSingleLineEllipsis_text_ellipsis_color, getResources().getColor(R.color.black));
stringText = typedArray.getString(R.styleable.TextViewSingleLineEllipsis_text_ellipsis_textvalue);
typedArray.recycle();
mPaint.setColor(textColor);
mPaint.setTextSize(textSize);
mPaint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = 0, height = 0;
int speceWithMode = MeasureSpec.getMode(widthMeasureSpec);
int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);
switch (speceWithMode) {
case MeasureSpec.EXACTLY:
width = getPaddingLeft() + getPaddingRight() + specWidthSize;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.UNSPECIFIED:
float textWidth = mPaint.measureText(stringText);
width = (int) (getPaddingLeft() + getPaddingRight() + textWidth);
break;
}
int speceHeightMode = MeasureSpec.getMode(heightMeasureSpec);
int specHeightSize = MeasureSpec.getSize(heightMeasureSpec);
switch (speceHeightMode) {
case MeasureSpec.EXACTLY:
height = getPaddingTop() + getPaddingBottom() + specHeightSize;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.UNSPECIFIED:
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
float textHeight = Math.abs((fontMetrics.bottom - fontMetrics.top));
height = (int) (getPaddingTop() + getPaddingBottom() + textHeight);
break;
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
if (!StringUtils.isEmpty(stringText)) {
if (stringText.length() > maxShowLength) {
stringText = stringText.substring(0, maxShowLength) + "...";
}
mPaint.setColor(textColor);
mPaint.setTextSize(textSize);
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
canvas.drawText(stringText, 0, getHeight() / 2 + distance, mPaint);
}
}
public void setStringText(String stringText) {
if (!StringUtils.isEmpty(stringText)) {
if (stringText.length() > maxShowLength) {
this.stringText = stringText.substring(0, maxShowLength) + "...";
}
}
}
public void setTextColor(int color) {
this.textColor = color;
}
public void setMaxShowLength(int maxShowLength) {
this.maxShowLength = maxShowLength;
}
public void setTextSize(float textSize){
this.textSize = textSize;
}
public void update(){
requestLayout();
invalidate();
}
public String getStringText() {
return stringText;
}
}