Example usage for android.text StaticLayout getHeight

List of usage examples for android.text StaticLayout getHeight

Introduction

In this page you can find the example usage for android.text StaticLayout getHeight.

Prototype

public int getHeight() 

Source Link

Document

Return the total height of this layout.

Usage

From source file:Main.java

public static int drawStaticLayout(Canvas canvas, StaticLayout layout, int x, int y) {
    canvas.translate(x, y);//from  w w  w. j  a va 2  s .  c om
    layout.draw(canvas);
    canvas.translate(-x, -y);
    return layout.getHeight();
}

From source file:Main.java

static int getTextHeight(CharSequence text, TextPaint paint, int targetWidth, float textSize) {
    TextPaint paintCopy = new TextPaint(paint);
    paintCopy.setTextSize(textSize);//from   w  ww .j  ava2 s.  c  om
    StaticLayout layout = new StaticLayout(text, paintCopy, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL,
            1.0f, 0.0f, true);
    return layout.getHeight();
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawWordmarkFromText(@NonNull Context context, @NonNull Canvas canvas,
        boolean isArticleRTL) {
    final int maxWidth = WIDTH - DESCRIPTION_WIDTH - 2 * HORIZONTAL_PADDING;
    final float fontSize = 20.0f;
    final float scaleX = 1.06f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);//from  w w  w .j a  v a 2s  .  c o m
    textPaint.setColor(Color.LTGRAY);
    textPaint.setTextSize(fontSize);
    textPaint.setTypeface(SERIF);
    textPaint.setTextScaleX(scaleX);

    Spanned wikipedia = StringUtil.fromHtml(context.getString(R.string.wp_stylized));
    Layout.Alignment align = L10nUtil.isDeviceRTL() ? ALIGN_OPPOSITE : ALIGN_NORMAL;
    StaticLayout wordmarkLayout = buildLayout(
            new TextLayoutParams(wikipedia, textPaint, maxWidth, 1.0f, align));
    final int width = (int) wordmarkLayout.getLineWidth(0);
    final int height = wordmarkLayout.getHeight();

    final int bottom = HEIGHT - BOTTOM_PADDING;
    final int top = bottom - height;

    int left = WIDTH - HORIZONTAL_PADDING - width;
    if (isArticleRTL) {
        left = HORIZONTAL_PADDING;
    }

    canvas.save(); // --
    canvas.translate(left, top);
    wordmarkLayout.draw(canvas);
    canvas.restore(); // --
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

@NonNull
private static Layout drawTextSnippet(@NonNull Canvas canvas, @NonNull CharSequence textSnippet) {
    final int top = TOP_PADDING;
    final int maxHeight = 225;
    final int maxLines = 5;
    final float maxFontSize = 195.0f;
    final float minFontSize = 32.0f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);//from   w w  w  .j ava 2s .  co  m
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(Typeface.DEFAULT_BOLD);
    textPaint.setShadowLayer(1.0f, 1.0f, 1.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(textSnippet, textPaint, TEXT_WIDTH, SPACING_MULTIPLIER), maxHeight, maxLines,
            maxFontSize, minFontSize);

    canvas.save();
    int horizontalCenterOffset = top + (maxHeight - textLayout.getHeight()) / QUARTER;
    canvas.translate(HORIZONTAL_PADDING, horizontalCenterOffset);
    textLayout.draw(canvas);
    canvas.restore();

    return textLayout;
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

/**
 * If the title or text is too long we first reduce the font size.
 * If that is not enough it gets ellipsized.
 *//*w  w  w. j  a va2  s .com*/
private static StaticLayout optimizeTextSize(TextLayoutParams params, int maxHeight, int maxLines,
        float maxFontSize, float minFontSize) {
    final float threshold1 = 60.0f;
    final float threshold2 = 40.0f;
    final float extraStep1 = 3.0f;
    final float extraStep2 = 1.0f;
    boolean fits = false;
    StaticLayout textLayout = null;

    // Try decreasing font size first
    for (float fontSize = maxFontSize; fontSize >= minFontSize; fontSize -= 1.0f) {
        params.textPaint.setTextSize(fontSize);
        textLayout = buildLayout(params);
        if (textLayout.getHeight() <= maxHeight) {
            fits = true;
            break;
        }

        // make it go faster at the beginning...
        if (fontSize > threshold1) {
            fontSize -= extraStep1;
        } else if (fontSize > threshold2) {
            fontSize -= extraStep2;
        }
    }

    // Then do own ellipsize: cut text off after last fitting space and add "..."
    // Didn't want to cut off randomly in the middle of a line or word.
    if (!fits) {
        final String textStr = params.text.toString();
        final int ellipsisLength = 3;
        final int ellipsisStart = textLayout != null ? textLayout.getLineStart(maxLines) - ellipsisLength
                : textStr.length();
        final int end = textStr.lastIndexOf(' ', ellipsisStart) + 1;
        if (end > 0) {
            textLayout = buildLayout(new TextLayoutParams(params, textStr.substring(0, end) + "..."));
            if (textLayout.getLineCount() <= maxLines) {
                fits = true;
            }
        }
    }

    // last resort: use TextUtils.ellipsize()
    if (!fits) {
        final float textRatio = .87f;
        final float maxWidth = textRatio * maxLines * params.lineWidth;
        textLayout = buildLayout(new TextLayoutParams(params,
                TextUtils.ellipsize(params.text, params.textPaint, maxWidth, TextUtils.TruncateAt.END)));
    }

    return textLayout;
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static int drawDescription(@NonNull Canvas canvas, @Nullable String description, int top,
        boolean isArticleRTL) {
    final int marginBottom = 5;
    final int maxHeight = 23;
    final int maxLines = 2;
    final float maxFontSize = 15.0f;
    final float minFontSize = 10.0f;

    if (TextUtils.isEmpty(description)) {
        return top - marginBottom;
    }//from  w  w  w  .  ja v a2 s.  co m
    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setShadowLayer(1.0f, 0.0f, 0.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(description, textPaint, DESCRIPTION_WIDTH, SPACING_MULTIPLIER), maxHeight,
            maxLines, maxFontSize, minFontSize);
    int left = HORIZONTAL_PADDING;
    if (isArticleRTL) {
        left = WIDTH - HORIZONTAL_PADDING - textLayout.getWidth();
    }

    top = top - marginBottom - textLayout.getHeight();
    canvas.save();
    canvas.translate(left, top);
    textLayout.draw(canvas);
    canvas.restore();

    return top;
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawTitle(@NonNull Canvas canvas, @NonNull String title, int top, boolean isArticleRTL) {
    final int marginBottom = 0;
    final int maxHeight = 70;
    final int maxLines = 2;
    final float maxFontSize = 30.0f;
    final float spacingMultiplier = 0.7f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);//from w ww  . ja  va 2  s.  co m
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(SERIF);
    textPaint.setShadowLayer(1.0f, 0.0f, 1.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(title, textPaint, DESCRIPTION_WIDTH, spacingMultiplier), maxHeight, maxLines,
            maxFontSize, maxFontSize);
    int left = HORIZONTAL_PADDING;
    if (isArticleRTL) {
        left = WIDTH - HORIZONTAL_PADDING - textLayout.getWidth();
    }
    int marginBottomTotal = marginBottom;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // versions < 5.0 don't compensate for bottom margin correctly when line
        // spacing is less than 1.0, so we'll compensate ourselves
        final int marginBoost = 10;
        marginBottomTotal += marginBoost;
    }

    top = top - marginBottomTotal - textLayout.getHeight();
    canvas.save();
    canvas.translate(left, top);
    textLayout.draw(canvas);
    canvas.restore();
}

From source file:com.savvasdalkitsis.betwixt.demo.InterpolatorView.java

public void setDescription(String description) {
    this.description = description;
    StaticLayout textLayout = new StaticLayout(description, textPaint, Integer.MAX_VALUE,
            Layout.Alignment.ALIGN_CENTER, 0, 0, false);
    textHeight = textLayout.getHeight();
}

From source file:com.ifoer.util.NetPOSPrinter.java

public Bitmap drawBitSecond(String s) {
    if (this.company_address == null) {
        this.company_address = XmlPullParser.NO_NAMESPACE;
    }//from ww w. j  a  va 2s  . co m
    if (this.company_phone == null) {
        this.company_phone = XmlPullParser.NO_NAMESPACE;
    }
    if (this.license_plate_number == null) {
        this.license_plate_number = XmlPullParser.NO_NAMESPACE;
    }
    StringBuffer ss = new StringBuffer();
    ss.append(new StringBuilder(
            String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_time)))
                    .append(this.str).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(new StringBuilder(
            String.valueOf(this.mContext.getResources().getString(C0136R.string.print_serial_number)))
                    .append(this.serialNum).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(new StringBuilder(
            String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_company_address)))
                    .append(this.company_address).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(new StringBuilder(
            String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_company_phone)))
                    .append(this.company_phone).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(new StringBuilder(String
            .valueOf(this.mContext.getResources().getString(C0136R.string.print_test_license_plate_number)))
                    .append(this.license_plate_number).append(SpecilApiUtil.LINE_SEP).toString());
    ss.append(s);
    this.textPaint.setColor(DefaultRenderer.BACKGROUND_COLOR);
    this.textPaint.setTextSize(20.0f);
    StaticLayout layout = new StaticLayout(ss, this.textPaint, PRINT_WIDTH, Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            true);
    this.nBitmapSecond = Bitmap.createBitmap(PRINT_WIDTH, layout.getHeight(), Config.RGB_565);
    Canvas canvas = new Canvas(this.nBitmapSecond);
    canvas.drawColor(-1);
    layout.draw(canvas);
    return this.nBitmapSecond;
}

From source file:com.mjhram.geodata.GpsMainActivity.java

public Bitmap drawMultilineTextToBitmap(Context gContext, Bitmap bitmap, String gText) {
    // prepare canvas
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    //Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }/*from w w  w. jav  a 2s.com*/
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);

    // new antialiased Paint
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (20 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
    //canvas.drawText("This is", 100, 100, paint);
    //canvas.drawText("multi-line", 100, 150, paint);
    //canvas.drawText("text", 100, 200, paint);

    // set text width to canvas width minus 16dp padding
    int textWidth = canvas.getWidth() - (int) (16 * scale);

    // init StaticLayout for text
    StaticLayout textLayout = new StaticLayout(gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f,
            0.0f, false);

    // get height of multiline text
    int textHeight = textLayout.getHeight();

    // get position of text's top left corner
    float x = (bitmap.getWidth() - textWidth) / 2;
    float y = bitmap.getHeight() - textHeight;

    // draw text to the Canvas center
    canvas.save();
    canvas.translate(x, y);
    textLayout.draw(canvas);
    canvas.restore();

    return bitmap;
}