一、导入jar包并添加依赖
(1)需要导入第三方jar包进行图片的加载处理
jar包下载地址:https://pan.baidu.com/s/1-o_hzhZoIHDeBi-ql_RwIg (提取码:bqys)
(2)将下载好的文件复制到libs文件夹下,右键Add As Library添加依赖。
二、在Activity中进行文本的加载处理
(1)创建一个activity,布局文件添加一个TextView控件即可
(2)添加两个相关处理类
public class URLImageParser implements Html.ImageGetter {
TextView mTextView;
public URLImageParser(TextView textView) {
this.mTextView = textView;
}
@Override
public Drawable getDrawable(String source) {
final URLDrawable urlDrawable = new URLDrawable();
ImageLoader.getInstance().loadImage(source,
new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
urlDrawable.bitmap = loadedImage;
urlDrawable.setBounds(0, 0, loadedImage.getWidth(), loadedImage.getHeight());
mTextView.invalidate();
mTextView.setText(mTextView.getText());
}
});
return urlDrawable;
}
}
public class URLDrawable extends BitmapDrawable {
protected Bitmap bitmap;
@Override
public void draw(Canvas canvas) {
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, getPaint());
}
}
}
(3)在onCreate方法中对ImageLoader进行实例化
contentTv = (TextView) findViewById(R.id.contentTv);
ImageLoader imageLoader = ImageLoader.getInstance();//实例化 ImageLoade
imageLoader.init(ImageLoaderConfiguration.createDefault(this));
final URLImageParser imageGetter = new URLImageParser(contentTv);
(4)setText
String str = "<p></p><p>(原标题:汇丰、渣打相继切断与华为业务往来)</p><p></p><p>【文/观察者网 尹哲】据《华尔街日报》当地时间20日报道,两家全球性银行决定不再向<a href="http://money.163.com/keywords/5/4/534e4e3a/1.html" title="华为" target="_blank">华为</a>提供金融服务。</p><p>报道援引熟悉上述决定的人士说法称,这两家银行分别是<a href="http://money.163.com/keywords/6/4/6c474e3094f6884c/1.html" title="汇丰银行" target="_blank">汇丰银行</a>(HSBC)和<a href="http://money.163.com/keywords/6/2/6e23625394f6884c/1.html" title="渣打银行" target="_blank">渣打银行</a>(Standard Chartered),<b>原因是华为的风险太高。</b></p><p><img alt="汇丰、渣打银行相继切断与华为业务往来:风险太高" src="http://cms-bucket.nosdn.127.net/2018/12/21/62a63493a847466b99741997250de802.jpg" width="600" height="400"></p><p>香港中环金融中心,渣打银行、汇丰银行等。图源:视觉中国</p><p>其中有人透露,<strong>汇丰银行</strong>是<strong>去年</strong>作出这个决定的。</p><p>2016年,该行和一家法院指定的机构向<a href="http://money.163.com/keywords/7/8/7f8e56fd/1.html" title="美国" target="_blank">美国</a>检察机关举报了华为的可疑交易。</p>";
contentTv.setText(Html.fromHtml(str, imageGetter, null));