Example usage for android.text StaticLayout getWidth

List of usage examples for android.text StaticLayout getWidth

Introduction

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

Prototype

public final int getWidth() 

Source Link

Document

Return the width of this layout.

Usage

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  w w  .  j a va 2  s .c  o 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: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  www . j ava  2s  .c  o 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:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

/**
 * Return the layout for a numbered event. Create it if not already existing
 *//* w  w  w .  j a  v  a 2 s.  co m*/
private StaticLayout getEventLayout(StaticLayout[] layouts, int i, Event event, Paint paint, Rect r) {
    if (i < 0 || i >= layouts.length) {
        return null;
    }

    StaticLayout layout = layouts[i];
    // Check if we have already initialized the StaticLayout and that
    // the width hasn't changed (due to vertical resizing which causes
    // re-layout of events at min height)
    if (layout == null || r.width() != layout.getWidth()) {
        SpannableStringBuilder bob = new SpannableStringBuilder();
        if (event.title != null) {
            // MAX - 1 since we add a space
            bob.append(drawTextSanitizer(event.title.toString(), MAX_EVENT_TEXT_LEN - 1));
            bob.setSpan(new StyleSpan(Typeface.BOLD), 0, bob.length(), 0);
            bob.append(' ');
        }
        if (event.location != null) {
            bob.append(drawTextSanitizer(event.location.toString(), MAX_EVENT_TEXT_LEN - bob.length()));
        }

        paint.setColor(mEventTextColor);

        // Leave a one pixel boundary on the left and right of the rectangle for the event
        layout = new StaticLayout(bob, 0, bob.length(), new TextPaint(paint), r.width(), Alignment.ALIGN_NORMAL,
                1.0f, 0.0f, true, null, r.width());

        layouts[i] = layout;
    }
    layout.getPaint().setAlpha(mEventsAlpha);
    return layout;
}

From source file:org.telegram.ui.ArticleViewer.java

private boolean checkLayoutForLinks(MotionEvent event, View parentView, StaticLayout layout, int layoutX,
        int layoutY) {
    if (parentView == null || layout == null) {
        return false;
    }/*from  w  w w .j  a va 2s  .  c om*/
    CharSequence text = layout.getText();
    if (!(text instanceof Spannable)) {
        return false;
    }
    int x = (int) event.getX();
    int y = (int) event.getY();
    boolean removeLink = false;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (x >= layoutX && x <= layoutX + layout.getWidth() && y >= layoutY
                && y <= layoutY + layout.getHeight()) {
            try {
                int checkX = x - layoutX;
                int checkY = y - layoutY;
                final int line = layout.getLineForVertical(checkY);
                final int off = layout.getOffsetForHorizontal(line, checkX);
                final float left = layout.getLineLeft(line);
                if (left <= checkX && left + layout.getLineWidth(line) >= checkX) {
                    Spannable buffer = (Spannable) layout.getText();
                    TextPaintUrlSpan[] link = buffer.getSpans(off, off, TextPaintUrlSpan.class);
                    if (link != null && link.length > 0) {
                        pressedLink = link[0];
                        int pressedStart = buffer.getSpanStart(pressedLink);
                        int pressedEnd = buffer.getSpanEnd(pressedLink);
                        for (int a = 1; a < link.length; a++) {
                            TextPaintUrlSpan span = link[a];
                            int start = buffer.getSpanStart(span);
                            int end = buffer.getSpanEnd(span);
                            if (pressedStart > start || end > pressedEnd) {
                                pressedLink = span;
                                pressedStart = start;
                                pressedEnd = end;
                            }
                        }
                        pressedLinkOwnerLayout = layout;
                        pressedLinkOwnerView = parentView;
                        try {
                            urlPath.setCurrentLayout(layout, pressedStart, 0);
                            layout.getSelectionPath(pressedStart, pressedEnd, urlPath);
                            parentView.invalidate();
                        } catch (Exception e) {
                            FileLog.e(e);
                        }
                    }
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (pressedLink != null) {
            removeLink = true;
            String url = pressedLink.getUrl();
            if (url != null) {
                int index;
                boolean isAnchor = false;
                final String anchor;
                if ((index = url.lastIndexOf('#')) != -1) {
                    anchor = url.substring(index + 1);
                    if (url.toLowerCase().contains(currentPage.url.toLowerCase())) {
                        Integer row = anchors.get(anchor);
                        if (row != null) {
                            layoutManager.scrollToPositionWithOffset(row, 0);
                            isAnchor = true;
                        }
                    }
                } else {
                    anchor = null;
                }
                if (!isAnchor) {
                    if (openUrlReqId == 0) {
                        showProgressView(true);
                        final TLRPC.TL_messages_getWebPage req = new TLRPC.TL_messages_getWebPage();
                        req.url = pressedLink.getUrl();
                        req.hash = 0;
                        openUrlReqId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                            @Override
                            public void run(final TLObject response, TLRPC.TL_error error) {
                                AndroidUtilities.runOnUIThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (openUrlReqId == 0) {
                                            return;
                                        }
                                        openUrlReqId = 0;
                                        showProgressView(false);
                                        if (isVisible) {
                                            if (response instanceof TLRPC.TL_webPage
                                                    && ((TLRPC.TL_webPage) response).cached_page instanceof TLRPC.TL_pageFull) {
                                                addPageToStack((TLRPC.TL_webPage) response, anchor);
                                            } else {
                                                Browser.openUrl(parentActivity, req.url);
                                            }
                                        }
                                    }
                                });
                            }
                        });
                    }
                }
            }
        }
    } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
        removeLink = true;
    }
    if (removeLink && pressedLink != null) {
        pressedLink = null;
        pressedLinkOwnerLayout = null;
        pressedLinkOwnerView = null;
        parentView.invalidate();
    }
    if (pressedLink != null && event.getAction() == MotionEvent.ACTION_DOWN) {
        startCheckLongPress();
    }
    if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) {
        cancelCheckLongPress();
    }
    return pressedLink != null;
}