Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usability of BoringLayout

I'm writing a custom layout that will manage text. Before I started implementing the ViewGroup#onMeasure() method I started to dig the EditText source code, specifically at the EditText#onMeasure() method. So I came across the BoringLayout. I read the docs but I didn't find much explanation on it and how to use it in an actual custom implementation. Then my question is how can I use it the right way and when it is really needed.

like image 905
kaneda Avatar asked Nov 06 '12 15:11

kaneda


2 Answers

BoringLayout is used to draw text on a view. It is called "boring" because it only handles a single line of left-to-right text without any interesting characters such as emoji. This simplification allows the class to override onDraw with more efficient logic than the default does. Here is the source code if you want to see for yourself.

Like StaticLayout and DynamicLayout, the BoringLayout is also a subclass of the abstract Layout class. As the documentation says, you probably wouldn't use these classes directly unless you are making your own text handling widget. How do you know if you should be using one of these classes? If you are thinking about using Canvas.drawText in your custom view, then you should probably think about using a Layout. They also eventually call Canvas.drawText, but they do a lot of other processing beforehand.

If you are making your own text widget, then you would only use the BoringLayout for single line, simple, left-to-right text. For multi-line and more complex text use a StaticLayout. And if you need to dynamically change the text after creation, then use a DynamicLayout.

like image 62
Suragch Avatar answered Nov 01 '22 18:11

Suragch


Technically, you can draw text on the canvas with `canvas.drawText("text");
However, text is a very general term and can get extremely complicated : is it LeftToRight or RightToLeft ? is it Ellipsized ? is it single or multiLine ? ...

android.text.Layout is here to handle all this.

the typical way to use it is :

        BoringLayout.Metrics boring = BoringLayout.isBoring(mText, mPaint);
    if (boring != null) {
        // this is boring !
        if (mSavedLayout != null) {
            mLayout = mSavedLayout.replaceOrMake(mText,
                    mPaint,
                    availableWidth,
                    Layout.Alignment.ALIGN_NORMAL,
                    SPACING_MULT,
                    SPACING_ADD,
                    boring,
                    true,
                    TextUtils.TruncateAt.END,
                    availableWidth);
        } else {
            mLayout = BoringLayout.make(mText,
                    mPaint,
                    availableWidth,
                    Layout.Alignment.ALIGN_NORMAL,
                    SPACING_MULT,
                    SPACING_ADD,
                    boring,
                    true,
                    TextUtils.TruncateAt.END,
                    availableWidth);
        }
        mSavedLayout = (BoringLayout) mLayout;
    } else {
        mLayout = new StaticLayout(mText,
                0,
                mText.length(),
                mPaint,
                availableWidth,
                Layout.Alignment.ALIGN_NORMAL,
                SPACING_MULT,
                SPACING_ADD,
                true,
                TextUtils.TruncateAt.END,
                availableWidth);
    }
like image 26
Teovald Avatar answered Nov 01 '22 18:11

Teovald