Java Code Examples for android.text.Layout#getLineBottom()

The following examples show how to use android.text.Layout#getLineBottom() . 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: BetterLinkMovementExtended.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
private BetterLinkMovementExtended.ClickableSpanWithText findClickableSpanUnderTouch(TextView textView, Spannable text, MotionEvent event) {
    int touchX = (int) event.getX();
    int touchY = (int) event.getY();
    touchX -= textView.getTotalPaddingLeft();
    touchY -= textView.getTotalPaddingTop();
    touchX += textView.getScrollX();
    touchY += textView.getScrollY();
    Layout layout = textView.getLayout();
    int touchedLine = layout.getLineForVertical(touchY);
    int touchOffset = layout.getOffsetForHorizontal(touchedLine, (float) touchX);
    this.touchedLineBounds.left = layout.getLineLeft(touchedLine);
    this.touchedLineBounds.top = (float) layout.getLineTop(touchedLine);
    this.touchedLineBounds.right = layout.getLineWidth(touchedLine) + this.touchedLineBounds.left;
    this.touchedLineBounds.bottom = (float) layout.getLineBottom(touchedLine);
    if (this.touchedLineBounds.contains((float) touchX, (float) touchY)) {
        Object[] spans = text.getSpans(touchOffset, touchOffset, SPAN_CLASS);
        for (Object span : spans) {
            if (span instanceof ClickableSpan) {
                return ClickableSpanWithText.ofSpan(textView, (ClickableSpan) span);
            }
        }
        return null;
    } else {
        return null;
    }
}
 
Example 2
Source File: CutTextView.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	int height = getHeight();
	Layout layout = getLayout();
	int maxHeight = 0;
	for (int i = 0; i < layout.getLineCount(); i++) {
		int bottom = layout.getLineBottom(i);
		if (bottom > height) {
			maxHeight = layout.getLineTop(i);
			break;
		}
	}
	if (maxHeight > 0) {
		canvas.save();
		canvas.clipRect(0, 0, getWidth(), maxHeight);
	}
	super.onDraw(canvas);
	OverlineSpan.draw(this, canvas);
	if (maxHeight > 0) {
		canvas.restore();
	}
}
 
Example 3
Source File: AccurateMovementMethod.java    From social-text-view with MIT License 5 votes vote down vote up
/**
 * Gets the span that was touched.
 * @param tv {@link TextView}
 * @param span {@link Spannable}
 * @param e {@link MotionEvent}
 * @return {@link TouchableSpan}
 */
private TouchableSpan getTouchedSpan(TextView tv, Spannable span, MotionEvent e) {
    // Find the location in which the touch was made
    int x = (int)e.getX();
    int y = (int)e.getY();

    // Ignore padding
    x -= tv.getTotalPaddingLeft();
    y -= tv.getTotalPaddingTop();

    // Account for scrollable text
    x += tv.getScrollX();
    y += tv.getScrollY();

    final Layout layout = tv.getLayout();
    final int touchedLine = layout.getLineForVertical(y);
    final int touchOffset = layout.getOffsetForHorizontal(touchedLine, x);

    // Set bounds of the touched line
    touchBounds.left = layout.getLineLeft(touchedLine);
    touchBounds.top = layout.getLineTop(touchedLine);
    touchBounds.right = layout.getLineRight(touchedLine);
    touchBounds.bottom = layout.getLineBottom(touchedLine);

    // Ensure the span falls within the bounds of the touch
    TouchableSpan touchSpan = null;
    if (touchBounds.contains(x, y)) {
        // Find clickable spans that lie under the touched area
        TouchableSpan[] spans = span.getSpans(touchOffset, touchOffset, TouchableSpan.class);
        touchSpan = (spans.length > 0) ? spans[0] : null;
    }

    return touchSpan;
}
 
Example 4
Source File: CodeTextView.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private static float getLineBottomWithoutSpacing(@NonNull Layout layout, int line) {
    final float value = layout.getLineBottom(line);

    final boolean isLastLineSpacingNotAdded = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    final boolean isLastLine = line == (layout.getLineCount() - 1);

    final float lineBottomWithoutSpacing;

    final float lineSpacingExtra = layout.getSpacingAdd();
    final float lineSpacingMultiplier = layout.getSpacingMultiplier();

    final boolean hasLineSpacing = Float.compare(lineSpacingExtra, .0F) != 0
            || Float.compare(lineSpacingMultiplier, 1F) != 0;

    if (!hasLineSpacing || isLastLine && isLastLineSpacingNotAdded) {
        lineBottomWithoutSpacing = value;
    } else {
        final float extra;
        if (Float.compare(lineSpacingMultiplier, 1F) != 0) {
            final float lineHeight = getLineHeight(layout, line);
            extra = lineHeight - (lineHeight - lineSpacingExtra) / lineSpacingMultiplier;
        } else {
            extra = lineSpacingExtra;
        }
        lineBottomWithoutSpacing = value - extra;
    }

    return lineBottomWithoutSpacing;
}
 
Example 5
Source File: TextProcessor.java    From CodeEditor with Apache License 2.0 4 votes vote down vote up
@Override
public void onDraw(@NonNull Canvas canvas) {
    if (!isInEditMode()) {
        int top;
        Layout layout = getLayout();
        if (layout != null && codeEditor != null && mHighlightCurrentLine) {
            int currentLineStart = codeEditor.getLineForIndex(getSelectionStart());
            if (currentLineStart == codeEditor.getLineForIndex(getSelectionEnd())) {
                int selectedLineStartIndex = codeEditor.getIndexForStartOfLine(currentLineStart);
                int selectedLineEndIndex = codeEditor.getIndexForEndOfLine(currentLineStart);
                int topVisualLine = layout.getLineForOffset(selectedLineStartIndex);
                int bottomVisualLine = layout.getLineForOffset(selectedLineEndIndex);
                int left = mGutterWidth;
                if (!mShowLineNumbers)
                    left = 0; //убираем отступ для Paint'а если номера строк выключены
                top = layout.getLineTop(topVisualLine) + getPaddingTop();
                int right = (layout.getWidth() + getPaddingLeft()) + getPaddingRight();
                int bottom = layout.getLineBottom(bottomVisualLine) + getPaddingTop();
                canvas.drawRect(left, top, right, bottom, mSelectedLinePaint);
            }
        }
        super.onDraw(canvas);
        if (layout != null && mShowLineNumbers) {
            int prevLineNumber = -1;
            canvas.drawRect(getScrollX(), getScrollY(),
                    mGutterWidth + getScrollX(),
                    getScrollY() + getHeight(), mGutterBackgroundPaint);
            int paddingTop = getPaddingTop();
            int max = mLineUtils.getBottomVisibleLine(this);
            int textRight = (mGutterWidth - mIdealMargin / 2) + getScrollX();
            if (codeEditor != null) {
                int i = mLineUtils.getTopVisibleLine(this);
                if (i >= 2) {
                    i -= 2;
                } else {
                    i = 0;
                }
                while (i <= max) {
                    int number = codeEditor.getLineForIndex(getLayout().getLineStart(i));
                    if (number != prevLineNumber) {
                        canvas.drawText(Integer.toString(number + 1), textRight,
                                layout.getLineBaseline(i) + paddingTop, mLineNumberPaint);
                    }
                    prevLineNumber = number;
                    i++;
                }
                top = getScrollY();
                canvas.drawLine(mGutterWidth + getScrollX(), top,
                        mGutterWidth + getScrollX(), top + getHeight(), mLinePaint);
            }
        }
    }
}
 
Example 6
Source File: LayoutUtils.java    From Markwon with Apache License 2.0 4 votes vote down vote up
public static int getLineBottomWithoutPaddingAndSpacing(
        @NonNull Layout layout,
        int line
) {

    final int bottom = layout.getLineBottom(line);
    final boolean lastLineSpacingNotAdded = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    final boolean isSpanLastLine = line == (layout.getLineCount() - 1);

    final int lineBottom;
    final float lineSpacingExtra = layout.getSpacingAdd();
    final float lineSpacingMultiplier = layout.getSpacingMultiplier();

    // simplified check
    final boolean hasLineSpacing = lineSpacingExtra != DEFAULT_EXTRA
            || lineSpacingMultiplier != DEFAULT_MULTIPLIER;

    if (!hasLineSpacing
            || (isSpanLastLine && lastLineSpacingNotAdded)) {
        lineBottom = bottom;
    } else {
        final float extra;
        if (Float.compare(DEFAULT_MULTIPLIER, lineSpacingMultiplier) != 0) {
            final int lineHeight = getLineHeight(layout, line);
            extra = lineHeight -
                    ((lineHeight - lineSpacingExtra) / lineSpacingMultiplier);
        } else {
            extra = lineSpacingExtra;
        }
        lineBottom = (int) (bottom - extra + .5F);
    }

    // check if it is the last line that span is occupying **and** that this line is the last
    //  one in TextView
    if (isSpanLastLine
            && (line == layout.getLineCount() - 1)) {
        return lineBottom - layout.getBottomPadding();
    }

    return lineBottom;
}