Class PrecomputedText
- All Implemented Interfaces:
Spannable,Spanned,CharSequence
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
TextView or StaticLayout. Since the text layout information will
be included in this instance, TextView or StaticLayout will not
have to recalculate this information.
Note that the PrecomputedText created from different parameters of the target TextView will be rejected internally and compute the text layout again with the
current TextView parameters.
An example usage is:
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);
});
});
}
Note that the PrecomputedText created from different parameters of the target
TextView will be rejected.
Note that any NoCopySpan attached to the original text won't be passed to
PrecomputedText.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic final classThe information required for buildingPrecomputedText.Nested classes/interfaces inherited from interface icyllis.modernui.text.Spannable
Spannable.Factory -
Field Summary
Fields inherited from interface icyllis.modernui.text.Spannable
DEFAULT_FACTORY, NO_COPY_FACTORYFields inherited from interface icyllis.modernui.text.Spanned
SPAN_COMPOSING, SPAN_EXCLUSIVE_EXCLUSIVE, SPAN_EXCLUSIVE_INCLUSIVE, SPAN_INCLUSIVE_EXCLUSIVE, SPAN_INCLUSIVE_INCLUSIVE, SPAN_INTERMEDIATE, SPAN_MARK_MARK, SPAN_MARK_POINT, SPAN_PARAGRAPH, SPAN_POINT_MARK, SPAN_POINT_MARK_MASK, SPAN_POINT_POINT, SPAN_PRIORITY, SPAN_PRIORITY_SHIFT, SPAN_USER, SPAN_USER_SHIFT -
Method Summary
Modifier and TypeMethodDescriptioncharcharAt(int index) static PrecomputedTextcreate(CharSequence text, PrecomputedText.Params params) Create a newPrecomputedTextwhich will pre-compute text measurement and glyph positioning information.static PrecomputedText.ParagraphInfo[]createMeasuredParagraphs(CharSequence text, PrecomputedText.Params params, int start, int end, boolean computeLayout) voidgetFontMetricsInt(int start, int end, FontMetricsInt outMetrics) Retrieves the text font metrics for the given range.intReturns the count of paragraphs.intgetParagraphEnd(int paraIndex) Returns the paragraph end offset of the text.intgetParagraphStart(int paraIndex) Returns the paragraph start offset of the text.Returns the layout parameters used to measure this text.intgetSpanEnd(Object tag) Return the end of the range of text to which the specified markup object is attached, or-1if the object is not attached.intgetSpanFlags(Object tag) Return the flags that were specified whenSpannable.setSpan(java.lang.Object, int, int, int)was used to attach the specified markup object, or0if the specified object has not been attached.<T> List<T> Query a set of the markup objects attached to the specified slice of thisCharSequenceand whose type is the specified type or a subclass of it.intgetSpanStart(Object tag) Return the beginning of the range of text to which the specified markup object is attached, or-1if the object is not attached.floatgetWidth(int start, int end) Returns text width for the given range.intlength()intnextSpanTransition(int start, int limit, Class<?> type) Return the first offset greater thanstartwhere a markup object of classtypebegins or ends, orlimitif there are no starts or ends greater thanstartbut less thanlimit.voidremoveSpan(Object what) Remove the specified object from the range of text to which it was attached, if any.voidAttach the specified markup object to the rangestart…endof the text, or move the object to that range if it was already attached elsewhere.subSequence(int start, int end) toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
chars, codePoints, isEmptyMethods inherited from interface icyllis.modernui.text.Spannable
removeSpan
-
Method Details
-
create
public static PrecomputedText create(@NonNull CharSequence text, @NonNull PrecomputedText.Params params) Create a newPrecomputedTextwhich 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
NoCopySpanattached to the text won't be passed to the created PrecomputedText.- Parameters:
text- the text to be measuredparams- parameters that define how text will be precomputed- Returns:
- A
PrecomputedText
-
createMeasuredParagraphs
@Internal public static PrecomputedText.ParagraphInfo[] createMeasuredParagraphs(@NonNull CharSequence text, @NonNull PrecomputedText.Params params, @IntRange(from=0L) int start, @IntRange(from=0L) int end, boolean computeLayout) -
getParams
Returns the layout parameters used to measure this text. -
getParagraphCount
Returns the count of paragraphs. -
getParagraphStart
Returns the paragraph start offset of the text. -
getParagraphEnd
Returns the paragraph end offset of the text. -
getWidth
@FloatRange(from=0.0) public float getWidth(@IntRange(from=0L) int start, @IntRange(from=0L) int end) Returns text width for the given range. Bothstartandendoffset need to be in the same paragraph, otherwise IllegalArgumentException will be thrown.- Parameters:
start- the inclusive start offset in the textend- the exclusive end offset in the text- Returns:
- the text width
- Throws:
IllegalArgumentException- if start and end offset are in the different paragraph.
-
getFontMetricsInt
public void getFontMetricsInt(@IntRange(from=0L) int start, @IntRange(from=0L) int end, @NonNull FontMetricsInt outMetrics) Retrieves the text font metrics for the given range. Bothstartandendoffset need to be in the same paragraph, otherwise IllegalArgumentException will be thrown.- Parameters:
start- the inclusive start offset in the textend- the exclusive end offset in the textoutMetrics- the output font metrics- Throws:
IllegalArgumentException- if start and end offset are in the different paragraph.
-
setSpan
Description copied from interface:SpannableAttach the specified markup object to the rangestart…endof the text, or move the object to that range if it was already attached elsewhere. SeeSpannedfor an explanation of what the flags mean. The object can be one that has meaning only within your application, or it can be one that the text system will use to affect text display or behavior. Some noteworthy ones are the subclasses ofCharacterStyleandParagraphStyle, andTextWatcherandSpanWatcher.- Specified by:
setSpanin interfaceSpannable- Parameters:
what- the markup objectstart- the start char index of the spanend- the end char index of the spanflags- the flags of the span- Throws:
IllegalArgumentException- ifMetricAffectingSpanis specified.
-
removeSpan
Description copied from interface:SpannableRemove the specified object from the range of text to which it was attached, if any.
It is OK to remove an object that was never attached in the first place.- Specified by:
removeSpanin interfaceSpannable- Parameters:
what- markup object to remove- Throws:
IllegalArgumentException- ifMetricAffectingSpanis specified.
-
getSpans
@NonNull public <T> List<T> getSpans(int start, int end, @Nullable Class<? extends T> type, @Nullable List<T> dest) Description copied from interface:SpannedQuery a set of the markup objects attached to the specified slice of thisCharSequenceand whose type is the specified type or a subclass of it.
SpecifynullorObject.classfor the type if you want all the objects regardless of type.If
destlist is non-null, it will be filled with the method results and returned as-is. Otherwise, a new (and possibly- unmodifiable) list will be created with method results and returned. The return list can be empty if there is no match. -
getSpanStart
Description copied from interface:SpannedReturn the beginning of the range of text to which the specified markup object is attached, or-1if the object is not attached.- Specified by:
getSpanStartin interfaceSpanned- Parameters:
tag- markup object- Returns:
- the start char index
-
getSpanEnd
Description copied from interface:SpannedReturn the end of the range of text to which the specified markup object is attached, or-1if the object is not attached.- Specified by:
getSpanEndin interfaceSpanned- Parameters:
tag- markup object- Returns:
- the end char index
-
getSpanFlags
Description copied from interface:SpannedReturn the flags that were specified whenSpannable.setSpan(java.lang.Object, int, int, int)was used to attach the specified markup object, or0if the specified object has not been attached.- Specified by:
getSpanFlagsin interfaceSpanned- Parameters:
tag- markup object- Returns:
- the flags
-
nextSpanTransition
Description copied from interface:SpannedReturn the first offset greater thanstartwhere a markup object of classtypebegins or ends, orlimitif there are no starts or ends greater thanstartbut less thanlimit. SpecifynullorObject.classfor the type if you want every transition regardless of type.- Specified by:
nextSpanTransitionin interfaceSpanned- Parameters:
start- start char index of the slicelimit- end char index of the slicetype- the markup type- Returns:
- transition point
-
length
public int length()- Specified by:
lengthin interfaceCharSequence
-
charAt
public char charAt(int index) - Specified by:
charAtin interfaceCharSequence
-
subSequence
- Specified by:
subSequencein interfaceCharSequence
-
toString
- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
-