Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to page a long text?

Tags:

text

android

I have a long text and fixed size textView. How can show the text page by page? Users will interactive with the program in this way: He swipe left or right to turn to next or previous page.

Currently, I create a PageManager to do this job. But it's very limited. The core code to process the text is:

while (true) {
        newLineIndex = TextUtils.indexOf(content, '\n', processIndex);
        if (newLineIndex == -1) {// till the end of content
            charCount = paint.breakText(content, processIndex, content.length(), false, leftLines * lineWidth, width);
        } else {
            charCount = paint.breakText(content, processIndex, ++newLineIndex, false, leftLines * lineWidth, width);
        }

        leftLines = (int) ((leftLines * lineWidth - width[0]) / lineWidth);
        processIndex += charCount;

        if (leftLines < 1 || processIndex >= content.length()) {
            page = new Page();
            page.endIndex = processIndex;
            page.startIndex = pageBaseLine;
            page.content = content.subSequence(page.startIndex, page.endIndex);
            result.add(page);
            pageBaseLine = processIndex;

            leftLines = lineNumber;
        }

        if (processIndex >= content.length()) {
            break;
        }
    }

The limitation is the page may truncate the text like

|A lon|
|g wor|
|d man|

// a long word man

or incorrec lines due to word wrapping:

//Page Manager calculates this(2 lines):

|a sentence with loooooooo|

|ooong word abcdefghijklmn|

//But actually in text view(3 lines):

|a sentence with           |

|looooooooooong word      |

|abcdefghijklmn           |

So the final line count is more than calculation. So my page manager is stupid. Would any one help me? thanks!

like image 934
Henry Sou Avatar asked Dec 03 '11 14:12

Henry Sou


1 Answers

It might be too late for answer, but I'll post how I solved problem like this.

I made similar project, but for android tablet. In my application, I have system bottom bar, my navigation bar and header. These parts of Activity don't take part in text preview, so I want to exclude them in calculations. For screen 750px these parts takes 24% of screen, it's about 180px. So I use the following algorithm to view text in text view:

  1. Calculate actual height to view text, it's height of my textview: ScreenHeight - ScreenHeight*0.24
  2. Set text size, for example 14.0
  3. Calculate number of lines in text file
  4. Calculate number of lines per page: Actual height/ text size
  5. Calculate number of pages and load them in HashMap

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight() — 0.24*display.getHeight();
mLinesInPage = (int) (Math.floor(height / mTextSize));
mPages = (int) (Math.ceil(mLinesInText / mLinesInPage));

Formatting and other transformations executes "on the fly". Nothing difficult, but works for me. There are examples

Before using algorithm:

enter image description here

After using algorithm:

enter image description here

enter image description here

like image 175
sigrlami Avatar answered Oct 20 '22 15:10

sigrlami