gpt4 book ai didi

android - 我如何在 android 的多个页面中显示一个长 TextView (字符串)

转载 作者:太空狗 更新时间:2023-10-29 13:30:37 25 4
gpt4 key购买 nike

我正在开发一个看起来像电子书阅读器(.text、pdf 文件等)的应用程序。我有一个巨大的文本,分为不同的章节或页面。

现在的问题是如何将整个内容分成若干页并逐页显示。我怎么知道适合屏幕的字符数(取决于屏幕大小和字体大小)。我对从哪里开始以及如何进行完全感到困惑。请举例说明帮助我。

最佳答案

这是一个简单的示例应用程序,以防其他人需要它 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]);
}

}

enter image description here

关于android - 我如何在 android 的多个页面中显示一个长 TextView (字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com