Added in API level 28

PrecomputedText


open class PrecomputedText : Spannable
kotlin.Any
   ↳ android.text.PrecomputedText

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 android.widget.TextView 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&lt;&gt;(textView);
       bgExecutor.submit(() -&gt; {
           TextView textView = textViewRef.get();
           if (textView == null) return;
           final PrecomputedText precomputedText = PrecomputedText.create(longString, params);
           textView.post(() -&gt; {
               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

Nested classes

The information required for building PrecomputedText.

Public methods
open static PrecomputedText!

Create a new PrecomputedText which will pre-compute text measurement and glyph positioning information.

open Char
get(index: Int)

Returns the char value at the specified index.

open Unit
getBounds(start: Int, end: Int, bounds: Rect)

Retrieves the text bounding box for the given range.

open Unit
getFontMetricsInt(start: Int, end: Int, outMetrics: Paint.FontMetricsInt)

Retrieves the text font metrics for the given range.

open Int

Returns the count of paragraphs.

open Int
getParagraphEnd(paraIndex: Int)

Returns the paragraph end offset of the text.

open Int
getParagraphStart(paraIndex: Int)

Returns the paragraph start offset of the text.

open PrecomputedText.Params

Returns the layout parameters used to measure this text.

open Int
getSpanEnd(tag: Any!)

Return the end of the range of text to which the specified markup object is attached, or -1 if the object is not attached.

open Int

Return the flags that were specified when Spannable.setSpan was used to attach the specified markup object, or 0 if the specified object has not been attached.

open Int

Return the beginning of the range of text to which the specified markup object is attached, or -1 if the object is not attached.

open Array<T>!
getSpans(start: Int, end: Int, type: Class<T>!)

Return an array of the markup objects attached to the specified slice of this CharSequence and whose type is the specified type or a subclass of it.

open Float
getWidth(start: Int, end: Int)

Returns text width for the given range.

open Int
nextSpanTransition(start: Int, limit: Int, type: Class<Any!>!)

Return the first offset greater than start where a markup object of class type begins or ends, or limit if there are no starts or ends greater than start but less than limit.

open Unit
removeSpan(what: Any!)

open Unit
setSpan(what: Any!, start: Int, end: Int, flags: Int)

open CharSequence
subSequence(startIndex: Int, endIndex: Int)

Returns a CharSequence that is a subsequence of this sequence.

open String

Returns a string representation of the object.

Properties
open Int

Returns the length of this character sequence.

Public methods

create

Added in API level 28
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.
Return
PrecomputedText! A PrecomputedText

get

Added in API level 28
open fun get(index: Int): Char

Returns the char value at the specified index. An index ranges from zero to length() - 1. The first char value of the sequence is at index zero, the next at index one, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

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

Added in API level 28
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

Added in API level 33
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

Added in API level 28
open fun getParagraphCount(): Int

Returns the count of paragraphs.

Return
Int Value is 0 or greater

getParagraphEnd

Added in API level 28
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

Added in API level 28
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

Added in API level 28
open fun getParams(): PrecomputedText.Params

Returns the layout parameters used to measure this text.

Return
PrecomputedText.Params This value cannot be null.

getSpanEnd

Added in API level 28
open fun getSpanEnd(tag: Any!): Int

Return the end of the range of text to which the specified markup object is attached, or -1 if the object is not attached.

getSpanFlags

Added in API level 28
open fun getSpanFlags(tag: Any!): Int

Return the flags that were specified when Spannable.setSpan was used to attach the specified markup object, or 0 if the specified object has not been attached.

getSpanStart

Added in API level 28
open fun getSpanStart(tag: Any!): Int

Return the beginning of the range of text to which the specified markup object is attached, or -1 if the object is not attached.

getSpans

Added in API level 28
open fun <T : Any!> getSpans(
    start: Int,
    end: Int,
    type: Class<T>!
): Array<T>!

Return an array of the markup objects attached to the specified slice of this CharSequence and whose type is the specified type or a subclass of it. Specify Object.class for the type if you want all the objects regardless of type.

getWidth

Added in API level 28
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.0f or greater
Exceptions
java.lang.IllegalArgumentException if start and end offset are in the different paragraph.

nextSpanTransition

Added in API level 28
open fun nextSpanTransition(
    start: Int,
    limit: Int,
    type: Class<Any!>!
): Int

Return the first offset greater than start where a markup object of class type begins or ends, or limit if there are no starts or ends greater than start but less than limit. Specify null or Object.class for the type if you want every transition regardless of type.

removeSpan

Added in API level 28
open fun removeSpan(what: Any!): Unit
Exceptions
java.lang.IllegalArgumentException if MetricAffectingSpan is specified.

setSpan

Added in API level 28
open fun setSpan(
    what: Any!,
    start: Int,
    end: Int,
    flags: Int
): Unit
Exceptions
java.lang.IllegalArgumentException if MetricAffectingSpan is specified.

subSequence

Added in API level 28
open fun subSequence(
    startIndex: Int,
    endIndex: Int
): CharSequence

Returns a CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1. The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.

Parameters
start the start index, inclusive
end the end index, exclusive
Return
CharSequence the specified subsequence
Exceptions
java.lang.IndexOutOfBoundsException if start or end are negative, if end is greater than length(), or if start is greater than end

toString

Added in API level 28
open fun toString(): String

Returns a string representation of the object.

Return
String a string consisting of exactly this sequence of characters

Properties

length

Added in API level 28
open val length: Int

Returns the length of this character sequence. The length is the number of 16-bit chars in the sequence.

Return
Int the number of chars in this sequence