Reputation: 882
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!
Upvotes: 5
Views: 3441
Reputation: 1832
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:
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:
After using algorithm:
Upvotes: 5