android教程之textview解析带图片的html示例
代码如下:
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=\"//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 class="lazy" data-src=\"//www.jb51.net/1207.jpg\"/></body>"
+ "下面是网络图片</p><img class="lazy" data-src=\"//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);
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;
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341