我正在开发一个看起来像电子书阅读器(.text、pdf 文件等)的应用程序。我有一个巨大的文本,分为不同的章节或页面。
现在的问题是如何将整个内容分成若干页并一页一页地显示。我怎么知道屏幕适合的字符数(取决于屏幕尺寸和字体大小)。我完全不知道从哪里开始以及如何继续。请举例帮助我。
这是一个简单的示例应用程序,以防其他人需要它https://github.com/koros/AndroidReader https://github.com/koros/AndroidReader
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextPaint;
public class PagerTask extends AsyncTask<MainActivity.ViewAndPaint, MainActivity.ProgressTracker, Void> {
private Context mContext;
public PagerTask(Context context){
this.mContext = context;
}
protected Void doInBackground(MainActivity.ViewAndPaint... vps) {
MainActivity.ViewAndPaint vp = vps[0];
MainActivity.ProgressTracker progress = new MainActivity.ProgressTracker();
TextPaint paint = vp.paint;
int numChars = 0;
int lineCount = 0;
int maxLineCount = vp.maxLineCount;
int totalCharactersProcessedSoFar = 0;
// contentString is the whole string of the book
int totalPages = 0;
while (vp.contentString != null && vp.contentString.length() != 0 )
{
while ((lineCount < maxLineCount) && (numChars < vp.contentString.length())) {
numChars = numChars + paint.breakText(vp.contentString.substring(numChars), true, vp.screenWidth, null);
lineCount ++;
}
// Retrieve the String to be displayed in the current textview
String stringToBeDisplayed = vp.contentString.substring(0, numChars);
int nextIndex = numChars;
char nextChar = nextIndex < vp.contentString.length() ? vp.contentString.charAt(nextIndex) : ' ';
if (!Character.isWhitespace(nextChar)) {
stringToBeDisplayed = stringToBeDisplayed.substring(0, stringToBeDisplayed.lastIndexOf(" "));
}
numChars = stringToBeDisplayed.length();
vp.contentString = vp.contentString.substring(numChars);
// publish progress
progress.totalPages = totalPages;
progress.addPage(totalPages, totalCharactersProcessedSoFar, totalCharactersProcessedSoFar + numChars);
publishProgress(progress);
totalCharactersProcessedSoFar += numChars;
// reset per page items
numChars = 0;
lineCount = 0;
// increment page counter
totalPages ++;
}
return null;
}
@Override
protected void onProgressUpdate(MainActivity.ProgressTracker... values) {
((MainActivity)mContext).onPageProcessedUpdate(values[0]);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)