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、填入空格。