寫了一個非常小的閱讀器。在實現(xiàn)分頁功能時,一直沒有思路。后來想了一個非常特別的方法。經(jīng)過測試可以完美的實現(xiàn)分頁功能。
主要思路:
- 將文本內(nèi)容填充到TextView中,調(diào)用setText一句搞定。
- 計算TextView的高度范圍內(nèi)可顯示的行數(shù)。如果TextView占據(jù)整個屏幕則計算屏幕范圍可顯示的的函數(shù)。利用TextView 的getLineBounds 函數(shù)可以計算每行占據(jù)的高度h。利用 h 和TextView的高度 H 就可以很方便計算可顯示的行數(shù)。
- 最關鍵的一步。計算TextView n 行顯示的字體個數(shù)。這是最關鍵的一個API,能夠?qū)崿F(xiàn)這個功能主要靠它。而且TextView本身也是借助這個API實現(xiàn)自動換行的。
這就是StatiLayout。StatiLayout有一個函數(shù)getLineEnd(n)可以計算從0到n行字體的個數(shù)。TextView 一頁顯示的行數(shù)是固定的,
分頁的難點就是每行的字體個數(shù)不固定。通過getLineEnd 就可以非常簡單的計算每頁的字體個數(shù)。 - 通過每頁的字體個數(shù)從文本內(nèi)容中截取每頁的內(nèi)容。
使用了一個PagerAdapter 將文本內(nèi)容創(chuàng)建為一個TextView,這樣就可以滑動分頁了.
關鍵代碼:
說明:代碼主要是說明分頁思路,其中有不少bug。
public int[] getPage( TextView textView){
int count=textView.getLineCount();
textView.setText(mContent);
int pCount=getPageLineCount(textView);
int pageNum=count/pCount;
int page[]=new int[pageNum];
for(int i=0;i<pageNum;i++){
page[i]=textView.getLayout().getLineEnd((i+1)*pCount-1); }
return page;}
private int getPageLineCount(TextView view)
{ /*
* The first row's height is different from other row.
*/
int h=view.getBottom()-view.getTop()-view.getPaddingTop();
int firstH=getLineHeight(0,view);
int otherH=getLineHeight(1,view);
return (h-firstH)/otherH + 1 ;}
private int getLineHeight(int line,TextView view)
{ Rect rect=new Rect(); view.getLineBounds(line,rect);
return rect.bottom-rect.top;}
下面是 分頁用的PagerAdapter,為了節(jié)省資源,對TextView進行了復用。
public class ContentAdapter extends PagerAdapter {
List mCache;
private int[] mPage;
private String mContent;
public ContentAdapter(int[] page, String content){ mPage=page; mContent=content; }
@Override public int getCount()
{ return mPage.length; }
@Override
public boolean isViewFromObject(View view, Object object)
{ return view==object; }
private String getText(int page)
{ if(page==0){ return mContent.substring(0,mPage[0]); }
return mContent.substring(mPage[page-1],mPage[page]); }
@Override
public Object instantiateItem(ViewGroup container, int position) { TextView textView=null; if(mCache==null){ mCache=new LinkedList(); }
if(mCache.size()>0){ textView=(TextView) mCache.remove(0); }
else { textView=new TextView(container.getContext()); }
textView.setText(getText(position)); container.addView(textView); return textView; }
@Override
public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); mCache.add(object); }}
最后:
如果文本的內(nèi)容比較大,可以采用分段載入的方法,這樣可以加快打開速度。 即先加載一部分文本用來顯示,然后在后臺線程加載剩余的文本。