PrecomputedTextCompat
public class PrecomputedTextCompat implements 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 PrecomputedTextCompat 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. On API 29 or later, there is full PrecomputedText support by framework. From API 21 to API 27, PrecomputedTextCompat relies on internal text layout cache. PrecomputedTextCompat immediately computes the text layout in the constuctor to warm up the internal text layout cache. On API 20 or before, PrecomputedTextCompat does nothing. Note that any android.text.NoCopySpan attached to the original text won't be passed to PrecomputedText.
Summary
Nested types |
|---|
public final class PrecomputedTextCompat.ParamsThe information required for building |
public class PrecomputedTextCompat.Params.BuilderA builder for creating |
Public methods |
|
|---|---|
char |
charAt(int index) |
static PrecomputedTextCompat |
create(Create a new |
@IntRange(from = 0) int |
Returns the count of paragraphs. |
@IntRange(from = 0) int |
getParagraphEnd(@IntRange(from = 0) int paraIndex)Returns the paragraph end offset of the text. |
@IntRange(from = 0) int |
getParagraphStart(@IntRange(from = 0) int paraIndex)Returns the paragraph start offset of the text. |
@NonNull PrecomputedTextCompat.Params |
Returns the parameters used to measure this text. |
int |
getSpanEnd(Object tag) |
int |
getSpanFlags(Object tag) |
int |
getSpanStart(Object tag) |
T[] |
|
static Future<PrecomputedTextCompat> |
@UiThreadHelper for PrecomputedText that returns a future to be used with |
int |
length() |
int |
nextSpanTransition(int start, int limit, Class type) |
void |
removeSpan(Object what) |
void |
|
CharSequence |
subSequence(int start, int end) |
@NonNull String |
toString() |
Inherited Constants |
||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited methods |
||||||||
|---|---|---|---|---|---|---|---|---|
|
Public methods
create
public static PrecomputedTextCompat create(
@NonNull CharSequence text,
@NonNull PrecomputedTextCompat.Params params
)
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 anyandroid.text.NoCopySpan attached to the text won't be passed to the created PrecomputedText.
| Parameters | |
|---|---|
@NonNull CharSequence text |
the text to be measured |
@NonNull PrecomputedTextCompat.Params params |
parameters that define how text will be precomputed |
| Returns | |
|---|---|
PrecomputedTextCompat |
getParagraphCount
public @IntRange(from = 0) int getParagraphCount()
Returns the count of paragraphs.
getParagraphEnd
public @IntRange(from = 0) int getParagraphEnd(@IntRange(from = 0) int paraIndex)
Returns the paragraph end offset of the text.
getParagraphStart
public @IntRange(from = 0) int getParagraphStart(@IntRange(from = 0) int paraIndex)
Returns the paragraph start offset of the text.
getParams
public @NonNull PrecomputedTextCompat.Params getParams()
Returns the parameters used to measure this text.
getTextFuture
@UiThread
public static Future<PrecomputedTextCompat> getTextFuture(
@NonNull CharSequence charSequence,
@NonNull PrecomputedTextCompat.Params params,
@Nullable Executor executor
)
Helper for PrecomputedText that returns a future to be used with setTextFuture. PrecomputedText is suited to compute on a background thread, but when TextView properties are dynamic, it's common to configure text properties and text at the same time, when binding a View. For example, in a RecyclerView Adapter:
void onBindViewHolder(ViewHolder vh, int position) {
ItemData data = getData(position);
vh.textView.setTextSize(...);
vh.textView.setFontVariationSettings(...);
vh.textView.setText(data.text);
} With getTextFuture(), you can block on the result of the precomputation safely before the result is needed. AppCompatTextView provides setTextFuture for exactly this use case. With the following code, the app's layout work is largely done on a background thread:
void onBindViewHolder(ViewHolder vh, int position) {
ItemData data = getData(position);
vh.textView.setTextSize(...);
vh.textView.setFontVariationSettings(...);
// start precompute
Futurefuture = PrecomputedTextCompat.getTextFuture(
data.text, vh.textView.getTextMetricsParamsCompat(), myExecutor);
// and pass future to TextView, which awaits result before measuring
vh.textView.setTextFuture(future);
}prefetches bind multiple frames in advance while scrolling, the text work generally has plenty of time to complete before measurement occurs.
Note: all TextView layout properties must be set before creating the Params object. If they are changed during the precomputation, this can cause a IllegalArgumentException when the precomputed value is consumed during measure, and doesn't reflect the TextView's current state.
| Parameters | |
|---|---|
@NonNull CharSequence charSequence |
the text to be displayed |
@NonNull PrecomputedTextCompat.Params params |
the parameters to be used for displaying text |
@Nullable Executor executor |
the executor to be process the text layout. If null is passed, the default single threaded pool will be used. |
| Returns | |
|---|---|
Future<PrecomputedTextCompat> |
a future of the precomputed text |
| See also | |
|---|---|
setTextFuture |
removeSpan
public void removeSpan(Object what)
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
setSpan
public void setSpan(Object what, int start, int end, int flags)
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |