複製代碼 代碼如下:
public class MainActivity extends Activity {
private Handler handler;
private String html;
private TextView tv;
private ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 網上找的html資料
html = "<html><head><title>TextView使用HTML</title></head><body><p><strong>強調</strong></p><p><em>斜體</em></p>"
+ "<p><a href=\"http://www.jb51.net">超連結HTML入門</a>學習HTML!</p><p><font color=\"#aabb00\">顏色1"
+ "</p><p><font color=\"#00bbaa\">顏色2</p><h1>標題1</h1><h3>標題2</h3><h6>標題3</h6><p>大於>小於<</p><p>"
+ "下面是網狀圖片</p><img src=\"http://www.jb51.net/1207.jpg\"/></body>"
+ "下面是網狀圖片</p><img src=\"http://www.jb51.net/207.jpg\"/></body></html>";
tv = (TextView) this.findViewById(R.id.id);
bar = (ProgressBar) this.findViewById(R.id.id_bar);
tv.setMovementMethod(ScrollingMovementMethod.getInstance());// 滾動
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == 0x101) {
bar.setVisibility(View.GONE);
tv.setText((CharSequence) msg.obj);
}
super.handleMessage(msg);
}
};
// 因為從網上下載圖片是耗時操作 所以要開啟新線程
Thread t = new Thread(new Runnable() {
Message msg = Message.obtain();
@Override
public void run() {
// TODO Auto-generated method stub
bar.setVisibility(View.VISIBLE);
/**
* 要實現圖片的顯示需要使用Html.fromHtml的一個重構方法:public static Spanned
* fromHtml (String source, Html.ImageGetterimageGetter,
* Html.TagHandler
* tagHandler)其中Html.ImageGetter是一個介面,我們要實現此介面,在它的getDrawable
* (String source)方法中返回圖片的Drawable對象才可以。
*/
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
URL url;
Drawable drawable = null;
try {
url = new URL(source);
drawable = Drawable.createFromStream(
url.openStream(), null);
drawable.setBounds(0, 0,
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return drawable;
}
};
CharSequence test = Html.fromHtml(html, imageGetter, null);
msg.what = 0x101;
msg.obj = test;
handler.sendMessage(msg);
}
});
t.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}