我有一个很长的文本和固定大小的textView.如何逐页显示文本?用户将以这种方式与程序交互:他向左或向右滑动以转到下一页或上一页.
目前,我创建了一个PageManager来完成这项工作.但它非常有限.处理文本的核心代码是:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
限制是页面可能截断文本
|A lon|
|g wor|
|d man|
Run Code Online (Sandbox Code Playgroud)
//一个长话的人
或由于自动换行而产生的线条:
//页面管理器计算这个(2行):
|a sentence with loooooooo|
|ooong word abcdefghijklmn|
Run Code Online (Sandbox Code Playgroud)
//但实际上是在文本视图中(3行):
|a sentence with |
|looooooooooong word |
|abcdefghijklmn |
Run Code Online (Sandbox Code Playgroud)
所以最后的行数不仅仅是计算.所以我的页面管理员是愚蠢的.有人会帮助我吗?谢谢!
回答可能为时已晚,但我会发布如何解决这样的问题.
我做了类似的项目,但对于Android平板电脑.在我的应用程序中,我有系统底栏,我的导航栏和标题.Activity的这些部分不参与文本预览,所以我想在计算中排除它们.对于屏幕750px,这些部分需要24%的屏幕,大约180px.所以我使用以下算法在文本视图中查看文本:
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));
Run Code Online (Sandbox Code Playgroud)
格式化和其他转换"在运行中"执行.没什么难的,但对我有用.有例子
在使用算法之前:

使用算法后:

