下面列出了android.text.Layout.Alignment#ALIGN_OPPOSITE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
private static Alignment convertTextAlignment(@TextAlignment int textAlignment) {
switch (textAlignment) {
case TEXT_ALIGNMENT_START:
case TEXT_ALIGNMENT_LEFT:
return Alignment.ALIGN_NORMAL;
case TEXT_ALIGNMENT_CENTER:
return Alignment.ALIGN_CENTER;
case TEXT_ALIGNMENT_END:
case TEXT_ALIGNMENT_RIGHT:
return Alignment.ALIGN_OPPOSITE;
default:
Log.w(TAG, "Unknown textAlignment: " + textAlignment);
return null;
}
}
private static Alignment parseTextAlignment(String s) {
switch (s) {
case "start":
case "left":
return Alignment.ALIGN_NORMAL;
case "center":
case "middle":
return Alignment.ALIGN_CENTER;
case "end":
case "right":
return Alignment.ALIGN_OPPOSITE;
default:
Log.w(TAG, "Invalid alignment value: " + s);
return null;
}
}
private static Alignment parseTextAlignment(String s) {
switch (s) {
case "start":
case "left":
return Alignment.ALIGN_NORMAL;
case "center":
case "middle":
return Alignment.ALIGN_CENTER;
case "end":
case "right":
return Alignment.ALIGN_OPPOSITE;
default:
Log.w(TAG, "Invalid alignment value: " + s);
return null;
}
}
private static Alignment parseTextAlignment(String s) {
switch (s) {
case "start":
case "left":
return Alignment.ALIGN_NORMAL;
case "center":
case "middle":
return Alignment.ALIGN_CENTER;
case "end":
case "right":
return Alignment.ALIGN_OPPOSITE;
default:
Log.w(TAG, "Invalid alignment value: " + s);
return null;
}
}
private static Alignment parseTextAlignment(String s) {
switch (s) {
case "start":
case "left":
return Alignment.ALIGN_NORMAL;
case "center":
case "middle":
return Alignment.ALIGN_CENTER;
case "end":
case "right":
return Alignment.ALIGN_OPPOSITE;
default:
Log.w(TAG, "Invalid alignment value: " + s);
return null;
}
}
private static Alignment parseTextAlignment(String s) {
switch (s) {
case "start":
case "left":
return Alignment.ALIGN_NORMAL;
case "center":
case "middle":
return Alignment.ALIGN_CENTER;
case "end":
case "right":
return Alignment.ALIGN_OPPOSITE;
default:
Log.w(TAG, "Invalid alignment value: " + s);
return null;
}
}
private static Alignment parseTextAlignment(String s) {
switch (s) {
case "start":
case "left":
return Alignment.ALIGN_NORMAL;
case "center":
case "middle":
return Alignment.ALIGN_CENTER;
case "end":
case "right":
return Alignment.ALIGN_OPPOSITE;
default:
Log.w(TAG, "Invalid alignment value: " + s);
return null;
}
}
public Cea708Cue build() {
if (isEmpty()) {
// The cue is empty.
return null;
}
SpannableStringBuilder cueString = new SpannableStringBuilder();
// Add any rolled up captions, separated by new lines.
for (int i = 0; i < rolledUpCaptions.size(); i++) {
cueString.append(rolledUpCaptions.get(i));
cueString.append('\n');
}
// Add the current line.
cueString.append(buildSpannableString());
// TODO: Add support for right-to-left languages (i.e. where right would correspond to normal
// alignment).
Alignment alignment;
switch (justification) {
case JUSTIFICATION_FULL:
// TODO: Add support for full justification.
case JUSTIFICATION_LEFT:
alignment = Alignment.ALIGN_NORMAL;
break;
case JUSTIFICATION_RIGHT:
alignment = Alignment.ALIGN_OPPOSITE;
break;
case JUSTIFICATION_CENTER:
alignment = Alignment.ALIGN_CENTER;
break;
default:
throw new IllegalArgumentException("Unexpected justification value: " + justification);
}
float position;
float line;
if (relativePositioning) {
position = (float) horizontalAnchor / RELATIVE_CUE_SIZE;
line = (float) verticalAnchor / RELATIVE_CUE_SIZE;
} else {
position = (float) horizontalAnchor / HORIZONTAL_SIZE;
line = (float) verticalAnchor / VERTICAL_SIZE;
}
// Apply screen-edge padding to the line and position.
position = (position * 0.9f) + 0.05f;
line = (line * 0.9f) + 0.05f;
// anchorId specifies where the anchor should be placed on the caption cue/window. The 9
// possible configurations are as follows:
// 0-----1-----2
// | |
// 3 4 5
// | |
// 6-----7-----8
@AnchorType int verticalAnchorType;
if (anchorId % 3 == 0) {
verticalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId % 3 == 1) {
verticalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
verticalAnchorType = Cue.ANCHOR_TYPE_END;
}
// TODO: Add support for right-to-left languages (i.e. where start is on the right).
@AnchorType int horizontalAnchorType;
if (anchorId / 3 == 0) {
horizontalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId / 3 == 1) {
horizontalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
horizontalAnchorType = Cue.ANCHOR_TYPE_END;
}
boolean windowColorSet = (windowFillColor != COLOR_SOLID_BLACK);
return new Cea708Cue(cueString, alignment, line, Cue.LINE_TYPE_FRACTION, verticalAnchorType,
position, horizontalAnchorType, Cue.DIMEN_UNSET, windowColorSet, windowFillColor,
priority);
}
/**
* 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);
}
public Cea708Cue build() {
if (isEmpty()) {
// The cue is empty.
return null;
}
SpannableStringBuilder cueString = new SpannableStringBuilder();
// Add any rolled up captions, separated by new lines.
for (int i = 0; i < rolledUpCaptions.size(); i++) {
cueString.append(rolledUpCaptions.get(i));
cueString.append('\n');
}
// Add the current line.
cueString.append(buildSpannableString());
// TODO: Add support for right-to-left languages (i.e. where right would correspond to normal
// alignment).
Alignment alignment;
switch (justification) {
case JUSTIFICATION_FULL:
// TODO: Add support for full justification.
case JUSTIFICATION_LEFT:
alignment = Alignment.ALIGN_NORMAL;
break;
case JUSTIFICATION_RIGHT:
alignment = Alignment.ALIGN_OPPOSITE;
break;
case JUSTIFICATION_CENTER:
alignment = Alignment.ALIGN_CENTER;
break;
default:
throw new IllegalArgumentException("Unexpected justification value: " + justification);
}
float position;
float line;
if (relativePositioning) {
position = (float) horizontalAnchor / RELATIVE_CUE_SIZE;
line = (float) verticalAnchor / RELATIVE_CUE_SIZE;
} else {
position = (float) horizontalAnchor / HORIZONTAL_SIZE;
line = (float) verticalAnchor / VERTICAL_SIZE;
}
// Apply screen-edge padding to the line and position.
position = (position * 0.9f) + 0.05f;
line = (line * 0.9f) + 0.05f;
// anchorId specifies where the anchor should be placed on the caption cue/window. The 9
// possible configurations are as follows:
// 0-----1-----2
// | |
// 3 4 5
// | |
// 6-----7-----8
@AnchorType int verticalAnchorType;
if (anchorId % 3 == 0) {
verticalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId % 3 == 1) {
verticalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
verticalAnchorType = Cue.ANCHOR_TYPE_END;
}
// TODO: Add support for right-to-left languages (i.e. where start is on the right).
@AnchorType int horizontalAnchorType;
if (anchorId / 3 == 0) {
horizontalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId / 3 == 1) {
horizontalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
horizontalAnchorType = Cue.ANCHOR_TYPE_END;
}
boolean windowColorSet = (windowFillColor != COLOR_SOLID_BLACK);
return new Cea708Cue(cueString, alignment, line, Cue.LINE_TYPE_FRACTION, verticalAnchorType,
position, horizontalAnchorType, Cue.DIMEN_UNSET, windowColorSet, windowFillColor,
priority);
}
public Cea708Cue build() {
if (isEmpty()) {
// The cue is empty.
return null;
}
SpannableStringBuilder cueString = new SpannableStringBuilder();
// Add any rolled up captions, separated by new lines.
for (int i = 0; i < rolledUpCaptions.size(); i++) {
cueString.append(rolledUpCaptions.get(i));
cueString.append('\n');
}
// Add the current line.
cueString.append(buildSpannableString());
// TODO: Add support for right-to-left languages (i.e. where right would correspond to normal
// alignment).
Alignment alignment;
switch (justification) {
case JUSTIFICATION_FULL:
// TODO: Add support for full justification.
case JUSTIFICATION_LEFT:
alignment = Alignment.ALIGN_NORMAL;
break;
case JUSTIFICATION_RIGHT:
alignment = Alignment.ALIGN_OPPOSITE;
break;
case JUSTIFICATION_CENTER:
alignment = Alignment.ALIGN_CENTER;
break;
default:
throw new IllegalArgumentException("Unexpected justification value: " + justification);
}
float position;
float line;
if (relativePositioning) {
position = (float) horizontalAnchor / RELATIVE_CUE_SIZE;
line = (float) verticalAnchor / RELATIVE_CUE_SIZE;
} else {
position = (float) horizontalAnchor / HORIZONTAL_SIZE;
line = (float) verticalAnchor / VERTICAL_SIZE;
}
// Apply screen-edge padding to the line and position.
position = (position * 0.9f) + 0.05f;
line = (line * 0.9f) + 0.05f;
// anchorId specifies where the anchor should be placed on the caption cue/window. The 9
// possible configurations are as follows:
// 0-----1-----2
// | |
// 3 4 5
// | |
// 6-----7-----8
@AnchorType int verticalAnchorType;
if (anchorId % 3 == 0) {
verticalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId % 3 == 1) {
verticalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
verticalAnchorType = Cue.ANCHOR_TYPE_END;
}
// TODO: Add support for right-to-left languages (i.e. where start is on the right).
@AnchorType int horizontalAnchorType;
if (anchorId / 3 == 0) {
horizontalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId / 3 == 1) {
horizontalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
horizontalAnchorType = Cue.ANCHOR_TYPE_END;
}
boolean windowColorSet = (windowFillColor != COLOR_SOLID_BLACK);
return new Cea708Cue(cueString, alignment, line, Cue.LINE_TYPE_FRACTION, verticalAnchorType,
position, horizontalAnchorType, Cue.DIMEN_UNSET, windowColorSet, windowFillColor,
priority);
}
@Override
public Alignment getAlignment() {
return Alignment.ALIGN_OPPOSITE;
}
public Cea708Cue build() {
if (isEmpty()) {
// The cue is empty.
return null;
}
SpannableStringBuilder cueString = new SpannableStringBuilder();
// Add any rolled up captions, separated by new lines.
for (int i = 0; i < rolledUpCaptions.size(); i++) {
cueString.append(rolledUpCaptions.get(i));
cueString.append('\n');
}
// Add the current line.
cueString.append(buildSpannableString());
// TODO: Add support for right-to-left languages (i.e. where right would correspond to normal
// alignment).
Alignment alignment;
switch (justification) {
case JUSTIFICATION_FULL:
// TODO: Add support for full justification.
case JUSTIFICATION_LEFT:
alignment = Alignment.ALIGN_NORMAL;
break;
case JUSTIFICATION_RIGHT:
alignment = Alignment.ALIGN_OPPOSITE;
break;
case JUSTIFICATION_CENTER:
alignment = Alignment.ALIGN_CENTER;
break;
default:
throw new IllegalArgumentException("Unexpected justification value: " + justification);
}
float position;
float line;
if (relativePositioning) {
position = (float) horizontalAnchor / RELATIVE_CUE_SIZE;
line = (float) verticalAnchor / RELATIVE_CUE_SIZE;
} else {
position = (float) horizontalAnchor / HORIZONTAL_SIZE;
line = (float) verticalAnchor / VERTICAL_SIZE;
}
// Apply screen-edge padding to the line and position.
position = (position * 0.9f) + 0.05f;
line = (line * 0.9f) + 0.05f;
// anchorId specifies where the anchor should be placed on the caption cue/window. The 9
// possible configurations are as follows:
// 0-----1-----2
// | |
// 3 4 5
// | |
// 6-----7-----8
@AnchorType int verticalAnchorType;
if (anchorId % 3 == 0) {
verticalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId % 3 == 1) {
verticalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
verticalAnchorType = Cue.ANCHOR_TYPE_END;
}
// TODO: Add support for right-to-left languages (i.e. where start is on the right).
@AnchorType int horizontalAnchorType;
if (anchorId / 3 == 0) {
horizontalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId / 3 == 1) {
horizontalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
horizontalAnchorType = Cue.ANCHOR_TYPE_END;
}
boolean windowColorSet = (windowFillColor != COLOR_SOLID_BLACK);
return new Cea708Cue(cueString, alignment, line, Cue.LINE_TYPE_FRACTION, verticalAnchorType,
position, horizontalAnchorType, Cue.DIMEN_UNSET, windowColorSet, windowFillColor,
priority);
}
/** A method that allows to create a StaticLayout with maxLines on all supported API levels. */
public StaticLayout build() throws StaticLayoutBuilderCompatException {
if (source == null) {
source = "";
}
int availableWidth = Math.max(0, width);
CharSequence textToDraw = source;
if (maxLines == 1) {
textToDraw = TextUtils.ellipsize(source, paint, availableWidth, ellipsize);
}
end = Math.min(textToDraw.length(), end);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (isRtl) {
alignment = Alignment.ALIGN_OPPOSITE;
}
// Marshmallow introduced StaticLayout.Builder which allows us not to use
// the hidden constructor.
StaticLayout.Builder builder =
StaticLayout.Builder.obtain(
textToDraw, start, end, paint, availableWidth);
builder.setAlignment(alignment);
builder.setIncludePad(includePad);
TextDirectionHeuristic textDirectionHeuristic = isRtl
? TextDirectionHeuristics.RTL
: TextDirectionHeuristics.LTR;
builder.setTextDirection(textDirectionHeuristic);
if (ellipsize != null) {
builder.setEllipsize(ellipsize);
}
builder.setMaxLines(maxLines);
return builder.build();
}
createConstructorWithReflection();
// Use the hidden constructor on older API levels.
try {
return checkNotNull(constructor)
.newInstance(
textToDraw,
start,
end,
paint,
availableWidth,
alignment,
checkNotNull(textDirection),
1.0f,
0.0f,
includePad,
null,
availableWidth,
maxLines);
} catch (Exception cause) {
throw new StaticLayoutBuilderCompatException(cause);
}
}
public Cea708Cue build() {
if (isEmpty()) {
// The cue is empty.
return null;
}
SpannableStringBuilder cueString = new SpannableStringBuilder();
// Add any rolled up captions, separated by new lines.
for (int i = 0; i < rolledUpCaptions.size(); i++) {
cueString.append(rolledUpCaptions.get(i));
cueString.append('\n');
}
// Add the current line.
cueString.append(buildSpannableString());
// TODO: Add support for right-to-left languages (i.e. where right would correspond to normal
// alignment).
Alignment alignment;
switch (justification) {
case JUSTIFICATION_FULL:
// TODO: Add support for full justification.
case JUSTIFICATION_LEFT:
alignment = Alignment.ALIGN_NORMAL;
break;
case JUSTIFICATION_RIGHT:
alignment = Alignment.ALIGN_OPPOSITE;
break;
case JUSTIFICATION_CENTER:
alignment = Alignment.ALIGN_CENTER;
break;
default:
throw new IllegalArgumentException("Unexpected justification value: " + justification);
}
float position;
float line;
if (relativePositioning) {
position = (float) horizontalAnchor / RELATIVE_CUE_SIZE;
line = (float) verticalAnchor / RELATIVE_CUE_SIZE;
} else {
position = (float) horizontalAnchor / HORIZONTAL_SIZE;
line = (float) verticalAnchor / VERTICAL_SIZE;
}
// Apply screen-edge padding to the line and position.
position = (position * 0.9f) + 0.05f;
line = (line * 0.9f) + 0.05f;
// anchorId specifies where the anchor should be placed on the caption cue/window. The 9
// possible configurations are as follows:
// 0-----1-----2
// | |
// 3 4 5
// | |
// 6-----7-----8
@AnchorType int verticalAnchorType;
if (anchorId % 3 == 0) {
verticalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId % 3 == 1) {
verticalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
verticalAnchorType = Cue.ANCHOR_TYPE_END;
}
// TODO: Add support for right-to-left languages (i.e. where start is on the right).
@AnchorType int horizontalAnchorType;
if (anchorId / 3 == 0) {
horizontalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId / 3 == 1) {
horizontalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
horizontalAnchorType = Cue.ANCHOR_TYPE_END;
}
boolean windowColorSet = (windowFillColor != COLOR_SOLID_BLACK);
return new Cea708Cue(cueString, alignment, line, Cue.LINE_TYPE_FRACTION, verticalAnchorType,
position, horizontalAnchorType, Cue.DIMEN_UNSET, windowColorSet, windowFillColor,
priority);
}
public Cea708Cue build() {
if (isEmpty()) {
// The cue is empty.
return null;
}
SpannableStringBuilder cueString = new SpannableStringBuilder();
// Add any rolled up captions, separated by new lines.
for (int i = 0; i < rolledUpCaptions.size(); i++) {
cueString.append(rolledUpCaptions.get(i));
cueString.append('\n');
}
// Add the current line.
cueString.append(buildSpannableString());
// TODO: Add support for right-to-left languages (i.e. where right would correspond to normal
// alignment).
Alignment alignment;
switch (justification) {
case JUSTIFICATION_FULL:
// TODO: Add support for full justification.
case JUSTIFICATION_LEFT:
alignment = Alignment.ALIGN_NORMAL;
break;
case JUSTIFICATION_RIGHT:
alignment = Alignment.ALIGN_OPPOSITE;
break;
case JUSTIFICATION_CENTER:
alignment = Alignment.ALIGN_CENTER;
break;
default:
throw new IllegalArgumentException("Unexpected justification value: " + justification);
}
float position;
float line;
if (relativePositioning) {
position = (float) horizontalAnchor / RELATIVE_CUE_SIZE;
line = (float) verticalAnchor / RELATIVE_CUE_SIZE;
} else {
position = (float) horizontalAnchor / HORIZONTAL_SIZE;
line = (float) verticalAnchor / VERTICAL_SIZE;
}
// Apply screen-edge padding to the line and position.
position = (position * 0.9f) + 0.05f;
line = (line * 0.9f) + 0.05f;
// anchorId specifies where the anchor should be placed on the caption cue/window. The 9
// possible configurations are as follows:
// 0-----1-----2
// | |
// 3 4 5
// | |
// 6-----7-----8
@AnchorType int verticalAnchorType;
if (anchorId % 3 == 0) {
verticalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId % 3 == 1) {
verticalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
verticalAnchorType = Cue.ANCHOR_TYPE_END;
}
// TODO: Add support for right-to-left languages (i.e. where start is on the right).
@AnchorType int horizontalAnchorType;
if (anchorId / 3 == 0) {
horizontalAnchorType = Cue.ANCHOR_TYPE_START;
} else if (anchorId / 3 == 1) {
horizontalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
} else {
horizontalAnchorType = Cue.ANCHOR_TYPE_END;
}
boolean windowColorSet = (windowFillColor != COLOR_SOLID_BLACK);
return new Cea708Cue(cueString, alignment, line, Cue.LINE_TYPE_FRACTION, verticalAnchorType,
position, horizontalAnchorType, Cue.DIMEN_UNSET, windowColorSet, windowFillColor,
priority);
}