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 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)

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!)

open Int

open Int

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

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

Returns text width for the given range.

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

open Unit
removeSpan(what: Any!)

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

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

open String

Properties
open Int

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
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

getSpanFlags

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

getSpanStart

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

getSpans

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

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 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

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
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
Return
String a string consisting of exactly this sequence of characters

Properties

length

Added in API level 28
open val length: Int
Return
Int the number of chars in this sequence