【问题标题】:String processing very slow in new Android version(4.3)新的 Android 版本(4.3)中的字符串处理非常慢
【发布时间】:2014-01-02 04:33:44
【问题描述】:

我试图将一个字符串分成多个页面。该字符串包含一个故事(因此有点长),我需要将其加载到字符串的 ArrayList 中,该字符串将显示为不同的页面。

下面是我的代码中的方法,它接收长字符串并将其分成页面并加载字符串的 ArrayList。我的问题是这在 Android 3.2 中运行得非常快。但是在我的另一部 4.3 手机中,它的加载速度非常慢(比如说,在 3.2 中需要 2 秒才能工作的内容大约需要 10 秒)。下面是我的代码。你们中的任何人都可以在代码中提出任何改进,以使其在新版本中更快地工作。不知道为什么新版本的处理速度比旧版本慢。

TextView contentTextView = (TextView)findViewById(R.id.textViewStory1);
String contentString;
TextPaint tp = contentTextView.getPaint();
String[] pages;
int totalPages=0;
ArrayList<String> pagesArray = new ArrayList<String>();
public void loadStory(String storyName){

    //initialize variables
    numCharsLine=0;
    contentString = getStringFromTxtFile(storyName);

    int linesInOnePage = getLinesInPage();//get how many lines will be displayed in one page
    //load story into arraylist pagesArray
    while (contentString != null && contentString.length() != 0) 
    {
        totalPages ++;
        int numChars = 0;
        int lineCount = 0;

        while ((lineCount < linesInOnePage) && (numChars < contentString.length())) {

            numCharsLine = tp.breakText(contentString.substring(numChars), true, pageWidth, null);

            numChars = numChars + numCharsLine;
            lineCount ++;
        }
        // retrieve the String to be displayed in pagesArray
        String toBeDisplayed = contentString.substring(0, numChars);
        contentString = contentString.substring(numChars);
        pagesArray.add(toBeDisplayed);
        numChars = 0;
        lineCount = 0;
    }
    //get the pagecount and reset pageNumber to current page
    totalPages=pagesArray.size();
    pages = new String[pagesArray.size()];
    pages = pagesArray.toArray(pages);  
}

下面还有将文本文件的内容加载到字符串中的方法

    public String getStringFromTxtFile(String fileName) {

    try {
         InputStream is = getAssets().open(fileName+".txt");
         int size = is.available();
         byte[] buffer = new byte[size];
         is.read(buffer);
         is.close();

         return new String(buffer);

         }
         catch (IOException e) {
            throw new RuntimeException(e);
         } 
}

【问题讨论】:

  • 请原谅我说实话。这段代码不可读、难以理解、难以维护并且效率极低。想到在那个循环中创建了多少个字符串,我不寒而栗。可能有更好的解决方案来实现您想要做的事情。为了提供帮助,请解释这个故事的出处、它的格式和布局以及您如何定义“页面”是什么。
  • 我已经编辑了我的代码以使其看起来更简单。所涉及的复杂性是因为我必须插入换行符以确保它完全适合屏幕。默认的 android textpaint 不是文字变形以适应屏幕。编辑后的代码,所有的字符串操作都完好无损,但是一些int相关的操作已经出局了。
  • 感谢 Simon 上面的赞美,我找到了一种非常有效的方法来显示跨页数的长字符串。 -我将长文本显示到文本视图中并禁用滚动-然后找出布局中的行和单个屏幕上的行,因此我知道长字符串将跨越多少个屏幕(页面)-然后每当用户单击按钮转到下一页或滑动屏幕转到下一页然后我使用 TexView.scrollTo() 函数向前移动到行数(屏幕上的行数)。所以它变得和移动到下一页一样好。

标签: android


【解决方案1】:

现在您正在将整个文件读取为字符串,然后将此字符串拆分为行并进行处理。

我建议您联合从文件中读取文本并逐行处理它。

例如,使用 LineNumberReader 或 BufferedReader 类:

BufferedReader bis = new BufferedReader(new FileReader("my/file/path"));
String line;
while ((line = bis.readLine()) != null) {
    processLine(line); //process current line
}

因此您不必执行这么多的 concat 和 substring 操作。

【讨论】:

  • 感谢您的输入,但这无济于事,因为无论如何我都必须使用更多字符串操作进一步处理每一行,以根据屏幕宽度插入换行符,并在编号时进一步插入分页符行数超过页面高度。此外,我还更新了上面的代码以添加 txt 文件读取方法。
猜你喜欢
  • 2019-09-12
  • 1970-01-01
  • 2020-08-23
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多