4

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

现在的问题是如何将整个内容分成几页并一页一页地显示。我怎么知道要适应屏幕的字符数(取决于屏幕大小和字体大小)。我完全不知道从哪里开始以及如何进行。请举个例子来帮助我。

4

2 回答 2

2

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

}

在此处输入图像描述

于 2015-12-30T14:10:43.490 回答
1

以下是PageTurner的做法。它需要一个滚动视图来显示长文本字符串。但是,滚动手势被覆盖。因此,当您执行“向左滑动手势”时,它将执行 Page Flip 动画并向下滚动视图(按屏幕高度)。因此,当您基本上手动滚动到视图底部时,用户感觉好像页面已经翻转。天才不是吗?而且您不必担心字体大小、段落间距、被剪切的单词等。该应用程序可在google play上找到。

于 2013-04-08T10:57:27.883 回答