PrecomputedText
open class PrecomputedText : Spannable
A text which has the character metrics data. A text object that contains the character metrics data and can be used to improve the performance of text layout operations. When a PrecomputedText is created with a given CharSequence
, it will measure the text metrics during the creation. This PrecomputedText instance can be set on android.widget.TextView
or StaticLayout
. Since the text layout information will be included in this instance, android.widget.TextView
or StaticLayout
will not have to recalculate this information. Note that the PrecomputedText
created from different parameters of the target will be rejected internally and compute the text layout again with the current android.widget.TextView
parameters.
An example usage is:
<code>
static void asyncSetText(TextView textView, final String longString, Executor bgExecutor) {
// construct precompute related parameters using the TextView that we will set the text on.
final PrecomputedText.Params params = textView.getTextMetricsParams();
final Reference textViewRef = new WeakReference<>(textView);
bgExecutor.submit(() -> {
TextView textView = textViewRef.get();
if (textView == null) return;
final PrecomputedText precomputedText = PrecomputedText.create(longString, params);
textView.post(() -> {
TextView textView = textViewRef.get();
if (textView == null) return;
textView.setText(precomputedText);
});
});
}
</code>
Note that the
PrecomputedText
created from different parameters of the target
android.widget.TextView
will be rejected. Note that any
android.text.NoCopySpan
attached to the original text won't be passed to PrecomputedText.
Summary
Public methods |
open static PrecomputedText! |
Create a new PrecomputedText which will pre-compute text measurement and glyph positioning information.
|
open Char |
|
open Unit |
Retrieves the text bounding box for the given range.
|
open Unit |
Retrieves the text font metrics for the given range.
|
open Int |
Returns the count of paragraphs.
|
open Int |
Returns the paragraph end offset of the text.
|
open Int |
Returns the paragraph start offset of the text.
|
open PrecomputedText.Params |
Returns the layout parameters used to measure this text.
|
open Int |
|
open Int |
|
open Int |
|
open Array<T>! |
|
open Float |
Returns text width for the given range.
|
open Int |
|
open Unit |
|
open Unit |
|
open CharSequence |
|
open String |
|
Public methods
create
open static fun create(
text: CharSequence,
params: PrecomputedText.Params
): PrecomputedText!
Create a new PrecomputedText
which will pre-compute text measurement and glyph positioning information.
This can be expensive, so computing this on a background thread before your text will be presented can save work on the UI thread.
Note that any
android.text.NoCopySpan
attached to the text won't be passed to the created PrecomputedText.
Parameters |
text |
CharSequence: the text to be measured This value cannot be null . |
params |
PrecomputedText.Params: parameters that define how text will be precomputed This value cannot be null . |
get
open fun get(index: Int): Char
Parameters |
index |
Int: the index of the char value to be returned |
Return |
Char |
the specified char value |
Exceptions |
java.lang.IndexOutOfBoundsException |
if the index argument is negative or not less than length() |
getBounds
open fun getBounds(
start: Int,
end: Int,
bounds: Rect
): Unit
Retrieves the text bounding box for the given range. Both start
and end
offset need to be in the same paragraph, otherwise IllegalArgumentException will be thrown.
Parameters |
start |
Int: the inclusive start offset in the text Value is 0 or greater |
end |
Int: the exclusive end offset in the text Value is 0 or greater |
bounds |
Rect: the output rectangle This value cannot be null . |
Exceptions |
java.lang.IllegalArgumentException |
if start and end offset are in the different paragraph. |
getFontMetricsInt
open fun getFontMetricsInt(
start: Int,
end: Int,
outMetrics: Paint.FontMetricsInt
): Unit
Retrieves the text font metrics for the given range. Both start
and end
offset need to be in the same paragraph, otherwise IllegalArgumentException will be thrown.
Parameters |
start |
Int: the inclusive start offset in the text Value is 0 or greater |
end |
Int: the exclusive end offset in the text Value is 0 or greater |
outMetrics |
Paint.FontMetricsInt: the output font metrics This value cannot be null . |
Exceptions |
java.lang.IllegalArgumentException |
if start and end offset are in the different paragraph. |
getParagraphCount
open fun getParagraphCount(): Int
Returns the count of paragraphs.
Return |
Int |
Value is 0 or greater |
getParagraphEnd
open fun getParagraphEnd(paraIndex: Int): Int
Returns the paragraph end offset of the text.
Parameters |
paraIndex |
Int: Value is 0 or greater |
Return |
Int |
Value is 0 or greater |
getParagraphStart
open fun getParagraphStart(paraIndex: Int): Int
Returns the paragraph start offset of the text.
Parameters |
paraIndex |
Int: Value is 0 or greater |
Return |
Int |
Value is 0 or greater |
getParams
open fun getParams(): PrecomputedText.Params
Returns the layout parameters used to measure this text.
getSpanEnd
open fun getSpanEnd(tag: Any!): Int
getSpanFlags
open fun getSpanFlags(tag: Any!): Int
getSpanStart
open fun getSpanStart(tag: Any!): Int
getWidth
open fun getWidth(
start: Int,
end: Int
): Float
Returns text width for the given range. Both start
and end
offset need to be in the same paragraph, otherwise IllegalArgumentException will be thrown.
Parameters |
start |
Int: the inclusive start offset in the text Value is 0 or greater |
end |
Int: the exclusive end offset in the text Value is 0 or greater |
Return |
Float |
the text width Value is 0 or greater |
Exceptions |
java.lang.IllegalArgumentException |
if start and end offset are in the different paragraph. |
nextSpanTransition
open fun nextSpanTransition(
start: Int,
limit: Int,
type: Class<Any!>!
): Int
removeSpan
open fun removeSpan(what: Any!): Unit
subSequence
open fun subSequence(
startIndex: Int,
endIndex: Int
): CharSequence
Parameters |
start |
the start index, inclusive |
end |
the end index, exclusive |
Exceptions |
java.lang.IndexOutOfBoundsException |
if start or end are negative, if end is greater than length() , or if start is greater than end |
toString
open fun toString(): String
Return |
String |
a string consisting of exactly this sequence of characters |
Properties
length
open val length: Int
Return |
Int |
the number of char s in this sequence |