Copy Code code as follows:
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 data found on the web
html = "+ "<p><a href=\" http://www.jb51.net > Hyperlink html Getting Started </a> learning Html!</p><p><font color=\ "# Aabb00\ "> Color 1"
+ "</p><p><font color=\" #00bbaa \ "> Color 2</p>+ "Below is the network picture </p></body>"
+ "Below is the network picture </p></body>
TV = (TextView) This.findviewbyid (r.id.id);
Bar = (ProgressBar) This.findviewbyid (R.id.id_bar);
Tv.setmovementmethod (Scrollingmovementmethod.getinstance ());//scrolling
Handler = new Handler () {
@Override
public void Handlemessage (msg) {
TODO auto-generated Method Stub
if (msg.what = = 0x101) {
Bar.setvisibility (View.gone);
Tv.settext ((charsequence) msg.obj);
}
Super.handlemessage (msg);
}
};
Because downloading pictures from the Internet is a time-consuming operation, opening a new thread
Thread t = new Thread (new Runnable () {
Message msg = Message.obtain ();
@Override
public void Run () {
TODO auto-generated Method Stub
Bar.setvisibility (view.visible);
/**
* To achieve the display of the picture you need to use a refactoring method of html.fromhtml: public static spanned
* Fromhtml (String source, Html.imagegetterimagegetter,
* Html.taghandler
* Taghandler) where Html.imagegetter is an interface, we want to implement this interface, in its getdrawable
* (String Source) method to return the Drawable object of a picture.
*/
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;
}
}