您的位置:首页 > Web前端 > HTML

TextView使用Glide加载html图片

2016-08-30 14:24 363 查看
public class MyImageGetter implements Html.ImageGetter {

private URLDrawable urlDrawable = null;
private TextView textView;
private Context context;

public MyImageGetter(Context context, TextView textView) {
this.textView = textView;
this.context = context;
}

@Override
public Drawable getDrawable(final String source) {
urlDrawable = new URLDrawable();

Glide.with(context).load(source).asBitmap().fitCenter().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
urlDrawable.bitmap = resource;
Logger.getLogger().d("加载的图片,Width:" + resource.getWidth() + ",Height:" + resource.getHeight());
urlDrawable.setBounds(0, 0, resource.getWidth(), resource.getHeight());
textView.invalidate();
textView.setText(textView.getText());//不加这句显示不出来图片,原因不详
}
});
return urlDrawable;
}

public class URLDrawable extends BitmapDrawable {
public Bitmap bitmap;

@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, getPaint());
}
}
}
}


使用方法:

tv_RichText.setText(Html.fromHtml(richText, new MyImageGetter(mContext, tv_RichText), null));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息