android.text.style.LineBackgroundSpan Java Examples

The following examples show how to use android.text.style.LineBackgroundSpan. 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: ComposedFrame.java    From Tehreer-Android with Apache License 2.0 5 votes vote down vote up
private void drawBackground(@NonNull Canvas canvas) {
    int frameLeft = 0;
    int frameRight = (int) (mWidth + 0.5f);

    int lineCount = lineList.size();
    for (int i = 0; i < lineCount; i++) {
        ComposedLine composedLine = lineList.get(i);
        Object[] lineSpans = composedLine.getSpans();

        for (Object style : lineSpans) {
            if (style instanceof LineBackgroundSpan) {
                LineBackgroundSpan span = (LineBackgroundSpan) style;

                Spanned sourceText = (Spanned) source;
                int spanStart = sourceText.getSpanStart(span);
                int spanEnd = sourceText.getSpanEnd(span);

                int lineStart = composedLine.getCharStart();
                int lineEnd = composedLine.getCharEnd();
                if (lineStart >= spanEnd || lineEnd <= spanStart) {
                    continue;
                }

                Paint paint = lazyPaint();
                int lineTop = (int) (composedLine.getTop() + 0.5f);
                int lineBaseline = (int) (composedLine.getOriginY() + 0.5f);
                int lineBottom = (int) (composedLine.getTop() + composedLine.getHeight() + 0.5f);

                span.drawBackground(canvas, paint, frameLeft, frameRight,
                                    lineTop, lineBaseline, lineBottom,
                                    sourceText, lineStart, lineEnd, i);
            }
        }
    }
}
 
Example #2
Source File: Layout.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * @hide
 */
public void drawBackground(Canvas canvas, Path highlight, Paint highlightPaint,
        int cursorOffsetVertical, int firstLine, int lastLine) {
    // First, draw LineBackgroundSpans.
    // LineBackgroundSpans know nothing about the alignment, margins, or
    // direction of the layout or line.  XXX: Should they?
    // They are evaluated at each line.
    if (mSpannedText) {
        if (mLineBackgroundSpans == null) {
            mLineBackgroundSpans = new SpanSet<LineBackgroundSpan>(LineBackgroundSpan.class);
        }

        Spanned buffer = (Spanned) mText;
        int textLength = buffer.length();
        mLineBackgroundSpans.init(buffer, 0, textLength);

        if (mLineBackgroundSpans.numberOfSpans > 0) {
            int previousLineBottom = getLineTop(firstLine);
            int previousLineEnd = getLineStart(firstLine);
            ParagraphStyle[] spans = NO_PARA_SPANS;
            int spansLength = 0;
            TextPaint paint = mPaint;
            int spanEnd = 0;
            final int width = mWidth;
            for (int i = firstLine; i <= lastLine; i++) {
                int start = previousLineEnd;
                int end = getLineStart(i + 1);
                previousLineEnd = end;

                int ltop = previousLineBottom;
                int lbottom = getLineTop(i + 1);
                previousLineBottom = lbottom;
                int lbaseline = lbottom - getLineDescent(i);

                if (start >= spanEnd) {
                    // These should be infrequent, so we'll use this so that
                    // we don't have to check as often.
                    spanEnd = mLineBackgroundSpans.getNextTransition(start, textLength);
                    // All LineBackgroundSpans on a line contribute to its background.
                    spansLength = 0;
                    // Duplication of the logic of getParagraphSpans
                    if (start != end || start == 0) {
                        // Equivalent to a getSpans(start, end), but filling the 'spans' local
                        // array instead to reduce memory allocation
                        for (int j = 0; j < mLineBackgroundSpans.numberOfSpans; j++) {
                            // equal test is valid since both intervals are not empty by
                            // construction
                            if (mLineBackgroundSpans.spanStarts[j] >= end ||
                                    mLineBackgroundSpans.spanEnds[j] <= start) continue;
                            spans = GrowingArrayUtils.append(
                                    spans, spansLength, mLineBackgroundSpans.spans[j]);
                            spansLength++;
                        }
                    }
                }

                for (int n = 0; n < spansLength; n++) {
                    LineBackgroundSpan lineBackgroundSpan = (LineBackgroundSpan) spans[n];
                    lineBackgroundSpan.drawBackground(canvas, paint, 0, width,
                            ltop, lbaseline, lbottom,
                            buffer, start, end, i);
                }
            }
        }
        mLineBackgroundSpans.recycle();
    }

    // There can be a highlight even without spans if we are drawing
    // a non-spanned transformation of a spanned editing buffer.
    if (highlight != null) {
        if (cursorOffsetVertical != 0) canvas.translate(0, cursorOffsetVertical);
        canvas.drawPath(highlight, highlightPaint);
        if (cursorOffsetVertical != 0) canvas.translate(0, -cursorOffsetVertical);
    }
}