Published by Florina Muntenescu, Development and Extension Commissioner, and Siyamed Sinir, Technical Director of Android Text
In “What’s new in Android P Beta” we’ve talked about two new text features for Android. Now that Android P Beta 3 and eventually API have been launched, we should also thoroughly explore what new text features it has. As we all know, TextView is one of the most critical components of the Android view system. This is why we have been investing in developers and users’ features and API improvements.
The text display is very complicated. Its characteristics include: multiple fonts, line spacing, word spacing, text orientation, broken lines, and character connections. To measure and lay out a given text, TextView must do a lot of work, such as reading font files, finding graphemes, deciding shapes, measuring bounds, and caching text in an internal text cache. More importantly, all this work is performed in UI threads, which may lead to a decrease in the number of app frames.
We found that the time spent on text measurement accounted for 90% of the text setting. To solve this problem, we introduced a new API in Android P and as part of Jetpack: PrecomputedText. The API was previously accessible in API 14 via PrecomputedTextCompat.
PrecomputedText enables the app to perform the most time-consuming part of the text layout in advance or even in the background thread to cache layout results and return valuable measurement data. You can then set the results of PrecomputedText.create (CharSequence, params) in TextView. In this way, only about 10% of the work is left to TextView execution.
Proportion of time spent measuring and layout text
Even with features such as smart text selection, accurate text selection is still difficult. Android P introduced text amplifiers to enhance the user experience in text selection. Since the amplifier provides a text amplification panel that can be dragged above the text, it helps the user to pinpoint the cursor or text selection handle.
Enlarge text in Android P
We want the user to have the same experience in all apps, both in the custom window widget and when the text is presented, we set up an amplifier window widget, so any view attached to the window can take advantage of that part. The amplifier window widget can provide an enlarged version of any view or interface, not just text.
There are three main methods for amplifiers: show, update, and dismiss. For example, you can call these methods when you implement custom view onTouchEvent processing. In this way, the amplifier can follow the user’s finger movement on the screen.
The Linkify class has existed since API 1, allowing regular expressions to add links to text. In addition, it calls an instance of WebView to find the physical address, which may degrade the performance of the app requesting the link. In order to provide more accurate links, especially for international text, and to eliminate performance degradation caused by WebView, we created Smart Linkify. It can be accessed via the TextClassifier API.
Smart Linkify uses machine learning algorithms and models to identify entities in text. This approach improves the reliability of entity recognition. Smart Linkify can make recommendations for actions that users can take based on entity categories. For example, if Smart Linkify identifies a phone number, it gives advice, such as sending text messages, calling, or adding to the address book.
Smart Linkify in Android P
In order to improve the performance of the app, you can put the generation and application of links into the background thread.
Line Height and Baseline Text Alignment
Sometimes the layout parameters provided by the designer to the developer do not exactly match the existing TextView properties. In Android P and Jetpack, we added three attributes and their corresponding methods to bridge the differences caused by the differences in the way designers and developers work.
Set Row Height
Before Android P, row spacing can be controlled by lineSpacingExtra and lineSpacingMultiplier attributes. However, designers often use a simple row height to set these values. To this end, on Android P, we added the lineHeight property to set the text row height, i.e., the spacing between the top and bottom of the row (or the spacing between two consecutive baselines). In fact, the property utilizes and changes existing lineSpacingExtra and lineSpacingMultiplier properties.
Line height and font size
Set Baseline Text Alignment
To control the spacing between the first and last line baselines and the view boundary, we have added two new properties: firstBaselineToTopHeight versus lastBaselineToBottomHeight.
firstBaselineToTopHeight: Set the spacing between the upper boundary of TextView and the first row baseline of TextView. The property actually changes the top margin.
lastBaselineToBottomHeight: Set the spacing between the lower boundary of TextView and the last baseline of TextView. This property actually changes the bottom margin.
The distance from the first row baseline to the top and the last row baseline to the bottom
Text plays an important role in most apps. It is a key component of app design language. The text is used by users and even used to render emoticons. We will continue to invest in text to enhance the experience of app users and developers.
If you want to learn more about the use of text APIs and the new text features of Android P, please see Google I/O 2018 discussion video on “Best practices with text”: https://www.youtube.com/watch? vx-FcOX6ErdIampfeature; youtu.be
Click on the end of the screen | Read the original | Go to the Android Developers Official Document for more information
· Android P Beta 3 is now released! · Application migration to Android P Operational Guide · Android simulator now supports AMD processor and Hyper-V