Java Code Examples for android.text.StaticLayout#Builder

The following examples show how to use android.text.StaticLayout#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaticLayoutEx.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
public static StaticLayout createStaticLayout2(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsisWidth, int maxLines) {
    if (Build.VERSION.SDK_INT >= 23) {
        StaticLayout.Builder builder = StaticLayout.Builder.obtain(source, 0, source.length(), paint, ellipsisWidth)
                .setAlignment(align)
                .setLineSpacing(spacingadd, spacingmult)
                .setIncludePad(includepad)
                .setEllipsize(TextUtils.TruncateAt.END)
                .setEllipsizedWidth(ellipsisWidth)
                .setMaxLines(maxLines)
                .setBreakStrategy(StaticLayout.BREAK_STRATEGY_HIGH_QUALITY)
                .setHyphenationFrequency(StaticLayout.HYPHENATION_FREQUENCY_NONE);
        return builder.build();
    } else {
        return createStaticLayout(source, 0, source.length(), paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsisWidth, maxLines, true);
    }
}
 
Example 2
Source File: StaticLayoutEx.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
public static StaticLayout createStaticLayout2(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsisWidth, int maxLines) {
    if (Build.VERSION.SDK_INT >= 23) {
        StaticLayout.Builder builder = StaticLayout.Builder.obtain(source, 0, source.length(), paint, ellipsisWidth)
                .setAlignment(align)
                .setLineSpacing(spacingadd, spacingmult)
                .setIncludePad(includepad)
                .setEllipsize(TextUtils.TruncateAt.END)
                .setEllipsizedWidth(ellipsisWidth)
                .setMaxLines(maxLines)
                .setBreakStrategy(StaticLayout.BREAK_STRATEGY_HIGH_QUALITY)
                .setHyphenationFrequency(StaticLayout.HYPHENATION_FREQUENCY_NONE);
        return builder.build();
    } else {
        return createStaticLayout(source, 0, source.length(), paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsisWidth, maxLines, true);
    }
}
 
Example 3
Source File: ReflowText.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
private Layout createLayout(ReflowData data, Context context, boolean enforceMaxLines) {
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(data.textSize);
    paint.setColor(data.textColor);
    paint.setLetterSpacing(data.letterSpacing);
    if (data.fontName != null) {
        paint.setTypeface(FontUtil.get(context, data.fontName));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        StaticLayout.Builder builder =  StaticLayout.Builder.obtain(
                data.text, 0, data.text.length(), paint, data.textWidth)
                .setLineSpacing(data.lineSpacingAdd, data.lineSpacingMult)
                .setBreakStrategy(data.breakStrategy);
        if (enforceMaxLines && data.maxLines != -1) {
            builder.setMaxLines(data.maxLines);
            builder.setEllipsize(TextUtils.TruncateAt.END);
        }
        return builder.build();
    } else {
        return new StaticLayout(
                data.text,
                paint,
                data.textWidth,
                Layout.Alignment.ALIGN_NORMAL,
                data.lineSpacingMult,
                data.lineSpacingAdd,
                true);
    }
}
 
Example 4
Source File: StaticLayoutBuilderCompat.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
/** 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);
  }
}