Search code examples
androidspannablestring

SpannableStringBuilder performance improvements?


I'm writing an app that constantly updates a TextView with a SpannableStringBuilder that uses a ton of spans. Benchmarking shows that, unsurprisingly, setText() and drawText() are both big blockers, and SpannableStringInternal.getSpans() has the single highest exclusive CPU time.

Is there a way to reuse the same span, so I don't have to clog up my SpannableString with multiple identical spans? Are there other ways I can improve SpannableString performance?


Solution

  • After a lot of experimentation, I found that creating a custom View that overrides onDraw to draw its own text is absolutely the way to go here. There's no way to prevent getSpans from using a ton of reflection.