下面列出了android.text.Layout#getParagraphDirection ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* If the direction of the URL has changed, update mUrlDirection and notify the
* UrlDirectionListeners.
*/
private void updateUrlDirection() {
Layout layout = getLayout();
if (layout == null) return;
int urlDirection;
if (length() == 0) {
urlDirection = LAYOUT_DIRECTION_LOCALE;
} else if (layout.getParagraphDirection(0) == Layout.DIR_LEFT_TO_RIGHT) {
urlDirection = LAYOUT_DIRECTION_LTR;
} else {
urlDirection = LAYOUT_DIRECTION_RTL;
}
if (urlDirection != mUrlDirection) {
mUrlDirection = urlDirection;
if (mUrlDirectionListener != null) {
mUrlDirectionListener.onUrlDirectionChanged(urlDirection);
}
}
}
/**
* If the direction of the URL has changed, update mUrlDirection and notify the
* UrlDirectionListeners.
*/
private void updateUrlDirection() {
Layout layout = getLayout();
if (layout == null) return;
int urlDirection;
if (length() == 0) {
urlDirection = LAYOUT_DIRECTION_LOCALE;
} else if (layout.getParagraphDirection(0) == Layout.DIR_LEFT_TO_RIGHT) {
urlDirection = LAYOUT_DIRECTION_LTR;
} else {
urlDirection = LAYOUT_DIRECTION_RTL;
}
if (urlDirection != mUrlDirection) {
mUrlDirection = urlDirection;
if (mUrlDirectionListener != null) {
mUrlDirectionListener.onUrlDirectionChanged(urlDirection);
}
}
}
/**
* If the direction of the URL has changed, update mUrlDirection and notify the
* UrlDirectionListeners.
*/
private void updateUrlDirection() {
Layout layout = getLayout();
if (layout == null) return;
int urlDirection;
if (length() == 0) {
urlDirection = LAYOUT_DIRECTION_LOCALE;
} else if (layout.getParagraphDirection(0) == Layout.DIR_LEFT_TO_RIGHT) {
urlDirection = LAYOUT_DIRECTION_LTR;
} else {
urlDirection = LAYOUT_DIRECTION_RTL;
}
if (urlDirection != mUrlDirection) {
mUrlDirection = urlDirection;
if (mUrlDirectionListener != null) {
mUrlDirectionListener.onUrlDirectionChanged(urlDirection);
}
}
}
/**
* Scrolls the specified widget to the specified coordinates, except
* constrains the X scrolling position to the horizontal regions of
* the text that will be visible after scrolling to the specified
* Y position.
*/
public static void scrollTo(TextView widget, Layout layout, int x, int y) {
final int horizontalPadding = widget.getTotalPaddingLeft() + widget.getTotalPaddingRight();
final int availableWidth = widget.getWidth() - horizontalPadding;
final int top = layout.getLineForVertical(y);
Alignment a = layout.getParagraphAlignment(top);
boolean ltr = layout.getParagraphDirection(top) > 0;
int left, right;
if (widget.getHorizontallyScrolling()) {
final int verticalPadding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
final int bottom = layout.getLineForVertical(y + widget.getHeight() - verticalPadding);
left = Integer.MAX_VALUE;
right = 0;
for (int i = top; i <= bottom; i++) {
left = (int) Math.min(left, layout.getLineLeft(i));
right = (int) Math.max(right, layout.getLineRight(i));
}
} else {
left = 0;
right = availableWidth;
}
final int actualWidth = right - left;
if (actualWidth < availableWidth) {
if (a == Alignment.ALIGN_CENTER) {
x = left - ((availableWidth - actualWidth) / 2);
} else if ((ltr && (a == Alignment.ALIGN_OPPOSITE)) ||
(!ltr && (a == Alignment.ALIGN_NORMAL)) ||
(a == Alignment.ALIGN_RIGHT)) {
// align_opposite does NOT mean align_right, we need the paragraph
// direction to resolve it to left or right
x = left - (availableWidth - actualWidth);
} else {
x = left;
}
} else {
x = Math.min(x, right - availableWidth);
x = Math.max(x, left);
}
widget.scrollTo(x, y);
}
private void draw(
@NonNull View view,
@NonNull Canvas canvas,
@NonNull Layout layout
) {
final CharSequence cs = layout.getText();
if (!(cs instanceof Spanned)) {
return;
}
final Spanned spanned = (Spanned) cs;
final int save = canvas.save();
try {
canvas.translate(view.getPaddingLeft(), view.getPaddingTop());
// TODO: block?
// TODO: we must remove _original_ spans
// TODO: cache (attach a listener?)
// TODO: editor?
final CodeSpan[] spans = spanned.getSpans(0, spanned.length(), CodeSpan.class);
if (spans != null && spans.length > 0) {
for (CodeSpan span : spans) {
final int startOffset = spanned.getSpanStart(span);
final int endOffset = spanned.getSpanEnd(span);
final int startLine = layout.getLineForOffset(startOffset);
final int endLine = layout.getLineForOffset(endOffset);
// do we need to round them?
final float left = layout.getPrimaryHorizontal(startOffset)
+ (-1 * layout.getParagraphDirection(startLine) * paddingHorizontal);
final float right = layout.getPrimaryHorizontal(endOffset)
+ (layout.getParagraphDirection(endLine) * paddingHorizontal);
final float top = getLineTop(layout, startLine, paddingVertical);
final float bottom = getLineBottom(layout, endLine, paddingVertical);
Debug.i(new RectF(left, top, right, bottom).toShortString());
if (startLine == endLine) {
canvas.drawRect(left, top, right, bottom, paint);
} else {
// draw first line (start until the lineEnd)
// draw everything in-between (startLine - endLine)
// draw last line (lineStart until the end
canvas.drawRect(
left,
top,
layout.getLineRight(startLine),
getLineBottom(layout, startLine, paddingVertical),
paint
);
for (int line = startLine + 1; line < endLine; line++) {
canvas.drawRect(
layout.getLineLeft(line),
getLineTop(layout, line, paddingVertical),
layout.getLineRight(line),
getLineBottom(layout, line, paddingVertical),
paint
);
}
canvas.drawRect(
layout.getLineLeft(endLine),
getLineTop(layout, endLine, paddingVertical),
right,
getLineBottom(layout, endLine, paddingVertical),
paint
);
}
}
}
} finally {
canvas.restoreToCount(save);
}
}