首先添加ImageLoader依赖:
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
之后实例化ImageLoader:
ImageLoader imageLoader = ImageLoader.getInstance(); imageLoader.init(ImageLoaderConfiguration.createDefault(this));
最后就是调用
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());
}
}
}
textView.setText(Html.fromHtml(“html”, imageGetter, null));