Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android长文本分页_Java_Android_Pagination - Fatal编程技术网

Java Android长文本分页

Java Android长文本分页,java,android,pagination,Java,Android,Pagination,我需要显示大的文本文件。显示文本,无需像电子书一样滚动。我可以在页面上打断一段长的文字,但它占用了我太多的时间。例如,下面的代码处理1.4MB的文本大约10-15秒 public void split(TextPaint textPaint, String filepath,Context context) { int pages = 0; File file = new File(filepath); char[] bufferChar

我需要显示大的文本文件。显示文本,无需像电子书一样滚动。我可以在页面上打断一段长的文字,但它占用了我太多的时间。例如,下面的代码处理1.4MB的文本大约10-15秒

    public void split(TextPaint textPaint, String filepath,Context context) {
        int pages = 0;
        File file = new File(filepath);
        char[] bufferChar = new char[1024];
        String uncompletedtext="";
        //How lines we can show
        int maxLinesOnpage = 0;
        StaticLayout staticLayout = new StaticLayout(
                context.getString(R.string.lorem_ipsum),
                textPaint,
                pageWidth,
                Layout.Alignment.ALIGN_NORMAL,
                lineSpacingMultiplier,
                lineSpacingExtra,
                false
        );
        int startLineTop = staticLayout.getLineTop(0);
        int endLine = staticLayout.getLineForVertical(startLineTop + pageHeight);
        int endLineBottom = staticLayout.getLineBottom(endLine);
        if (endLineBottom > startLineTop + pageHeight) {
            maxLinesOnpage = endLine - 1;
        } else {
            maxLinesOnpage = endLine;

        }


        //let's paginate
try {
            BufferedReader buffer = new BufferedReader(new FileReader(file));
            while (buffer.read(bufferChar)>=0) {
                uncompletedtext += new String(bufferChar);
                boolean allcomplete = false;


                    staticLayout = new StaticLayout(
                            uncompletedtext,
                            textPaint,
                            pageWidth,
                            Layout.Alignment.ALIGN_NORMAL,
                            lineSpacingMultiplier,
                            lineSpacingExtra,
                            false
                    );
                    staticLayout.getLineCount();
                    int curTextPages= (int) Math.floor(staticLayout.getLineCount() / maxLinesOnpage);
                    uncompletedtext=uncompletedtext.substring(staticLayout.getLineEnd(curTextPages));
                    pages+=curTextPages;
                    Log.e("PAGES","" + pages);



            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.e("FILE READED FULLY!!", "READ COMPLETE!!!!!!!!!!!!!!!!");




    }
太长了。我无法理解FBReader和СoolReader等应用程序如何立即处理大文件(超过9 MB)。 我看到了应用程序的来源,但它们的功能太多,无法快速找到答案。
我真的需要帮助和提示。谢谢。

谢谢大家!我找到了解决办法!不优雅但非常快的代码(10Mb~600ms)

public void split(TextPaint TextPaint、字符串文件路径、上下文){
文件文件=新文件(文件路径);
char[]bufferChar=新字符[512];
//页面上的行数是多少
int maxLinesOnpage=0;
int SYMBOLS在线=0;
StaticLayout StaticLayout=新的StaticLayout(
context.getString(R.string.lorem\u ipsum),//100行的短文本(\R\n\R\n\R\n\R\n\R\n\R\n\R\n)
textPaint,//MONOSPACE!!!
页面宽度,
Layout.Alignment.ALIGN_NORMAL,
行距倍增管,
行距,
假的
);
int=staticLayout.getLineTop(0);
int endLine=staticLayout.getLineForVertical(startinetop+pageHeight);
int endLineBottom=staticLayout.getLineBottom(endLine);
if(endLineBottom>startInetop+pageHeight){
maxLinesOnpage=endLine-1;
}否则{
maxLinesOnpage=终点线;
}
symbolsOnLine=staticLayout.getLineEnd(0);
试一试{
RandomAccessFile rac=新的RandomAccessFile(文件“r”);
字节[]缓冲区=新字节[2048];
int-wordLen=0;//符号中的单词长度
int wordInBytes=0;//单词长度
int STARTINEPOS=0;//开始第一行位置
int lineWidth=0;//当前行长度
int totalines=0;//当前页面上的行总数
Log.e(“开始分页”,“总计行”);
长超时=System.currentTimeMillis();
int buflen=0;//缓冲区大小
int totalReadedBytes=0;//读取的总字节数
字节skipBytes=0;
而((buflen=rac.read(buffer))!=-1){
对于(inti=0;i=192){skipBytes=2;}
if(unsignedToBytes(buffer[i])>=224{skipBytes=3;}
if(unsignedToBytes(buffer[i])>=240{skipBytes=4;}
if(unsignedToBytes(buffer[i])>=248{skipBytes=5;}
if(unsignedToBytes(buffer[i])>=252{skipBytes=6;}
}
//符号上是否有完整字节
如果(skipBytes>0){
skipBytes--;
如果(skipBytes>0){continue;}
}
如果(buffer[i]==13){//我们有一个\r符号。忽略。
继续;
}
if(buffer[i]==10){//新行符号
if(线宽+字长度>符号在线){
总计++;
如果(totalLines>maxLinesOnpage){
int[]pgsbytes={startinepos,totalReadedBytes};
添加页数(pgsbytes);
startLinePos=总读取字节数;
总数=0;
}
}
wordLen=0;
字字节=0;
总计++;
线宽=0;
如果(totalLines>maxLinesOnpage){
int[]pgsbytes={startinepos,totalReadedBytes-1};
添加页数(pgsbytes);
startLinePos=总读取字节数-1;
总数=0;
}
}
if(buffer[i]==32){//空格符号
如果(线宽+字长度+1 maxLinesOnpage){
int[]pgsbytes={startinepos,totalReadedBytes};
添加页数(pgsbytes);
startLinePos=总读取字节数;
总数=0;
}
线宽=0;
wordLen=0;
字字节=0;
}
}否则{
if(线宽+字长度==符号在线){
总计++;
如果(totalLines>maxLinesOnpage){
int[]pgsbytes={startinepos,totalReadedBytes};
添加页数(pgsbytes);
startLinePos=总读取字节数;
总数=0;
}
线宽=0;
wordLen=0;
字字节=0;
}否则{
总计++;
如果(totalLines>maxLinesOnpage){
int[]pgsbytes={startinepos,totalReadedBytes-1-wordInBytes};
添加页数(pgsbytes);
startLinePos=总读取字节数-1;
总数=0;
}
线宽=字长度+1;
wordLen=0;
字字节=0;
public void split(TextPaint textPaint, String filepath,Context context) {
        File file = new File(filepath);
        char[] bufferChar = new char[512];
        //How lines on page
        int maxLinesOnpage = 0;
        int symbolsOnLine = 0;
        StaticLayout staticLayout = new StaticLayout(
                context.getString(R.string.lorem_ipsum),//short text with 100 lines (\r\n\r\n\r\n\r\n\r\n\r\n)
                textPaint, //MONOSPACE!!!
                pageWidth,
                Layout.Alignment.ALIGN_NORMAL,
                lineSpacingMultiplier,
                lineSpacingExtra,
                false
        );
        int startLineTop = staticLayout.getLineTop(0);
        int endLine = staticLayout.getLineForVertical(startLineTop + pageHeight);
        int endLineBottom = staticLayout.getLineBottom(endLine);
        if (endLineBottom > startLineTop + pageHeight) {
            maxLinesOnpage = endLine - 1;
        } else {
            maxLinesOnpage = endLine;
        }
        symbolsOnLine = staticLayout.getLineEnd(0);

        try {
            RandomAccessFile rac = new RandomAccessFile(file, "r");
            byte[] buffer = new byte[2048];
            int wordLen = 0; //Length of word in symbols
            int wordInBytes = 0; //Lenght of word
            int startLinePos = 0; //Start first line position
            int lineWidth = 0; //Current line length
            int totalLines =0; //Total lines on current page
            Log.e("Start pagination", "" + totalLines);
            long timeout= System.currentTimeMillis();
            int buflen=0; //buffer size
            int totalReadedBytes = 0; //Total bytes readed
            byte skipBytes = 0;
            while ( (buflen=rac.read(buffer))!=-1){
                for (int i=0;i<buflen;i++) {
                    totalReadedBytes++;
                    wordInBytes++;
                    if (skipBytes==0){ //Bytes on one symbol
                        if (unsignedToBytes(buffer[i])>=192){skipBytes=2;}
                        if (unsignedToBytes(buffer[i])>=224){skipBytes=3;}
                        if (unsignedToBytes(buffer[i])>=240){skipBytes=4;}
                        if (unsignedToBytes(buffer[i])>=248){skipBytes=5;}
                        if (unsignedToBytes(buffer[i])>=252){skipBytes=6;}
                    }
                    //Full bytes on symbol or not
                    if (skipBytes>0){
                        skipBytes--;
                        if (skipBytes>0){continue;}
                    }

                    if (buffer[i] == 13) {//We have a \r symbol. Ignore.
                        continue;
                    }



                    if (buffer[i]==10){//New line symbol
                        if (lineWidth + wordLen>symbolsOnLine){
                            totalLines++;
                            if (totalLines > maxLinesOnpage) {
                                int[] pgsbytes = {startLinePos, totalReadedBytes};
                                pages.add(pgsbytes);
                                startLinePos = totalReadedBytes ;
                                totalLines = 0;
                            }
                        }
                        wordLen=0;
                        wordInBytes=0;
                        totalLines++;
                        lineWidth=0;
                        if (totalLines>maxLinesOnpage){
                            int[] pgsbytes = {startLinePos, totalReadedBytes-1};
                            pages.add(pgsbytes);
                            startLinePos = totalReadedBytes-1;
                            totalLines=0;
                        }
                    }

                    if (buffer[i]==32){//Space symbol
                        if (lineWidth + wordLen+1<=symbolsOnLine){//Word fits in line
                            lineWidth+=wordLen + 1;
                            wordLen=0;
                            if (lineWidth==symbolsOnLine){
                                totalLines++;
                                if (totalLines > maxLinesOnpage) {
                                    int[] pgsbytes = {startLinePos, totalReadedBytes};
                                    pages.add(pgsbytes);
                                    startLinePos = totalReadedBytes ;
                                    totalLines = 0;
                                }
                                lineWidth = 0;
                                wordLen = 0;
                                wordInBytes=0;
                            }
                        } else {
                            if (lineWidth + wordLen==symbolsOnLine){
                                totalLines++;
                                if (totalLines > maxLinesOnpage) {
                                    int[] pgsbytes = {startLinePos, totalReadedBytes};
                                    pages.add(pgsbytes);
                                    startLinePos = totalReadedBytes ;
                                    totalLines = 0;
                                }
                                lineWidth = 0;
                                wordLen = 0;
                                wordInBytes=0;
                            } else {
                                totalLines++;
                                if (totalLines > maxLinesOnpage) {
                                    int[] pgsbytes = {startLinePos, totalReadedBytes - 1 - wordInBytes};
                                    pages.add(pgsbytes);
                                    startLinePos = totalReadedBytes - 1;
                                    totalLines = 0;
                                }
                                lineWidth = wordLen + 1;
                                wordLen = 0;
                                wordInBytes=0;
                            }
                        }
                    }

                    if (buffer[i]!=32&&buffer[i]!=10&&buffer[i]!=13){wordLen++; }
                    if (wordLen==symbolsOnLine){
                        totalLines++;
                        if (totalLines>maxLinesOnpage){
                            int[] pgsbytes = {startLinePos, totalReadedBytes-1  - wordInBytes};
                            pages.add(pgsbytes);
                            startLinePos = totalReadedBytes-1;
                            totalLines=0;
                        }
                        lineWidth=0;
                        wordLen=0;
                        wordInBytes=0;
                    }
                }

            }
            rac.close();
            timeout = System.currentTimeMillis() - timeout;
            Log.e("TOTAL Time",  " time " + timeout + "ms");
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.e("FILE READED FULLY!!", "READ COMPLETE!!!!!!!!!!!!!!!!");
    }