前言:如遇到Textview显示带图片的html内容时图片不适应控件大小情况下可以试一下如下方法
1、TextView显示html内容用法:
MyImageGetter2 imageGetter = new MyImageGetter2(context, txt_image);
txt_image.setText(Html.fromHtml(“html内容”, imageGetter, null));
public class MyImageGetter2 implements Html.ImageGetter {
Context c;
TextView textView;
int width;
public MyImageGetter2(Context context, TextView textView) {
this.c = context;
this.textView = textView;
width = c.getResources().getDisplayMetrics().heightPixels;//横屏的宽
}
@Override
public Drawable getDrawable(final String source) {
final UrlDrawable urlDrawable = new UrlDrawable();
String strSource;
if(TextUtils.isEmpty(source)){
return urlDrawable;
}
if(source.substring(0,4).contains("http") || source.substring(0,4).contains("https")){
strSource =source;
}else {
strSource ="https:"+source;
}
Glide.with(c)
.load(strSource)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap loadedImage, GlideAnimation<? super Bitmap> glideAnimation) {
float scaleWidth = ((float) width) / loadedImage.getWidth();
Matrix matrix = new Matrix();
matrix.postScale((float) (scaleWidth+0.1), (float) (scaleWidth+0.1));//根据情况可以适当调整
loadedImage = Bitmap.createBitmap(loadedImage, 0, 0,loadedImage.getWidth(), loadedImage.getHeight(),matrix, true);
urlDrawable.bitmap = loadedImage;
urlDrawable.setBounds(0, 0, loadedImage.getWidth(),loadedImage.getHeight());
textView.invalidate();
textView.setText(textView.getText());
}
});
return urlDrawable;
}
class UrlDrawable extends BitmapDrawable {
protected Bitmap bitmap;
@Override
public void draw(Canvas canvas) {
// override the draw to facilitate refresh function later
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, getPaint());
}
}
}
}
2、WebView显示html内容用法:
WebSettings websetting = ((ZongHeActivitysAdapter.ContentHolder) viewHolder).web_view.getSettings();
websetting.setDefaultTextEncodingName("utf-8") ;
websetting.setUseWideViewPort(true);//设定支持viewport
websetting.setLoadWithOverviewMode(true);//页面自适应手机屏幕
websetting.setBuiltInZoomControls(true);
websetting.setSupportZoom(true);//设定支持缩放
websetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
websetting.setBlockNetworkImage(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
websetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
websetting.setDomStorageEnabled(true); //开启DOM形式存储
websetting.setDatabaseEnabled(true); //开启数据库形式存储
String appCacheDir = context.getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath(); //缓存数据的存储地址
websetting.setAppCachePath(appCacheDir);
websetting.setAppCacheEnabled(true); //开启缓存功能
websetting.setCacheMode(WebSettings.LOAD_DEFAULT);//缓存模式
websetting.setAllowFileAccess(true);
web_view.loadDataWithBaseURL(null,getHtmlData(“html内容”),"text/html", "utf-8", null);
//------
private String getHtmlData(String bodyHTML) {
String head = "<head><style>img{max-width: 100%; width:auto; height: auto;}</style></head>";
return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
}