2013-04-08 76 views
3

我正在開發一個看起來像電子書閱讀器(.text,pdf文件等)的應用程序。我有一個巨大的文本,分爲不同的章節或頁面。我如何顯示一個長的TextView(字符串)到多個頁面的Android

現在的問題是如何將整個內容分成多頁並逐一顯示這些頁面。我怎麼知道,這些字符的數量將會適合屏幕(取決於屏幕尺寸和字體大小)。我完全困惑於從哪裏開始以及如何繼續。請通過舉例來幫助我。

+0

使用'ViewFlipper'或'ViewPager'它們之間顯示您的網頁,並刷卡 – Houcine 2013-04-08 10:41:43

+0

@Prosanto有你解決了這個問題? 如果你解決了這個問題,請幫助我。 我正在使用相同的功能。 我很困惑從哪裏開始以及如何繼續。 請幫助我 – 2013-07-28 13:29:00

回答

1

以下是PageTurner如何做到這一點。它需要一個滾動視圖來顯示長文本字符串。但是,滾動手勢被覆蓋。所以當你做一個「向左滑動的手勢」時,它會做頁面翻轉動畫並向下滾動視圖(按屏幕高度)。因此,用戶感覺就好像頁面已經轉向,當你剛剛手動滾動到視圖的底部時。 Ingenius不是嗎?而且您不必擔心字體大小,段落間距,文字被剪切等。該應用程序可在google play上獲得。

0

下面是一個簡單的示例應用程序櫃面任何人需要它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

相關問題