下面列出了android.text.Layout#getLineWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private BetterLinkMovementExtended.ClickableSpanWithText findClickableSpanUnderTouch(TextView textView, Spannable text, MotionEvent event) {
int touchX = (int) event.getX();
int touchY = (int) event.getY();
touchX -= textView.getTotalPaddingLeft();
touchY -= textView.getTotalPaddingTop();
touchX += textView.getScrollX();
touchY += textView.getScrollY();
Layout layout = textView.getLayout();
int touchedLine = layout.getLineForVertical(touchY);
int touchOffset = layout.getOffsetForHorizontal(touchedLine, (float) touchX);
this.touchedLineBounds.left = layout.getLineLeft(touchedLine);
this.touchedLineBounds.top = (float) layout.getLineTop(touchedLine);
this.touchedLineBounds.right = layout.getLineWidth(touchedLine) + this.touchedLineBounds.left;
this.touchedLineBounds.bottom = (float) layout.getLineBottom(touchedLine);
if (this.touchedLineBounds.contains((float) touchX, (float) touchY)) {
Object[] spans = text.getSpans(touchOffset, touchOffset, SPAN_CLASS);
for (Object span : spans) {
if (span instanceof ClickableSpan) {
return ClickableSpanWithText.ofSpan(textView, (ClickableSpan) span);
}
}
return null;
} else {
return null;
}
}
private float getMaxLineWidth(TextView textView) {
Layout layout = textView.getLayout();
float max_width = 0.0f;
int lines = layout.getLineCount();
for (int i = 0; i < lines; i++) {
if (layout.getLineWidth(i) > max_width) {
max_width = layout.getLineWidth(i);
}
}
return max_width;
}
private float getMaxLineWidth(Layout layout) {
float max_width = 0.0f;
int lines = layout.getLineCount();
for (int i = 0; i < lines; i++) {
if (layout.getLineWidth(i) > max_width) {
max_width = layout.getLineWidth(i);
}
}
return max_width;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (needCheckDevice && isVivoApi22()) {
int width = getMeasuredWidth();
CharSequence charSequence = getText();
Layout layout = createWorkingLayout(charSequence, getPaint(), width);
float dotWidth = getPaint().measureText(DOT_SUFFIX);
int lineCount = layout.getLineCount();
Log.d(TAG, "onMeasure , lineCount : " + lineCount);
// 多行且第一行未占满
if (lineCount > 1 && Math.abs(width - layout.getLineWidth(0)) > dotWidth) {
Log.d(TAG, "onMeasure , firstLineEndIndex : " + layout.getLineEnd(0));
int index = layout.getLineEnd(1);
while (true) {
index--;
charSequence = charSequence.subSequence(0, index);
charSequence = SpannableStringBuilder.valueOf(charSequence).append(DOT_SUFFIX);
layout = createWorkingLayout(charSequence, getPaint(), width);
if (layout.getLineCount() == 1 && (width - layout.getLineWidth(0) <= dotWidth || index <= 10)) {
mLayout = layout;
break;
}
}
}
needCheckDevice = false;
}
}
private float getMaxLineWidth(Layout layout) {
float max_width = 0.0f;
int lines = layout.getLineCount();
for (int i = 0; i < lines; i++) {
if (layout.getLineWidth(i) > max_width) {
max_width = layout.getLineWidth(i);
}
}
return max_width;
}
/**
* Approximates a given lines width with the new provided text size.
*/
private float approximateLineWidth(Layout layout, int line, float textSize) {
return layout.getLineWidth(line) * (textSize / layout.getPaint().getTextSize());
}
/** Approximates a given lines width with the new provided text size. */
private float approximateLineWidth(@NonNull Layout layout, int line, float textSize) {
return layout.getLineWidth(line) * (textSize / layout.getPaint().getTextSize());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Rect bounds = new Rect();
Drawable background = getBackground();
if (background != null) {
background.getPadding(bounds);
}
int wMode = MeasureSpec.getMode(widthMeasureSpec);
int maxW = MeasureSpec.getSize(widthMeasureSpec) - bounds.left - bounds.right;
TextView messageView = (TextView) getChildAt(0);
messageView.measure(MeasureSpec.makeMeasureSpec(maxW, wMode), heightMeasureSpec);
View timeView = getChildAt(1);
timeView.measure(MeasureSpec.makeMeasureSpec(maxW, wMode), heightMeasureSpec);
Layout textLayout = messageView.getLayout();
int contentW = messageView.getMeasuredWidth();
int timeW = timeView.getMeasuredWidth();
boolean isRtl = BidiFormatter.getInstance().isRtl(messageView.getText().toString());
if (messageView.getLayout().getLineCount() < 5 && !isRtl) {
contentW = 0;
for (int i = 0; i < textLayout.getLineCount(); i++) {
contentW = Math.max(contentW, (int) textLayout.getLineWidth(i));
}
}
int lastLineW = (int) textLayout.getLineWidth(textLayout.getLineCount() - 1);
if (isRtl) {
lastLineW = contentW;
}
int fullContentW, fullContentH;
if (isRtl) {
fullContentW = contentW;
fullContentH = messageView.getMeasuredHeight() + timeView.getMeasuredHeight();
} else {
if (lastLineW + timeW < contentW) {
// Nothing to do
fullContentW = contentW;
fullContentH = messageView.getMeasuredHeight();
} else if (lastLineW + timeW < maxW) {
fullContentW = lastLineW + timeW;
fullContentH = messageView.getMeasuredHeight();
} else {
fullContentW = contentW;
fullContentH = messageView.getMeasuredHeight() + timeView.getMeasuredHeight();
}
}
setMeasuredDimension(fullContentW + bounds.left + bounds.right, fullContentH + bounds.top + bounds.bottom);
}