以下代码是想要在某些文本前面绘制蓝色圆圈的PointReplaceSpan
public class PointReplaceSpan extends ReplacementSpan {
private final String TAG = "PointReplaceSpan";
private boolean DEBUG = true;
private int mMeasureWidth;
private int mPointColor = 0xB33FBEFF;
private Paint mPaint;
private int textWidth;
private Context mContext;
private int radius;
private final String ellipsis = "...";
private boolean mIndicatorEnable;
private int mPadding;
private RectF textRectF;
public PointReplaceSpan(Context context) {
this(context, false);
}
public PointReplaceSpan(Context context, boolean pointIndicator) {
this.mContext = context;
mPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, context.getResources().getDisplayMetrics()));
int color = 0x444444;
mPaint.setColor(color);
radius = dp2px(3);
mIndicatorEnable = pointIndicator;
textRectF = new RectF();
}
public void setPadding(int padding) {
mPadding = padding;
}
public void setPointColor(int color) {
mPointColor = color;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
//get the width of text
if (mIndicatorEnable) {
mMeasureWidth = (int) mPaint.measureText(text, start, end) + 2 * radius + dp2px(4);
} else {
mMeasureWidth = (int) mPaint.measureText(text, start, end);
}
final int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;
textWidth = (widthPixels - 2 * mPadding)/4;
if (DEBUG) {
Log.i(TAG, "bankle getSize() called with text = [" + text + "], start = [" + start + "], end= "+end+" screenWidth "+widthPixels+" padding "+mPadding+" mMeasureWidth "+ mMeasureWidth+" textWidth "+ textWidth);
}
if (mMeasureWidth > textWidth) {
return textWidth;
}
return mMeasureWidth;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
//draw the text
if (TextUtils.isEmpty(text)) {
return;
}
if (DEBUG) {
Log.i(TAG, "bankle draw() called with " + text +" [" + text + "], start = [" + start + "], end = [" + end + "], x = [" + x + "], top = [" + top + "], y = [" + y + "], bottom = [" + bottom +"]");
}
paint.setColor(mPointColor);
Paint.FontMetrics fontM = paint.getFontMetrics();
float v = top - (fontM.top - fontM.ascent / 2);
y = (int) (top - fontM.ascent);
int circlePointWidth = 0;
if (mIndicatorEnable) {//draw point
circlePointWidth = 2 * radius + dp2px(4);
canvas.drawCircle(x + radius, v, radius, paint);
}
if (DEBUG) {
Log.i(TAG, "bankle draw() called with text = " + text + " mMeasureWidth " + mMeasureWidth + " textWidth " + textWidth);
}
if (mMeasureWidth > textWidth) { // 文字过长时添加省略号
float ellipsisWidth = mPaint.measureText(ellipsis, 0, ellipsis.length());
float restWidth = textWidth - ellipsisWidth - circlePointWidth;
int availableIndex = mPaint.breakText(text, 0, text.length(), true, restWidth, null);// available index of text should be drawn
if (DEBUG) {
Log.i(TAG, "bankle draw() called with text = ["+text+"] availableIndex "+ availableIndex +" text.length "+ text.length()+" ellipsisWidth "+ ellipsisWidth);
}
if (availableIndex > text.length() -1) {
availableIndex = text.length() -1;
}
float measuredWidth = mPaint.measureText(text, 0, availableIndex);// the measureWidth of available text
canvas.drawText(text, 0, availableIndex, x + circlePointWidth, y, mPaint);
float ellipsisX = circlePointWidth + measuredWidth;// the margin x of ellipsis
canvas.drawText(ellipsis, 0, ellipsis.length(), ellipsisX, y, mPaint);
textRectF.set(x, top, x + circlePointWidth+ellipsisWidth+ measuredWidth, bottom);
} else {
canvas.drawText(text, start, end, x + circlePointWidth, y, mPaint);
float textWidth = mPaint.measureText(text, start, end);
textRectF.set(x, top, x + circlePointWidth + textWidth, bottom);
}
if (DEBUG) {
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRoundRect(textRectF, 10, 10, paint);
}
}
private int dp2px(int dpValue) {
return (int) mContext.getResources().getDisplayMetrics().density * dpValue;
}
}
在其他地方,我这样做:
CharSequence appName = appInfo.getDisplayName(context)
replace = new PointReplaceSpan(context, true);
replace.setPadding(outPadding);
SpannableString span = new SpannableString(appName);
span.setSpan(replace,0,appName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.setAppName(span, TextView.BufferType.SPANNABLE);
然后我们可以看到:
logcat是:
bankle getSize() called with text = [SpreadScreenSpread], start = [0], end= 18 screenWidth 1440 padding 25 mMeasureWidth 422 textWidth 347 bankle getSize() called with text = [Browser], start = [0], end= 7 screenWidth 1440 padding 25 mMeasureWidth 154 textWidth 347 bankle getSize() called with text = [Google Play Store], start = [0], end= 17 screenWidth 1440 padding 25 mMeasureWidth 332 textWidth 347 bankle getSize() called with text = [Gallery], start = [0], end= 7 screenWidth 1440 padding 25 mMeasureWidth 128 textWidth 347 bankle getSize() called with text = […], start = [0], end= 18 screenWidth 1440 padding 25 mMeasureWidth 58 textWidth 347 bankle getSize() called with text = […], start = [0], end= 18 screenWidth 1440 padding 25 mMeasureWidth 58 textWidth 347 bankle getSize() called with text = [Browser], start = [0], end= 7 screenWidth 1440 padding 25 mMeasureWidth 154 textWidth 347 bankle getSize() called with text = [Browser], start = [0], end= 7 screenWidth 1440 padding 25 mMeasureWidth 154 textWidth 347 bankle getSize() called with text = […], start = [0], end= 17 screenWidth 1440 padding 25 mMeasureWidth 28 textWidth 347 bankle getSize() called with text = […], start = [0], end= 17 screenWidth 1440 padding 25 mMeasureWidth 28 textWidth 347 bankle getSize() called with text = [Gallery], start = [0], end= 7 screenWidth 1440 padding 25 mMeasureWidth 128 textWidth 347 bankle getSize() called with text = [Gallery], start = [0], end= 7 screenWidth 1440 padding 25 mMeasureWidth 128 textWidth 347 bankle getSize() called with text = […], start = [0], end= 18 screenWidth 1440 padding 25 mMeasureWidth 58 textWidth 347 bankle draw() called with … […], start = [0], end = [18], x = [135.0], top = [0], y = [89], bottom = [112] bankle draw() called with text = … mMeasureWidth 58 textWidth 347 bankle getSize() called with text = [Browser], start = [0], end= 7 screenWidth 1440 padding 25 mMeasureWidth 154 textWidth 347 bankle draw() called with Browser [Browser], start = [0], end = [7], x = [87.0], top = [0], y = [89], bottom = [112] bankle draw() called with text = Browser mMeasureWidth 154 textWidth 347 bankle getSize() called with text = […], start = [0], end= 17 screenWidth 1440 padding 25 mMeasureWidth 28 textWidth 347 bankle draw() called with … […], start = [0], end = [17], x = [150.0], top = [0], y = [89], bottom = [112] bankle draw() called with text = … mMeasureWidth 28 textWidth 347 bankle getSize() called with text = [Gallery], start = [0], end= 7 screenWidth 1440 padding 25 mMeasureWidth 128 textWidth 347 bankle draw() called with Gallery [Gallery], start = [0], end = [7], x = [100.0], top = [0], y = [89], bottom = [112] bankle draw() called with text = Gallery mMeasureWidth 128 textWidth 347
当文本宽度超过TextView,然后文本被分配给“...”,为什么?任何人都可以帮助我吗?请
另外,对应的textview
<TextView
android:id="@+id/xxx"
android:layout_width="match_parent"
android:layout_height="18dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#444444"
android:textSize="12sp"/>
最后我删除了attribue android:maxLines =“1” 然后问题已经解决了芽我不知道为什么?