TextView 中插入网络图片

private static final String htmlFor03 = “网络图片测试:http://img1.imgtn.bdimg.com/it/u=4190601239,967361436&fm=11&gp=0.jpg‘>哎呦不错奥”;
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textview);
Thread t = new Thread(new Runnable() {
Message msg = Message.obtain();
@Override
public void run() {
Html.ImageGetter imageGetter = new Html.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(htmlFor03, imageGetter, null);
msg.what = 0x101;
msg.obj = test;
handler.sendMessage(msg);

        }
    });
    t.start();    }

Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x101) {
textView1.setText((CharSequence) msg.obj);
}
super.handleMessage(msg);
}

};

你可能感兴趣的:(Android,技术)