getTextFuture
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);
}
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.
Return
a future of the precomputed text
Parameters
the text to be displayed
the parameters to be used for displaying text
the executor to be process the text layout. If null is passed, the default single threaded pool will be used.