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:android.support.v7.widget.AppCompatTextViewAutoSizeHelper.java

private boolean suggestedSizeFitsInSpace(int suggestedSizeInPx, RectF availableSpace) {
    CharSequence text = mTextView.getText();
    TransformationMethod transformationMethod = mTextView.getTransformationMethod();
    if (transformationMethod != null) {
        CharSequence transformedText = transformationMethod.getTransformation(text, mTextView);
        if (transformedText != null) {
            text = transformedText;/*from   w w  w .  jav  a  2s  .  c o m*/
        }
    }

    final int maxLines = Build.VERSION.SDK_INT >= 16 ? mTextView.getMaxLines() : -1;
    if (mTempTextPaint == null) {
        mTempTextPaint = new TextPaint();
    } else {
        mTempTextPaint.reset();
    }
    mTempTextPaint.set(mTextView.getPaint());
    mTempTextPaint.setTextSize(suggestedSizeInPx);

    // Needs reflection call due to being private.
    Layout.Alignment alignment = invokeAndReturnWithDefault(mTextView, "getLayoutAlignment",
            Layout.Alignment.ALIGN_NORMAL);
    final StaticLayout layout = Build.VERSION.SDK_INT >= 23
            ? createStaticLayoutForMeasuring(text, alignment, Math.round(availableSpace.right), maxLines)
            : createStaticLayoutForMeasuringPre23(text, alignment, Math.round(availableSpace.right));
    // Lines overflow.
    if (maxLines != -1 && (layout.getLineCount() > maxLines
            || (layout.getLineEnd(layout.getLineCount() - 1)) != text.length())) {
        return false;
    }

    // Height overflow.
    if (layout.getHeight() > availableSpace.bottom) {
        return false;
    }

    return true;
}

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;
    }/*  ww w .j a  v  a2  s  . 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;
}