Android阅读器如何实现文字两端对齐

Android阅读器如何实现文字两端对齐,达到类似ireader一样的效果图片说明

在android中的webview中,可以对文本内容进行对齐,具体方法如下:
public class MainActivity extends Activity {

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    String htmlText = " %s ";  
    String myData = "Hello World! This tutorial is to show demo of displaying text with justify alignment in WebView.";  

    WebView webView = (WebView) findViewById(R.id.webView1);  
    webView.loadData(String.format(htmlText, myData), "text/html", "utf-8");  
}  

}

方案二:使用textView改造:
MainActivity中:

Display display = getWindowManager().getDefaultDisplay();

DisplayMetrics dm = new DisplayMetrics();

display.getMetrics(dm);

width = dm.widthPixels;

//根据屏幕调整文字大小
mArticleTextView.setLineSpacing(0f, 1.5f);
mArticleTextView.setTextSize(8*(float)width/320f);

//设置TextView
mArticleTextView.setText("TextView需要显示的文本内容");

TextJustification.justify(mArticleTextView,mArticleTextView.getWidth());首先设置TextView的显示字体大小和文本内容,这里设置字体大小根据屏幕尺寸调整。然后调用自定义的类Textustification中的justify方法来实现TextView的分散对齐,两个参数分别是TextView控件以及控件的宽度。

自定义的类TextJustification内容如下:

import java.util.ArrayList;

import android.graphics.Paint;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class TextJustification {

public static void justify(TextView textView, float contentWidth) {
    String text=textView.getText().toString();
    String tempText;
    String resultText = "";
    Paint paint=textView.getPaint();

    ArrayList<String> paraList = new ArrayList<String>();       
    paraList = paraBreak(text);       
    for(int i = 0; i<paraList.size(); i++) {           
        ArrayList<String> lineList=lineBreak(paraList.get(i).trim(),paint,contentWidth);           
        tempText = TextUtils.join(" ", lineList).replaceFirst("\\s*", "");           
        resultText += tempText.replaceFirst("\\s*", "") + "\n";       
    }               

    textView.setText(resultText);
}
//分开每个段落
public static ArrayList<String> paraBreak(String text, TextView textview) {
    ArrayList<String> paraList = new ArrayList<String>();
    String[] paraArray = text.split("\\n+");
       for(String para:paraArray) {
           paraList.add(para);
    }
    return paraList;
}

//分开每一行,使每一行填入最多的单词数
private static ArrayList<String> lineBreak(String text, Paint paint, float contentWidth){
    String [] wordArray=text.split("\\s");
    ArrayList<String> lineList = new ArrayList<String>();
    String myText="";

    for(String word:wordArray){
        if(paint.measureText(myText+" "+word)<=contentWidth)
            myText=myText+" "+word;
        else{
            int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" "));
            lineList.add(justifyLine(myText,totalSpacesToInsert));
            myText=word;
        }
    }
    lineList.add(myText);
    return lineList;
}
//已填入最多单词数的一行,插入对应的空格数直到该行满
private static String justifyLine(String text,int totalSpacesToInsert){
    String[] wordArray=text.split("\\s");
    String toAppend=" ";

    while((totalSpacesToInsert)>=(wordArray.length-1)){
        toAppend=toAppend+" ";
        totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1);
    }
    int i=0;
    String justifiedText="";
    for(String word:wordArray){
        if(i<totalSpacesToInsert)
            justifiedText=justifiedText+word+" "+toAppend;

        else               
            justifiedText=justifiedText+word+toAppend;

        i++;
    }

    return justifiedText;
}

}这个类完成了TextView内部文字的排版工作,主要分3个步骤:
1、将一篇文章按段落分成若干段(如果只有一段可以略去该步骤);

2、将每一段的文字拆分成各个单词,然后根据控件长度确定每一行最多可以填入的单词数,并且算出排满该行还需要填入几个空格。

3、填入空格。

关于文本两端对齐的问题,可以参考如下:
To justify text in android I used WebView

setContentView(R.layout.main);

WebView view = new WebView(this);

view.setVerticalScrollBarEnabled(false);

((LinearLayout)findViewById(R.id.inset_web_view)).addView(view);

view.loadData(getString(R.string.hello), "text/html", "utf-8");

and html

<![CDATA[



Lorem ipsum dolor sit amet, consectetur

adipiscing elit. Nunc pellentesque, urna
nec hendrerit pellentesque, risus massa


]]>

I can't yet upload images to prove it but "it works for me".
主要思路:使用WebView来加载,用css来实现两端对齐。
注意:webview要设置成透明的,还有加载时使用
myWebView.loadDataWithBaseURL("", getString(R.string.desc), "text/html", "utf-8",""); 这个方法
// myWebView.loadData(getString(R.string.desc), "text/html", "utf-8"); // 这个方法遇到一些字符中会乱码

可参考以下链接
http://emilyzhou.blog.51cto.com/3632647/990713