下面列出了android.text.StaticLayout#getLineTop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected int getLineHeight() {
if (selectedView != null && selectedView.getMessageObject() != null) {
MessageObject object = selectedView.getMessageObject();
StaticLayout layout = null;
if (isDescription) {
layout = selectedView.getDescriptionlayout();
} else if (selectedView.hasCaptionLayout()) {
layout = selectedView.getCaptionLayout();
} else if (object.textLayoutBlocks != null) {
layout = object.textLayoutBlocks.get(0).textLayout;
}
if (layout == null) {
return 0;
}
int lineHeight = layout.getLineBottom(0) - layout.getLineTop(0);
return lineHeight;
}
return 0;
}
@Override
protected int getLineHeight() {
if (selectedView == null) {
return 0;
} else {
arrayList.clear();
selectedView.fillTextLayoutBlocks(arrayList);
int index = startPeek ? startViewChildPosition : endViewChildPosition;
if (index < 0 || index >= arrayList.size()) {
return 0;
}
StaticLayout layout = arrayList.get(index).getLayout();
int min = Integer.MAX_VALUE;
for (int i = 0; i < layout.getLineCount(); i++) {
int h = layout.getLineBottom(i) - layout.getLineTop(i);
if (h < min) min = h;
}
return min;
}
}
@Override
protected int getLineHeight() {
if (selectedView != null && selectedView.getMessageObject() != null) {
MessageObject object = selectedView.getMessageObject();
StaticLayout layout = null;
if (isDescription) {
layout = selectedView.getDescriptionlayout();
} else if (selectedView.hasCaptionLayout()) {
layout = selectedView.getCaptionLayout();
} else if (object.textLayoutBlocks != null) {
layout = object.textLayoutBlocks.get(0).textLayout;
}
if (layout == null) {
return 0;
}
int lineHeight = layout.getLineBottom(0) - layout.getLineTop(0);
return lineHeight;
}
return 0;
}
@Override
protected int getLineHeight() {
if (selectedView == null) {
return 0;
} else {
arrayList.clear();
selectedView.fillTextLayoutBlocks(arrayList);
int index = startPeek ? startViewChildPosition : endViewChildPosition;
if (index < 0 || index >= arrayList.size()) {
return 0;
}
StaticLayout layout = arrayList.get(index).getLayout();
int min = Integer.MAX_VALUE;
for (int i = 0; i < layout.getLineCount(); i++) {
int h = layout.getLineBottom(i) - layout.getLineTop(i);
if (h < min) min = h;
}
return min;
}
}
private void roateSpan() {
TextView tv_rotate_img_span = (TextView) findViewById(R.id.tv_rotate_img_span);
ImageView img_rotate = (ImageView) findViewById(R.id.img_rotate);
Drawable drawable = getResources().getDrawable(R.drawable.icon);
// drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.setBounds(0, 0, 200, 200);
img_rotate.setImageDrawable(drawable);
ViewGroup.LayoutParams lp = img_rotate.getLayoutParams();
lp.width = 200;
lp.height = 200;
img_rotate.setLayoutParams(lp);
//这个可以在oncreate的时候测量出来
StaticLayout sl = new StaticLayout("aa", tv_rotate_img_span.getPaint(),
100, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int lineHeight = sl.getLineBottom(0) - sl.getLineTop(0);// 比 tv_rotate_img_span.getLineHeight() 这个好 因为每行高刚度可能不一致
int affectLine = 0;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if (i * lineHeight > 200 + 20) {
affectLine = i - 1;
break;
}
}
SpannableString msp = new SpannableString("可是现在我已不是在天上翱翔的小鸟,在地上奔跑的动物了,因为我长大了,已经五年级了,大家都在努力地学习,谁都不想输给别人,每个人都在奋发图强,“学习如逆水行舟,不进则退”。这句话在我耳边回荡,难道我们就不应该有一个孩子应该有的童真吗?难道我已经失去了吗?难道现在我们所有的孩子都应该抓住学习这根攀山绳吗?为什么我们不?");
tv_rotate_img_span.setText(msp);
tv_rotate_img_span.setGravity(Gravity.LEFT);
msp.setSpan(new ImageRotate(affectLine), 0, msp.length() - 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
tv_rotate_img_span.setText(msp);
}
private void drawLine(Canvas canvas, StaticLayout layout, int line, int start, int end) {
layout.getSelectionPath(start, end, path);
if (path.lastBottom < layout.getLineBottom(line)) {
int lineBottom = layout.getLineBottom(line);
int lineTop = layout.getLineTop(line);
float lineH = lineBottom - lineTop;
float lineHWithoutSpaсing = path.lastBottom - lineTop;
canvas.save();
canvas.scale(1f, lineH / lineHWithoutSpaсing, 0, lineTop);
canvas.drawPath(path, selectionPaint);
canvas.restore();
} else {
canvas.drawPath(path, selectionPaint);
}
}
public void setCurrentLayout(StaticLayout layout, int start, float yOffset) {
currentLayout = layout;
currentLine = layout.getLineForOffset(start);
lastTop = -1;
heightOffset = yOffset;
if (Build.VERSION.SDK_INT >= 28) {
int lineCount = layout.getLineCount();
if (lineCount > 0) {
lineHeight = layout.getLineBottom(lineCount - 1) - layout.getLineTop(lineCount - 1);
}
}
}
private void drawLine(Canvas canvas, StaticLayout layout, int line, int start, int end) {
layout.getSelectionPath(start, end, path);
if (path.lastBottom < layout.getLineBottom(line)) {
int lineBottom = layout.getLineBottom(line);
int lineTop = layout.getLineTop(line);
float lineH = lineBottom - lineTop;
float lineHWithoutSpaсing = path.lastBottom - lineTop;
canvas.save();
canvas.scale(1f, lineH / lineHWithoutSpaсing, 0, lineTop);
canvas.drawPath(path, selectionPaint);
canvas.restore();
} else {
canvas.drawPath(path, selectionPaint);
}
}
public void setCurrentLayout(StaticLayout layout, int start, float yOffset) {
currentLayout = layout;
currentLine = layout.getLineForOffset(start);
lastTop = -1;
heightOffset = yOffset;
if (Build.VERSION.SDK_INT >= 28) {
int lineCount = layout.getLineCount();
if (lineCount > 0) {
lineHeight = layout.getLineBottom(lineCount - 1) - layout.getLineTop(lineCount - 1);
}
}
}
@SuppressWarnings("PMD.NPathComplexity") // TODO break this method up
private void drawTextLayout(Canvas canvas) {
StaticLayout layout = textLayout;
if (layout == null) {
// Nothing to draw.
return;
}
int saveCount = canvas.save();
canvas.translate(textLeft, textTop);
if (Color.alpha(windowColor) > 0) {
paint.setColor(windowColor);
canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(),
paint);
}
if (Color.alpha(backgroundColor) > 0) {
paint.setColor(backgroundColor);
float previousBottom = layout.getLineTop(0);
int lineCount = layout.getLineCount();
for (int i = 0; i < lineCount; i++) {
lineBounds.left = layout.getLineLeft(i) - textPaddingX;
lineBounds.right = layout.getLineRight(i) + textPaddingX;
lineBounds.top = previousBottom;
lineBounds.bottom = layout.getLineBottom(i);
previousBottom = lineBounds.bottom;
canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint);
}
}
if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
textPaint.setStrokeJoin(Join.ROUND);
textPaint.setStrokeWidth(outlineWidth);
textPaint.setColor(edgeColor);
textPaint.setStyle(Style.FILL_AND_STROKE);
layout.draw(canvas);
} else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
} else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
|| edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
int colorUp = raised ? Color.WHITE : edgeColor;
int colorDown = raised ? edgeColor : Color.WHITE;
float offset = shadowRadius / 2;
textPaint.setColor(foregroundColor);
textPaint.setStyle(Style.FILL);
textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
layout.draw(canvas);
textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
}
textPaint.setColor(foregroundColor);
textPaint.setStyle(Style.FILL);
layout.draw(canvas);
textPaint.setShadowLayer(0, 0, 0, 0);
canvas.restoreToCount(saveCount);
}
private void drawTextLayout(Canvas canvas) {
StaticLayout layout = textLayout;
if (layout == null) {
// Nothing to draw.
return;
}
int saveCount = canvas.save();
canvas.translate(textLeft, textTop);
if (Color.alpha(windowColor) > 0) {
paint.setColor(windowColor);
canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(),
paint);
}
if (Color.alpha(backgroundColor) > 0) {
paint.setColor(backgroundColor);
float previousBottom = layout.getLineTop(0);
int lineCount = layout.getLineCount();
for (int i = 0; i < lineCount; i++) {
lineBounds.left = layout.getLineLeft(i) - textPaddingX;
lineBounds.right = layout.getLineRight(i) + textPaddingX;
lineBounds.top = previousBottom;
lineBounds.bottom = layout.getLineBottom(i);
previousBottom = lineBounds.bottom;
canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint);
}
}
if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
textPaint.setStrokeJoin(Join.ROUND);
textPaint.setStrokeWidth(outlineWidth);
textPaint.setColor(edgeColor);
textPaint.setStyle(Style.FILL_AND_STROKE);
layout.draw(canvas);
} else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
} else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
|| edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
int colorUp = raised ? Color.WHITE : edgeColor;
int colorDown = raised ? edgeColor : Color.WHITE;
float offset = shadowRadius / 2f;
textPaint.setColor(foregroundColor);
textPaint.setStyle(Style.FILL);
textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
layout.draw(canvas);
textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
}
textPaint.setColor(foregroundColor);
textPaint.setStyle(Style.FILL);
layout.draw(canvas);
textPaint.setShadowLayer(0, 0, 0, 0);
canvas.restoreToCount(saveCount);
}
private void cacuLineHeight()
{
layout = new StaticLayout("爱我中华", mTextPaint, 0, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, false);
mLineHeight = layout.getLineBottom(0) - layout.getLineTop(0);
}
@Override
protected void onDraw(Canvas c) {
final StaticLayout layout = this.layout;
if (layout == null) {
return;
}
final int saveCount = c.save();
final int innerPaddingX = this.innerPaddingX;
c.translate(getPaddingLeft() + innerPaddingX, getPaddingTop());
final int lineCount = layout.getLineCount();
final Paint textPaint = this.textPaint;
final Paint paint = this.paint;
final RectF bounds = lineBounds;
if (Color.alpha(backgroundColor) > 0) {
final float cornerRadius = this.cornerRadius;
float previousBottom = layout.getLineTop(0);
paint.setColor(backgroundColor);
paint.setStyle(Style.FILL);
for (int i = 0; i < lineCount; i++) {
bounds.left = layout.getLineLeft(i) - innerPaddingX;
bounds.right = layout.getLineRight(i) + innerPaddingX;
bounds.top = previousBottom;
bounds.bottom = layout.getLineBottom(i);
previousBottom = bounds.bottom;
c.drawRoundRect(bounds, cornerRadius, cornerRadius, paint);
}
}
if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
textPaint.setStrokeJoin(Join.ROUND);
textPaint.setStrokeWidth(outlineWidth);
textPaint.setColor(edgeColor);
textPaint.setStyle(Style.FILL_AND_STROKE);
layout.draw(c);
} else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
} else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
|| edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
int colorUp = raised ? Color.WHITE : edgeColor;
int colorDown = raised ? edgeColor : Color.WHITE;
float offset = shadowRadius / 2f;
textPaint.setColor(foregroundColor);
textPaint.setStyle(Style.FILL);
textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
layout.draw(c);
textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
}
textPaint.setColor(foregroundColor);
textPaint.setStyle(Style.FILL);
layout.draw(c);
textPaint.setShadowLayer(0, 0, 0, 0);
c.restoreToCount(saveCount);
}
@Override
protected int getCharOffsetFromCord(int x, int y, int offsetX, int offsetY, ArticleSelectableView view, boolean maybe) {
if (view == null) {
return -1;
}
int line = -1;
x -= offsetX;
y -= offsetY;
arrayList.clear();
view.fillTextLayoutBlocks(arrayList);
int childIndex;
if (maybe) {
childIndex = maybeTextIndex;
} else {
childIndex = startPeek ? startViewChildPosition : endViewChildPosition;
}
StaticLayout layout = arrayList.get(childIndex).getLayout();
if (x < 0) {
x = 1;
}
if (y < 0) {
y = 1;
}
if (x > layout.getWidth()) {
x = layout.getWidth();
}
if (y > layout.getLineBottom(layout.getLineCount() - 1)) {
y = (int) (layout.getLineBottom(layout.getLineCount() - 1) - 1);
}
for (int i = 0; i < layout.getLineCount(); i++) {
if (y > layout.getLineTop(i) && y < layout.getLineBottom(i)) {
line = i;
break;
}
}
if (line >= 0) {
return layout.getOffsetForHorizontal(line, x);
}
return -1;
}
@Override
protected int getCharOffsetFromCord(int x, int y, int offsetX, int offsetY, ArticleSelectableView view, boolean maybe) {
if (view == null) {
return -1;
}
int line = -1;
x -= offsetX;
y -= offsetY;
arrayList.clear();
view.fillTextLayoutBlocks(arrayList);
int childIndex;
if (maybe) {
childIndex = maybeTextIndex;
} else {
childIndex = startPeek ? startViewChildPosition : endViewChildPosition;
}
StaticLayout layout = arrayList.get(childIndex).getLayout();
if (x < 0) {
x = 1;
}
if (y < 0) {
y = 1;
}
if (x > layout.getWidth()) {
x = layout.getWidth();
}
if (y > layout.getLineBottom(layout.getLineCount() - 1)) {
y = (int) (layout.getLineBottom(layout.getLineCount() - 1) - 1);
}
for (int i = 0; i < layout.getLineCount(); i++) {
if (y > layout.getLineTop(i) && y < layout.getLineBottom(i)) {
line = i;
break;
}
}
if (line >= 0) {
return layout.getOffsetForHorizontal(line, x);
}
return -1;
}