下面列出了android.text.Layout#DIR_LEFT_TO_RIGHT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static String getTextDirection(Spanned text, int start, int end) {
// FIXME not supported
int paraDir = Layout.DIR_LEFT_TO_RIGHT;
// final int len = end - start;
// final byte[] levels = ArrayUtils.newUnpaddedByteArray(len);
// final char[] buffer = TextUtils.obtain(len);
// TextUtils.getChars(text, start, end, buffer, 0);
// int paraDir = AndroidBidi.bidi(Layout.DIR_REQUEST_DEFAULT_LTR, buffer, levels, len,
// false /* no info */);
switch (paraDir) {
case Layout.DIR_RIGHT_TO_LEFT:
return " dir=\"rtl\"";
case Layout.DIR_LEFT_TO_RIGHT:
default:
return " dir=\"ltr\"";
}
}
/**
* 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);
}
}
}
private static String getTextDirection(Spanned text, int start, int end) {
/*final int len = end - start;
final byte[] levels = ArrayUtils.newUnpaddedByteArray(len);
final char[] buffer = TextUtils.obtain(len);
TextUtils.getChars(text, start, end, buffer, 0);
int paraDir = AndroidBidi.bidi(Layout.DIR_REQUEST_DEFAULT_LTR, buffer, levels, len,
false *//* no info *//*);*/
int paraDir = Layout.DIR_LEFT_TO_RIGHT;
switch (paraDir) {
case Layout.DIR_RIGHT_TO_LEFT:
return " dir=\"rtl\"";
case Layout.DIR_LEFT_TO_RIGHT:
default:
return " dir=\"ltr\"";
}
}
/**
* 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);
}
}
}
/**
* Draws this frame onto the given <code>canvas</code> using the given <code>renderer</code>.
*
* @param renderer The renderer to use for drawing this frame.
* @param canvas The canvas onto which to draw this frame.
* @param x The x- position at which to draw this frame.
* @param y The y- position at which to draw this frame.
*/
public void draw(@NonNull Renderer renderer, @NonNull Canvas canvas, float x, float y) {
canvas.translate(x, y);
drawBackground(canvas);
int lineCount = lineList.size();
for (int i = 0; i < lineCount; i++) {
ComposedLine composedLine = lineList.get(i);
Object[] lineSpans = composedLine.getSpans();
int lineLeft = 0;
int lineRight = (int) (mWidth + 0.5f);
// Draw leading margins of this line.
for (Object style : lineSpans) {
if (style instanceof LeadingMarginSpan) {
LeadingMarginSpan span = (LeadingMarginSpan) style;
byte paragraphLevel = composedLine.getParagraphLevel();
boolean isLTR = (paragraphLevel & 1) == 0;
Paint paint = lazyPaint();
int margin = (isLTR ? lineLeft : lineRight);
int direction = (isLTR ? Layout.DIR_LEFT_TO_RIGHT : Layout.DIR_RIGHT_TO_LEFT);
int lineTop = (int) (composedLine.getTop() + 0.5f);
int lineBaseline = (int) (composedLine.getOriginY() + 0.5f);
int lineBottom = (int) (composedLine.getTop() + composedLine.getHeight() + 0.5f);
Spanned sourceText = (Spanned) source;
int lineStart = composedLine.getCharStart();
int lineEnd = composedLine.getCharEnd();
boolean isFirst = composedLine.isFirst();
span.drawLeadingMargin(canvas, paint, margin, direction,
lineTop, lineBaseline, lineBottom,
sourceText, lineStart, lineEnd, isFirst, null);
if (isLTR) {
lineLeft += span.getLeadingMargin(isFirst);
} else {
lineRight -= span.getLeadingMargin(isFirst);
}
}
}
composedLine.draw(renderer, canvas, composedLine.getOriginX(), composedLine.getOriginY());
}
canvas.translate(-x, -y);
}