6-Android之TextView显示HTML
TextView tvProductContant = null;
Context context = null;
CharSequence text = Html.fromHtml(storeProductBean.getIntroduction(), new MImageGetter(tvProductContant, context), null);
tvProductContant.setText(text);
public class MImageGetter implements Html.ImageGetter {
Context c;
TextView container;
public MImageGetter(TextView text, Context c) {
this.c = c;
this.container = text;
}
@SuppressLint("CheckResult")
public Drawable getDrawable(String source) {
final LevelListDrawable drawable = new LevelListDrawable();
Glide.with(c).load(source).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
if (resource != null) {
Bitmap bitmap = Bitmap.createBitmap(
resource.getIntrinsicWidth(),
resource.getIntrinsicHeight(),
resource.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
resource.setBounds(0, 0, resource.getIntrinsicWidth(), resource.getIntrinsicHeight());
resource.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
drawable.addLevel(1, 1, bitmapDrawable);
int screenWidth = ScreenUtils.getScreenWidth(c);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
height = screenWidth * height / width;
if (width > screenWidth) {
drawable.setBounds(0, 0, screenWidth, height);
} else {
drawable.setBounds(0, 0, screenWidth, height);
}
drawable.setLevel(1);
container.invalidate();
container.setText(container.getText());
}
}
});
return drawable;
}
}