Android TextView 使用append() 数百次缓慢 - 解决方案?

发布于 2024-12-19 12:19:34 字数 762 浏览 1 评论 0原文

我有一个 Android 源代码编辑器,并且有一个行号计数器,位于主 EditText 的左侧,其中包含源代码。

我有以下函数用于更新行号文本视图:

String lineDelimiter = "\n";
public void updateLineNumbers(){
    int lines = textBox.getLineCount();
    lineNums.setText(1 + lineDelimiter);
    for(int i = 2; i < lines; i++){
      lineNums.append(i + lineDelimiter);
    }
}

这一切都很好,但问题是当您的文档有 200 奇数行时,您开始注意到添加行时有一点延迟。这是因为 Android TextView 的 setText/append 方法有点慢吗?或者是连接导致了延迟?

我还制作了一个类似的函数,当用户添加行号时附加行号,反之亦然,而不是像上面的函数那样清除 TextView 并再次添加每个行号。但当用户添加/删除行时,此功能仍然滞后于应用程序。

我怎样才能阻止这个?我不知道该怎么做,这让我压力很大,因为它落后于我的应用程序,导致它无法用于大文件! :(

感谢您的关注!


解决方案

我找到了一种获得快速行号的方法,即使用重写 onDraw(Canvas canvas) 的自定义 TextView 并以这种方式绘制它们,这会导致滞后-免费行号:)。

I have a source code editor for Android, and I have a line numbers counter that's to the left of the main EditText with source code inside it.

I have the following function I use for updating the line numbers text view:

String lineDelimiter = "\n";
public void updateLineNumbers(){
    int lines = textBox.getLineCount();
    lineNums.setText(1 + lineDelimiter);
    for(int i = 2; i < lines; i++){
      lineNums.append(i + lineDelimiter);
    }
}

All this is fine, but the problem is when you have a document with say 200 odd lines you start to notice a little delay when adding lines. Is this cause Android TextView's setText/append methods are a little slow? Or is it the concatination that's causing the delay?

I've also made a similar function that appends a line number when the user adds a line number, and vice versa, as opposed to clearing the TextView and adding each line numbers again like the function above does. But this function still lags the app when the user adds/removes line(s).

How can I stop this? I can't think of what to do and it's stressing me out because it's lagging my app and rendering it unusable for large files! :(

Thanks for looking!


SOLUTION

I've found a way to have fast line numbers, which is to use a custom TextView with onDraw(Canvas canvas) overriden and to draw them that way which results in lag-free line numbers :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦 2024-12-26 12:19:34

这是否导致 Android TextView 的 setText/append 方法有点慢?或者是连接导致了延迟?

使用 Traceview 并找出答案。

即兴发挥,我认为在 TextView 上多次调用 append() 会比调用 append() 慢得多在 StringBuilder 上调用多次,然后在 TextView 上调用 setText() 一次。

我怎样才能阻止这个?

不要以这种方式处理行号。例如,将 TextView 放在 EditText 的左侧,并将行号放入 TextView 中,每行一个。

Is this cause Android TextView's setText/append methods are a little slow? Or is it the concatination that's causing the delay?

Use Traceview and find out.

Off the cuff, I would imagine that calling append() a whole bunch of times on a TextView will be vastly slower than calling append() a bunch of times on a StringBuilder, then calling setText() once on the TextView.

How can I stop this?

Don't handle line numbers that way. For example, put a TextView to the left of the EditText, and put your line numbers in the TextView, one per line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文