use of android.text.style.RelativeSizeSpan in project openScale by oliexdev.
the class FloatMeasurementView method appendDiffValue.
@Override
public void appendDiffValue(SpannableStringBuilder text, boolean newLine) {
if (previousValue < 0.0f) {
return;
}
char symbol;
int color;
final float diff = value - previousValue;
if (diff > 0.0f) {
symbol = SYMBOL_UP;
color = Color.GREEN;
} else if (diff < 0.0f) {
symbol = SYMBOL_DOWN;
color = Color.RED;
} else {
symbol = SYMBOL_NEUTRAL;
color = Color.GRAY;
}
// change color depending on if you are going towards or away from your weight goal
if (this instanceof WeightMeasurementView) {
if (diff > 0.0f) {
color = (value > getScaleUser().getGoalWeight()) ? Color.RED : Color.GREEN;
} else if (diff < 0.0f) {
color = (value < getScaleUser().getGoalWeight()) ? Color.RED : Color.GREEN;
}
}
final float evalValue = maybeConvertToOriginalValue(value);
EvaluationSheet evalSheet = new EvaluationSheet(getScaleUser(), dateTime);
evaluationResult = evaluateSheet(evalSheet, evalValue);
if (evaluationResult != null) {
switch(evaluationResult.eval_state) {
case LOW:
color = (diff > 0.0f) ? Color.GREEN : Color.RED;
break;
case HIGH:
color = (diff < 0.0f) ? Color.GREEN : Color.RED;
break;
case NORMAL:
color = Color.GREEN;
break;
}
}
if (newLine) {
text.append('\n');
}
int start = text.length();
text.append(symbol);
text.setSpan(new ForegroundColorSpan(color), start, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
text.append(' ');
start = text.length();
text.append(formatValue(diff));
text.setSpan(new RelativeSizeSpan(0.8f), start, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
use of android.text.style.RelativeSizeSpan in project openScale by oliexdev.
the class ChartMarkerView method refreshContent.
@Override
public void refreshContent(Entry e, Highlight highlight) {
Object[] extraData = (Object[]) e.getData();
ScaleMeasurement measurement = (ScaleMeasurement) extraData[0];
ScaleMeasurement prevMeasurement = (ScaleMeasurement) extraData[1];
FloatMeasurementView measurementView = (FloatMeasurementView) extraData[2];
SpannableStringBuilder markerText = new SpannableStringBuilder();
if (measurement != null) {
measurementView.loadFrom(measurement, prevMeasurement);
DateFormat dateFormat = DateFormat.getDateInstance();
markerText.append(dateFormat.format(measurement.getDateTime()));
markerText.setSpan(new RelativeSizeSpan(0.8f), 0, markerText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
markerText.append("\n");
if (measurement.isAverageValue()) {
markerText.append(getContext().getString(R.string.label_trend) + " ");
}
}
markerText.append(measurementView.getValueAsString(true));
if (prevMeasurement != null) {
markerText.append("\n");
int textPosAfterSymbol = markerText.length() + 1;
measurementView.appendDiffValue(markerText, false);
// set color diff value to text color
if (markerText.length() > textPosAfterSymbol) {
markerText.setSpan(new ForegroundColorSpan(ColorUtil.COLOR_WHITE), textPosAfterSymbol, markerText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
markerText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), 0, markerText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
markerTextField.setText(markerText);
super.refreshContent(e, highlight);
}
use of android.text.style.RelativeSizeSpan in project ExoPlayer by google.
the class SpannedToHtmlConverterTest method convert_supportsRelativeSizeSpan.
@Test
public void convert_supportsRelativeSizeSpan() {
SpannableString spanned = new SpannableString("String with 10% section");
spanned.setSpan(new RelativeSizeSpan(/* proportion= */
0.1f), "String with ".length(), "String with 10%".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannedToHtmlConverter.HtmlAndCss htmlAndCss = SpannedToHtmlConverter.convert(spanned, displayDensity);
assertThat(htmlAndCss.cssRuleSets).isEmpty();
assertThat(htmlAndCss.html).isEqualTo("String with <span style='font-size:10.00%;'>10%</span> section");
}
use of android.text.style.RelativeSizeSpan in project ExoPlayer by google.
the class SubtitleViewUtilsTest method buildCue.
private static Cue buildCue() {
SpannableString spanned = new SpannableString("TextEmphasis おはよ Ruby ございます 123 Underline RelativeSize AbsoluteSize");
spanned.setSpan(new TextEmphasisSpan(TextEmphasisSpan.MARK_SHAPE_CIRCLE, TextEmphasisSpan.MARK_FILL_FILLED, TextAnnotation.POSITION_BEFORE), "Text emphasis ".length(), "Text emphasis おはよ".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanned.setSpan(new RubySpan("おはよ", TextAnnotation.POSITION_BEFORE), "TextEmphasis おはよ Ruby ".length(), "TextEmphasis おはよ Ruby ございます".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanned.setSpan(new HorizontalTextInVerticalContextSpan(), "TextEmphasis おはよ Ruby ございます ".length(), "TextEmphasis おはよ Ruby ございます 123".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanned.setSpan(new UnderlineSpan(), "TextEmphasis おはよ Ruby ございます 123 ".length(), "TextEmphasis おはよ Ruby ございます 123 Underline".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanned.setSpan(new RelativeSizeSpan(1f), "TextEmphasis おはよ Ruby ございます 123 Underline ".length(), "TextEmphasis おはよ Ruby ございます 123 Underline RelativeSize".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanned.setSpan(new AbsoluteSizeSpan(10), "TextEmphasis おはよ Ruby ございます 123 Underline RelativeSize ".length(), "TextEmphasis おはよ Ruby ございます 123 Underline RelativeSize AbsoluteSize".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return new Cue.Builder().setText(spanned).setTextAlignment(Layout.Alignment.ALIGN_CENTER).setMultiRowAlignment(Layout.Alignment.ALIGN_NORMAL).setLine(5, Cue.LINE_TYPE_NUMBER).setLineAnchor(Cue.ANCHOR_TYPE_END).setPosition(0.4f).setPositionAnchor(Cue.ANCHOR_TYPE_MIDDLE).setTextSize(0.2f, Cue.TEXT_SIZE_TYPE_FRACTIONAL).setSize(0.8f).setWindowColor(Color.CYAN).setVerticalType(Cue.VERTICAL_TYPE_RL).setShearDegrees(-15f).build();
}
use of android.text.style.RelativeSizeSpan in project ExoPlayer by google.
the class TtmlRenderUtil method applyStylesToSpan.
public static void applyStylesToSpan(Spannable builder, int start, int end, TtmlStyle style, @Nullable TtmlNode parent, Map<String, TtmlStyle> globalStyles, @Cue.VerticalType int verticalType) {
if (style.getStyle() != TtmlStyle.UNSPECIFIED) {
builder.setSpan(new StyleSpan(style.getStyle()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.isLinethrough()) {
builder.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.isUnderline()) {
builder.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.hasFontColor()) {
SpanUtil.addOrReplaceSpan(builder, new ForegroundColorSpan(style.getFontColor()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.hasBackgroundColor()) {
SpanUtil.addOrReplaceSpan(builder, new BackgroundColorSpan(style.getBackgroundColor()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.getFontFamily() != null) {
SpanUtil.addOrReplaceSpan(builder, new TypefaceSpan(style.getFontFamily()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (style.getTextEmphasis() != null) {
TextEmphasis textEmphasis = checkNotNull(style.getTextEmphasis());
@TextEmphasisSpan.MarkShape int markShape;
@TextEmphasisSpan.MarkFill int markFill;
if (textEmphasis.markShape == TextEmphasis.MARK_SHAPE_AUTO) {
// If a vertical writing mode applies, then 'auto' is equivalent to 'filled sesame';
// otherwise, it's equivalent to 'filled circle':
// https://www.w3.org/TR/ttml2/#style-value-emphasis-style
markShape = (verticalType == Cue.VERTICAL_TYPE_LR || verticalType == Cue.VERTICAL_TYPE_RL) ? TextEmphasisSpan.MARK_SHAPE_SESAME : TextEmphasisSpan.MARK_SHAPE_CIRCLE;
markFill = TextEmphasisSpan.MARK_FILL_FILLED;
} else {
markShape = textEmphasis.markShape;
markFill = textEmphasis.markFill;
}
@TextEmphasis.Position int position;
if (textEmphasis.position == TextEmphasis.POSITION_OUTSIDE) {
// 'outside' is not supported by TextEmphasisSpan, so treat it as 'before':
// https://www.w3.org/TR/ttml2/#style-value-annotation-position
position = TextAnnotation.POSITION_BEFORE;
} else {
position = textEmphasis.position;
}
SpanUtil.addOrReplaceSpan(builder, new TextEmphasisSpan(markShape, markFill, position), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
switch(style.getRubyType()) {
case TtmlStyle.RUBY_TYPE_BASE:
// look for the sibling RUBY_TEXT and add it as span between start & end.
@Nullable TtmlNode containerNode = findRubyContainerNode(parent, globalStyles);
if (containerNode == null) {
// No matching container node
break;
}
@Nullable TtmlNode textNode = findRubyTextNode(containerNode, globalStyles);
if (textNode == null) {
// no matching text node
break;
}
String rubyText;
if (textNode.getChildCount() == 1 && textNode.getChild(0).text != null) {
rubyText = Util.castNonNull(textNode.getChild(0).text);
} else {
Log.i(TAG, "Skipping rubyText node without exactly one text child.");
break;
}
@Nullable TtmlStyle textStyle = resolveStyle(textNode.style, textNode.getStyleIds(), globalStyles);
// Use position from ruby text node if defined.
@TextAnnotation.Position int rubyPosition = textStyle != null ? textStyle.getRubyPosition() : TextAnnotation.POSITION_UNKNOWN;
if (rubyPosition == TextAnnotation.POSITION_UNKNOWN) {
// If ruby position is not defined, use position info from container node.
@Nullable TtmlStyle containerStyle = resolveStyle(containerNode.style, containerNode.getStyleIds(), globalStyles);
rubyPosition = containerStyle != null ? containerStyle.getRubyPosition() : rubyPosition;
}
builder.setSpan(new RubySpan(rubyText, rubyPosition), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case TtmlStyle.RUBY_TYPE_DELIMITER:
// fall through and delete the text.
case TtmlStyle.RUBY_TYPE_TEXT:
// We can't just remove the text directly from `builder` here because TtmlNode has fixed
// ideas of where every node starts and ends (nodeStartsByRegion and nodeEndsByRegion) so
// all these indices become invalid if we mutate the underlying string at this point.
// Instead we add a special span that's then handled in TtmlNode#cleanUpText.
builder.setSpan(new DeleteTextSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case TtmlStyle.RUBY_TYPE_CONTAINER:
case TtmlStyle.UNSPECIFIED:
default:
// Do nothing
break;
}
if (style.getTextCombine()) {
SpanUtil.addOrReplaceSpan(builder, new HorizontalTextInVerticalContextSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
switch(style.getFontSizeUnit()) {
case TtmlStyle.FONT_SIZE_UNIT_PIXEL:
SpanUtil.addOrReplaceSpan(builder, new AbsoluteSizeSpan((int) style.getFontSize(), true), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case TtmlStyle.FONT_SIZE_UNIT_EM:
SpanUtil.addOrReplaceSpan(builder, new RelativeSizeSpan(style.getFontSize()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case TtmlStyle.FONT_SIZE_UNIT_PERCENT:
SpanUtil.addOrReplaceSpan(builder, new RelativeSizeSpan(style.getFontSize() / 100), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case TtmlStyle.UNSPECIFIED:
// Do nothing.
break;
}
}
Aggregations