Android 开发:将 setText 与 SpannableString 一起使用,由于 Spans 较多,速度很慢,有解决方法吗?
我为我的应用程序制作了一个小语法荧光笔。
它执行正则表达式并将 ForegroundColorSpans 应用于 EditText 内容的副本 - 所有这些都在一个线程中。完成所有这些后,将调用 setText() 方法,并将 EditText 的上下文替换为现在样式的副本。
问题是,setText() 方法会导致 UI 冻结。在我对 2,000 行文件进行的测试中,应用样式文本需要 30 秒!
除了建议用户不要对大文件使用语法突出显示并在打开大文件时自动将其关闭之外,我还能做些什么来加快语法突出显示的速度?除了减少正则表达式并使之更加高效之外。
谢谢, 亚历克斯.
I've made a little syntax highlighter for my app.
It performs the regular expressions and applies the ForegroundColorSpans to a copy of the EditText's contect - all in a thread. Once all this is done, the setText() method gets called and replaces the EditText's context with the now-styled copy.
Problem is, the setText() method causes the UI to freeze. In a test I did with a 2,000 line file it took 30 seconds to apply the styled text!
Aside from recommending users that the syntax highlighting shouldn't be used with big files and automatically turning it off if they are opening a big file, what can I do to speed the syntax highlighting up? Apart from reducing and making the regular expressions more efficient.
Thanks,
Alex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以使用 WebView 或者尝试开发一些功能,以便仅对用户可见的文本进行样式设置。
Maybe you can use a WebView or alternatively try to develop some functionality so that only the text visible to the user is styled.