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的子类,被设计用来保持和显示文本。我们可以在代码中导入android.widget.TextView
来使用它。典型地,TextView不是用来编辑文本的,在编辑场景我们使用EditText.
开发Android应用程序时,TextView是最重要的控件。没有它你无法简单地开发Android应用程序。
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的架构
- 样式文本
- 在View中的文本布局
当前文章是第一篇,这里我讨论TextView的架构和样式文本。
Text Architecture-
Text stack in android is split in two parts, there is java code and there is native or C++ code.
在Android领域内,文本相关的技术栈可以分为两个部分,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层 - 就像图标中显示的内容一样,在Java层的顶端有TextView和EditText这两个控件可以用。对于要实现自定义View的场景,下面的第二层提供了Layout(布局),Paint(画笔)和Canvas(画布)。这一层可以不依赖控件就可以布局和渲染文本。
![](https://img.haomeiwen.com/i2366635/b31cba7afbd74834.png)
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,用来做动态图到位图的变换;在下面是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技术栈的重要部分,由C++实现。它的主要职责是文本布局的测量以及断行(逻辑上,测量与断行是强相关的)。
**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 把字符串作为参数并且确认他们的字形(就像图像与字符的对应关系一样,这里的字形就是字符在图像上的代表)
![](https://img.haomeiwen.com/i2366635/e821fdfd71513b58.png)
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.
字形的匹配没有必要一对一,不同的字体可能有不同的字形。一旦所有的字形都被确定,系统会确定好它们的位置来获得最终的图像。当我们提供了很长的字符串时,Minikin会先把字符串分隔成很多单词,对于每一个单词做测量的工作,把测量结果添加到一个叫单词布局缓存的LRU缓存中(它的容量是确定的5k),以便可以让相同的单词使用缓存而不是重复测量。
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.
断行:当字符串的长度比要填入的数组大时,Minkin会做一个断行处理。简单地情况是位置会被一个一个填满,直到到达边缘。然后移动到下一行。这个行为可以通过断行策略来控制。
![](https://img.haomeiwen.com/i2366635/457f6d74f140837d.png)
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把单词分布在不同的行内。默认值是高质量的,这一策略与平衡策略类似,除了最后一行的细微断字。
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.
断字,断字会引入文本对齐和空格的运用。这会提升测量的成本,因为会有更多需要测量的单词,并且,为了最佳的效果还需要对比更多的配置方法。
![](https://img.haomeiwen.com/i2366635/638b083df1a16365.png)
Performance in Android P
![](https://img.haomeiwen.com/i2366635/c86ab197e421275e.png)
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.
断字也会因为现场而受到影响。我们必须明确地设置不同语言的现场信息。如果字符串中有多重语言。那么我们不得不设置多个现场的信息(span愿意为跨度,但这里说不通吧),这样会降低文本布局的性能。
Styling Text- 样式文本
When we want to set multiple style in a text we have to use span.
我们需要使用span来设置一个文本的多重样式。
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是用修饰文本的标记对象。Spans可以修饰字符,也可以修饰段落,这取决于它们被应用到几个字符还是整个段落。
-
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)需要调用TextView的重回方法;而度量变化(例如字符修改的TypeFace span)需要调用重新测量和重新绘制两个方法,因为这种情况下文本的尺寸也会被改变。
![](https://img.haomeiwen.com/i2366635/e0f8871226a3ee32.png)
- 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常见场景使用框架就可以了。你还可以创建自定义的Span。但是你还是应该使用框架层的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.复制可序列化的文本,然后通过剪切板服务来反序列化。
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这两个关键的接口来使用span.
Spanned适用于不可变的修饰和不可变的文本,有声明好的方法如getSpan(), getSpanStart 等,只允许查询span,不允许修改。
Spannable适用于不可变的文本和可变的修饰,允许设置和移除span。
![](https://img.haomeiwen.com/i2366635/60809405fd1f4cd5.png)
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.
上图中有三个可用的具体的类,这些类实现了上面两个接口。SpannableString持有span的数组,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数到达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.
样式文本的国际化, 当应用使用多种语言时,会有多个字符串配置文件。在不同的语言中,相同的文本可能会显示在不同的位置,所以spannable的检索方式也是不同的。我们可以在字符串xml配置文件中使用功能受限制的超文本标记语言(HTML)。
We should use annotation tags for achieving this.Annotation tags allows to set key and value pairs.
我们应该使用注解标签来实现在注解中设置键值对。
![](https://img.haomeiwen.com/i2366635/790fffc6a071c1d0.png)
To be continued……….
Stay tuned for further articles on this series.
网友评论