Android TextView class is a subclass of View class and is designed to hold and display text. It can be used in the code by importing the package android.widget.TextView. Typically, a TextView isn’t meant for editing. For editing we use EditText.
TextView is most important widgets in while developing android applications.Simply you can not develop any applications without using this.
Android中的TextView類是View的子類帅刊,被設(shè)計用來保持和顯示文本。我們可以在代碼中導(dǎo)入android.widget.TextView
來使用它流礁。典型地唠椭,TextView不是用來編輯文本的关筒,在編輯場景我們使用EditText.
開發(fā)Android應(yīng)用程序時,TextView是最重要的控件。沒有它你無法簡單地開發(fā)Android應(yīng)用程序戒洼。
In this series We will discuss following things.
- TextView Architecture
- Styling text.
- Text Layout in View
In first article we will discuss about TextView Architecture and Styling Text.
這個系列的文章中,我們將討論下面幾件事允华。
- TextView的架構(gòu)
- 樣式文本
- 在View中的文本布局
當(dāng)前文章是第一篇圈浇,這里我討論TextView的架構(gòu)和樣式文本寥掐。
Text Architecture-
Text stack in android is split in two parts, there is java code and there is native or C++ code.
在Android領(lǐng)域內(nèi),文本相關(guān)的技術(shù)椓资瘢可以分為兩個部分召耘,java代碼和native或C++代碼。
1. Java layer - At the top of java layer(as you can see in below diagram) two widgets TextView and EditText is available.For custom view implementation below this layer second layer exists which consist Layout,Paint and Canvas.This layer helps to layout text and render it without using widgets.
1. Java層 - 就像圖標(biāo)中顯示的內(nèi)容一樣褐隆,在Java層的頂端有TextView和EditText這兩個控件可以用污它。對于要實現(xiàn)自定義View的場景,下面的第二層提供了Layout(布局)庶弃,Paint(畫筆)和Canvas(畫布)衫贬。這一層可以不依賴控件就可以布局和渲染文本。
2.Native layer- At the top of native layer Minikin library exists.Minikin helps to do word measurements, line breaking and hyphenation.Below this ICU library exists which deals with unicode, HarfBuzz does text shaping, FreeType for conversion of Gifs to bitmaps, Skia which is graphics engine.
2.Native 層- native層的最頂端是Minikin庫歇攻。Minikin可以用來測量詞匯固惯,換行和斷字。下面是ICU庫缴守,用來處理編碼問題缝呕;在下面是HarfBuzz庫,用來做文本塑造斧散;再下面是FreeType供常,用來做動態(tài)圖到位圖的變換;在下面是Skia庫鸡捐,是圖像引擎栈暇。
Minikin-It lies at the core of android stack and is implemented in c++.It’s main responsibilities are text layout measurement and line breaking.
Minkin是Android技術(shù)棧的重要部分,由C++實現(xiàn)箍镜。它的主要職責(zé)是文本布局的測量以及斷行(邏輯上源祈,測量與斷行是強相關(guān)的)。
**i).Text Measurement****-Minikin took string as parameter and identify the glyphs(Glyph is a representation of character similar to image representation of character).
文本測量色迂,Minikin 把字符串作為參數(shù)并且確認(rèn)他們的字形(就像圖像與字符的對應(yīng)關(guān)系一樣香缺,這里的字形就是字符在圖像上的代表)
Glyph matching is not necessarily one to one ,it can also be found in different fonts.Once all the glyphs are identified system can position them to have final look.When we provide longer strings minikin first break it into words and for each word it does the measurements the result of measurements is added into a LRU cache called Word Layout cache(It has fixed size of 5k items)so that if system come across same word again so cached value can be used instead of re measurements.
字形的匹配沒有必要一對一,不同的字體可能有不同的字形歇僧。一旦所有的字形都被確定图张,系統(tǒng)會確定好它們的位置來獲得最終的圖像。當(dāng)我們提供了很長的字符串時诈悍,Minikin會先把字符串分隔成很多單詞祸轮,對于每一個單詞做測量的工作,把測量結(jié)果添加到一個叫單詞布局緩存的LRU緩存中(它的容量是確定的5k)侥钳,以便可以讓相同的單詞使用緩存而不是重復(fù)測量适袜。
ii).Line Breaking- When the string is wider than the array in which text to fit in then minikin has to do a line break.
In simple case boxes are put side by side until boundary is reached an then moving to next line.this behaviour can be controlled using break strategy.
斷行:當(dāng)字符串的長度比要填入的數(shù)組大時,Minkin會做一個斷行處理舷夺。簡單地情況是位置會被一個一個填滿苦酱,直到到達邊緣售貌。然后移動到下一行。這個行為可以通過斷行策略來控制疫萤。
In balanced strategy minikin will distribute the words among the lines to have better text alignment. Default value is high quality which is vey similar to balanced except some subtle differences like hyphenation for last line.
在平衡策略中趁矾,為了更好的文本對齊,Minikin把單詞分布在不同的行內(nèi)给僵。默認(rèn)值是高質(zhì)量的毫捣,這一策略與平衡策略類似,除了最后一行的細微斷字帝际。
iii)Hyphenation-Hyphenation improves the alignment of text and use of white space.It arises the measurements costs because now there will be more words to be measured an it has to compare more configuration for optimum solution.
斷字蔓同,斷字會引入文本對齊和空格的運用。這會提升測量的成本蹲诀,因為會有更多需要測量的單詞斑粱,并且,為了最佳的效果還需要對比更多的配置方法脯爪。
Performance in Android P
Hyphenations is also affected by locale.We have to explicitly set locale for other language string.If string have multiple language then we have to set the multiple locale span which downgrade text layout performance.
斷字也會因為現(xiàn)場而受到影響则北。我們必須明確地設(shè)置不同語言的現(xiàn)場信息。如果字符串中有多重語言痕慢。那么我們不得不設(shè)置多個現(xiàn)場的信息(span愿意為跨度尚揣,但這里說不通吧),這樣會降低文本布局的性能掖举。
Styling Text- 樣式文本
When we want to set multiple style in a text we have to use span.
我們需要使用span來設(shè)置一個文本的多重樣式快骗。
Spans are markup objects that can be attached to text.Spans can be characterised into character and paragraph spans depending on whether they apply to only a few characters or to entire paragraph.
Spans是用修飾文本的標(biāo)記對象。Spans可以修飾字符塔次,也可以修飾段落方篮,這取決于它們被應(yīng)用到幾個字符還是整個段落。
-
Character spans can be split into appearance affecting and metric affecting.
Appearance affecting(i.e. background color span) requires redraw method of textview to be called while metric affecting(i.e. TypeFace span for font change) requires both remeasure and redraw method to be called because in this case size of text is also changing.
字符Span可以分為外觀變化和度量變化励负。
外觀變化(例如背景顏色span)需要調(diào)用TextView的重回方法藕溅;而度量變化(例如字符修改的TypeFace span)需要調(diào)用重新測量和重新繪制兩個方法,因為這種情況下文本的尺寸也會被改變继榆。
- Paragraph spans are used to style blocks of text like changing the layout margin or drawing bullets. Bullets span is used to do these things.
段落的span被用在文本樣式塊中巾表,比如改變布局邊緣空白或繪制項目符號。BulletsSpan就用來做這些事情裕照。
Note:- Comman use case spans are already available in framework and you can still create new Custom span. But you should use Framework span because only these spans can be parceled.
spans常見場景使用框架就可以了攒发。你還可以創(chuàng)建自定義的Span。但是你還是應(yīng)該使用框架層的Span晋南,因為只有這些span可以被序列化。
When we require parcelling of span-
需要實例化span的場景
- When we pass text via intents. 通過intent(意圖)傳值時
- When we copy text text is parcelled and then unparcelled via clipboard service.復(fù)制可序列化的文本羔砾,然后通過剪切板服務(wù)來反序列化负间。
How to use span- To use spans two key interface are available spanned and spannable.
Spanned is for immutable markup and immutable text,have declaration of methods like getSpan(),getSpanStart etc.It will allow only query span not modify them.
Spannable is for immutable text and mutable markup. It allow to set and remove span.
如何使用span:可以通過Spanned和Spannable這兩個關(guān)鍵的接口來使用span.
Spanned適用于不可變的修飾和不可變的文本偶妖,有聲明好的方法如getSpan(), getSpanStart 等,只允許查詢span政溃,不允許修改趾访。
Spannable適用于不可變的文本和可變的修飾,允許設(shè)置和移除span董虱。
Three concrete class implementation of these two interfaces is available as shown in above diagram.
SpannableString hold array of spans whereas SpannableStringBuilder hold tree of spans.
上圖中有三個可用的具體的類扼鞋,這些類實現(xiàn)了上面兩個接口。SpannableString持有span的數(shù)組愤诱,SpannableStringBuilder持有span的樹云头。
Note:- Upto 250 spans performance of SpannableString and Spannable StringBuilder is same but after that SpannbleStringBuilder is faster in rendering.
Note:For checking if one span is present in spannable use Spanned.nextSpanTransition.
注意:span數(shù)到達250的量級,SpannableString與SpannableStringBuilder的性能是相近的淫半,只是SpannableStringBuilder渲染更快一些溃槐。
注意:檢查一個spannable中是否存在一個span,可以使用Spanned.nextSpanTransition方法科吭。
Styling internationalised text-When we use multiple language ,we have multiple string files.In different language same text can be appear in different places so indexing is different for spannable. HTML can we used in string xml but they have limited functionality.
樣式文本的國際化昏滴, 當(dāng)應(yīng)用使用多種語言時,會有多個字符串配置文件对人。在不同的語言中谣殊,相同的文本可能會顯示在不同的位置,所以spannable的檢索方式也是不同的牺弄。我們可以在字符串xml配置文件中使用功能受限制的超文本標(biāo)記語言(HTML)蟹倾。
We should use annotation tags for achieving this.Annotation tags allows to set key and value pairs.
我們應(yīng)該使用注解標(biāo)簽來實現(xiàn)在注解中設(shè)置鍵值對。
To be continued……….
Stay tuned for further articles on this series.