Android WebView加载html文本时,图片太大不能适配屏幕

遇到这么长一段html文本,图片大小不一,调webView也没什么效果,可以试试Jsoup


String htmltext2= "<p>\r\n\t\"英美剧鲜肉系列\"之《少狼众美男》第三弹!本期要为大家介绍的,就是第四季加入的角色Liam Dunbar的扮演者Dylan Sprayberry!\r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807145715_74483.jpg\" alt=\"\" width=\"600\" height=\"600\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<br />\r\n</p>\r\n<p>\r\n\t<br />\r\n</p>\r\n<p>\r\n\t<span>Dylan Sprayberry</span>于1998年7月7日出生于美国德州休斯敦,今年也就17岁,够鲜嫩吧!目前身高165厘米,本部落真心希望他二十三猛一窜,好歹窜到175。\r\n</p>\r\n<p>\r\n\t<br />\r\n</p>\r\n<p>\r\n\t<span style=\"line-height:1.5;\">看看他的颜值,眼眸清澈如水:</span> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807143135_52152.gif\" alt=\"\" width=\"200\" height=\"206\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143136_46795.gif\" alt=\"\" width=\"200\" height=\"206\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143139_77323.gif\" alt=\"\" width=\"200\" height=\"206\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807143141_47716.gif\" alt=\"\" width=\"200\" height=\"206\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143143_26661.gif\" alt=\"\" width=\"200\" height=\"206\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143405_96855.gif\" alt=\"\" width=\"200\" height=\"206\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807143436_97604.gif\" alt=\"\" width=\"300\" height=\"282\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143439_56819.gif\" alt=\"\" width=\"300\" height=\"282\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807143524_47482.png\" alt=\"\" width=\"600\" height=\"814\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143526_42860.png\" alt=\"\" width=\"600\" height=\"911\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143528_10491.png\" alt=\"\" width=\"600\" height=\"401\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143530_31425.png\" alt=\"\" width=\"600\" height=\"896\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143532_21796.png\" alt=\"\" width=\"600\" height=\"398\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143533_62276.png\" alt=\"\" width=\"600\" height=\"901\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143535_14151.png\" alt=\"\" width=\"600\" height=\"399\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143536_61739.png\" alt=\"\" width=\"600\" height=\"898\" title=\"\" align=\"\" /> \r\n</p>\r\n<br />\r\n<p>\r\n\t2013年,Dylan在《超人:钢铁英雄》中饰演13岁时的超人克拉克·肯特,并在2014年的土星奖中获得\"年轻演员最佳表演奖\"提名。随后加入MTV热播青春科幻剧《少狼 第四季》,饰演被男主Scott救下咬伤转变成的狼人Liam Dunbar,并在第五季中成为常驻角色。最初Dylan试镜的角色是第三季中的小德里克,但因为当时年纪太小,并未成功。\r\n</p>\r\n<p>\r\n\t<span style=\"line-height:1.5;\">Dylan Sprayberry在<span>《超人:钢铁英雄》中的剧照:</span></span> \r\n</p>\r\n<p>\r\n\t<span style=\"line-height:1.5;\"><img src=\"http://img.princeblog.com/img/20150807/20150807143749_98220.jpg\" alt=\"\" width=\"600\" height=\"399\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143750_38194.jpg\" alt=\"\" width=\"600\" height=\"399\" title=\"\" align=\"\" /><br />\r\n</span> \r\n</p>\r\n<p>\r\n\t<span style=\"line-height:1.5;\"></span> \r\n</p>\r\n<p>\r\n\t<span><br />\r\n</span> \r\n</p>\r\n<p>\r\n\t<span>Dylan Sprayberry在《少狼》中的剧照:</span> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807144819_33623.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807144821_35194.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807144823_66929.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807144825_78794.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807144826_60907.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807144828_90984.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807144829_92538.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807144831_54918.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807145251_78725.gif\" alt=\"\" width=\"200\" height=\"200\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807145335_49115.gif\" alt=\"\" width=\"300\" height=\"268\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807145346_98384.gif\" alt=\"\" width=\"300\" height=\"300\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807145005_12372.gif\" alt=\"\" width=\"300\" height=\"300\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807145007_63172.gif\" alt=\"\" width=\"300\" height=\"300\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t没错,Dylan所饰演的Liam在剧里全面负责卖萌、搞笑、打斗等等。看看男主对Dlyan的喜爱之情,恨不得Dylan是自己亲弟弟:\r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807145643_43314.png\" alt=\"\" width=\"600\" height=\"493\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807145644_81822.png\" alt=\"\" width=\"600\" height=\"904\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\t<br />\r\n</p>\r\n<p>\r\n\t<br />\r\n</p>\r\n<p>\r\n\tDylan有个妹妹,名叫Ellery Sprayberry,也是名演员。看看兄妹二人的合照,果然家里的基因不错。\r\n</p>\r\n<p>\r\n\t<img src=\"http://img.princeblog.com/img/20150807/20150807143854_66207.png\" alt=\"\" width=\"600\" height=\"900\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143856_29568.png\" alt=\"\" width=\"600\" height=\"902\" title=\"\" align=\"\" /><img src=\"http://img.princeblog.com/img/20150807/20150807143858_11117.png\" alt=\"\" width=\"600\" height=\"404\" title=\"\" align=\"\" /> \r\n</p>\r\n<p>\r\n\tPRINCEBLOG王子部落 原创文章\r\n</p>";

方法封装好了

 private String getNewContent(String htmltext){
    	
    	Document doc=Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
        	element.attr("width","100%").attr("height","auto");
		}
        
        Log.d("VACK", doc.toString());
        return doc.toString();
    }


接下来只要调用webView.loadDataWithBaseURL(null,getNewContent(htmltext2), "text/html", "utf-8", null);


就可以让图片大小和屏幕同宽,平且高度自动缩放,而且gif也是正常的缩放


如果觉得图片大小还不好看可以继续调这里

element.attr("width","100%").attr("height","auto");


改动前的效果,屏幕都不足显示图片的一半



而改动后,大小刚好



另外如果你导入的jsoup包在1.63以上可能会出现

ExceptionInInitializerError

这个时候千万别质疑我的代码,好像是20以上的api加载不到jar包,只要把包换成1.61就可以了

注意导包是这几个
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;//这个特别容易导错
import org.jsoup.select.Elements;


vack首次发帖,请指教

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值