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 TextView
or StaticLayout
. Since the text
layout information will be included in this instance, 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 NoCopySpan
attached to the original text won't be passed to
PrecomputedText.
Nested classes |
class |
PrecomputedTextCompat.Params
The information required for building PrecomputedTextCompat .
|
Inherited constants |
From interface
android.text.Spanned
int |
SPAN_COMPOSING
|
int |
SPAN_EXCLUSIVE_EXCLUSIVE
|
int |
SPAN_EXCLUSIVE_INCLUSIVE
|
int |
SPAN_INCLUSIVE_EXCLUSIVE
|
int |
SPAN_INCLUSIVE_INCLUSIVE
|
int |
SPAN_INTERMEDIATE
|
int |
SPAN_MARK_MARK
|
int |
SPAN_MARK_POINT
|
int |
SPAN_PARAGRAPH
|
int |
SPAN_POINT_MARK
|
int |
SPAN_POINT_MARK_MASK
|
int |
SPAN_POINT_POINT
|
int |
SPAN_PRIORITY
|
int |
SPAN_PRIORITY_SHIFT
|
int |
SPAN_USER
|
int |
SPAN_USER_SHIFT
|
|
Public methods |
char
|
charAt(int index)
|
static
PrecomputedTextCompat
|
create(CharSequence text, PrecomputedTextCompat.Params params)
Create a new PrecomputedText which will pre-compute text measurement and glyph
positioning information.
|
int
|
getParagraphCount()
Returns the count of paragraphs.
|
int
|
getParagraphEnd(int paraIndex)
Returns the paragraph end offset of the text.
|
int
|
getParagraphStart(int paraIndex)
Returns the paragraph start offset of the text.
|
PrecomputedTextCompat.Params
|
getParams()
Returns the parameters used to measure this text.
|
int
|
getSpanEnd(Object tag)
|
int
|
getSpanFlags(Object tag)
|
int
|
getSpanStart(Object tag)
|
<T>
T[]
|
getSpans(int start, int end, Class<T> type)
|
static
Future<PrecomputedTextCompat>
|
getTextFuture(CharSequence charSequence, PrecomputedTextCompat.Params params, Executor executor)
Helper for PrecomputedText that returns a future to be used with
AppCompatTextView.setTextFuture(Future) .
|
int
|
length()
|
int
|
nextSpanTransition(int start, int limit, Class type)
|
void
|
removeSpan(Object what)
|
void
|
setSpan(Object what, int start, int end, int flags)
|
CharSequence
|
subSequence(int start, int end)
|
String
|
toString()
|
Inherited methods |
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
From interface
android.text.Spannable
abstract
void
|
removeSpan(Object arg0)
|
abstract
void
|
setSpan(Object arg0, int arg1, int arg2, int arg3)
|
|
From interface
android.text.Spanned
abstract
int
|
getSpanEnd(Object arg0)
|
abstract
int
|
getSpanFlags(Object arg0)
|
abstract
int
|
getSpanStart(Object arg0)
|
abstract
<T>
T[]
|
getSpans(int arg0, int arg1, Class<T> arg2)
|
abstract
int
|
nextSpanTransition(int arg0, int arg1, Class arg2)
|
|
From interface
java.lang.CharSequence
abstract
char
|
charAt(int arg0)
|
default
IntStream
|
chars()
|
default
IntStream
|
codePoints()
|
static
int
|
compare(CharSequence arg0, CharSequence arg1)
|
abstract
int
|
length()
|
abstract
CharSequence
|
subSequence(int arg0, int arg1)
|
abstract
String
|
toString()
|
|
Public methods
charAt
public char charAt (int index)
create
public static PrecomputedTextCompat create (CharSequence text,
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 any
NoCopySpan
attached to the text won't be passed to the
created PrecomputedText.
Parameters |
text |
CharSequence : the text to be measured |
params |
PrecomputedTextCompat.Params : parameters that define how text will be precomputed |
getParagraphCount
public int getParagraphCount ()
Returns the count of paragraphs.
getParagraphEnd
public int getParagraphEnd (int paraIndex)
Returns the paragraph end offset of the text.
getParagraphStart
public int getParagraphStart (int paraIndex)
Returns the paragraph start offset of the text.
getSpanEnd
public int getSpanEnd (Object tag)
getSpanFlags
public int getSpanFlags (Object tag)
getSpanStart
public int getSpanStart (Object tag)
getSpans
public T[] getSpans (int start,
int end,
Class<T> type)
Parameters |
start |
int |
end |
int |
type |
Class |
getTextFuture
public static Future<PrecomputedTextCompat> getTextFuture (CharSequence charSequence,
PrecomputedTextCompat.Params params,
Executor executor)
Helper for PrecomputedText that returns a future to be used with
AppCompatTextView.setTextFuture(Future)
.
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);
}
In such cases, using PrecomputedText is difficult, since it isn't safe to defer the setText()
code arbitrarily - a layout pass may happen before computation finishes, and will be
incorrect if the text isn't ready yet.
With getTextFuture()
, you can block on the result of the precomputation safely
before the result is needed. AppCompatTextView provides
AppCompatTextView.setTextFuture(Future)
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
Future future = PrecomputedTextCompat.getTextFuture(
data.text, vh.textView.getTextMetricsParamsCompat(), myExecutor);
// and pass future to TextView, which awaits result before measuring
vh.textView.setTextFuture(future);
}
Because RecyclerView
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 |
charSequence |
CharSequence : the text to be displayed |
params |
PrecomputedTextCompat.Params : the parameters to be used for displaying text |
executor |
Executor : the executor to be process the text layout. If null is passed, the default
single threaded pool will be used. |
length
public int length ()
nextSpanTransition
public int nextSpanTransition (int start,
int limit,
Class type)
Parameters |
start |
int |
limit |
int |
type |
Class |
removeSpan
public void removeSpan (Object what)
setSpan
public void setSpan (Object what,
int start,
int end,
int flags)
Parameters |
what |
Object |
start |
int |
end |
int |
flags |
int |
subSequence
public CharSequence subSequence (int start,
int end)
Parameters |
start |
int |
end |
int |
toString
public String toString ()
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2021-02-24 UTC.