Textview、WebView显示html内容,适配图片适应屏幕

前言:如遇到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>";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值