也许你看到这个标题第一个想法就是,这不是 h5 同事去做的事吗,直接给个 url 地址,我们直接加载就行了。但是这个是根据业务情况来的,很多情况下加载的是 html 的代码片段,而不是 url地址。
在我们项目中,很多情况是课程的介绍还有文章的详情都是加载的Html 片段。在后台管理系统编辑的时候通过通用的插件使其插入图片及其他元素,存在数据库里的时候就是其里面的一个字段。展示如下图:
优化开启
因为是两个安卓跟 IOS 端都要优化,IOS 同事仿微信图片懒加载先做的差不多了,我这边又在其基础上做了些优化。安卓的话搜的资料倒不是很多,好在跟 IOS 都异曲同工吧算是。
先说一下实现思路吧:
本地加载基于 jquery的懒加载的 js 文件,加载进行展示
相关文件如下:
下载地址:https://github.com/xinghedazhan/lazyload/tree/master
将两个 js 文件放在项目目录的assets目录下
因为加载Html 的代码片段是自己添加网页的头跟尾的,所以在头里面添加两个 js 文件的引用。
" <script src='file:///android_asset/jquery.min.js'></script>"
" <script src='file:///android_asset/jquery.lazyload.js'></script>"
2.添加 js方法调用
"<script type='text/javascript'>" +
"jQuery(document).ready(" +
"function(){" +
"$(\"img\").lazyload({" +
"placeholder : 'data:image/gif;base64,R0lGODlh/AF9AbMAAP///+7u7t3d3czMzLu7u6qqqpmZmYiIiHd3d2ZmZv4BAgAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgAKACwAAAAA/AF9AQAE/xDISau9OOvNu/9gKI5kaZ5oqq5s675wLM90bd94ru987//AoHBILBqPyKRyyWw6n9CodEqtWq/YrHbL7Xq/4LB4TC6bz+i0es1uu9/wuHxOr9vv+Lx+z+/7/4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AAMKHEiwoMGDCBMqXMiwocOHECNKnEixosWLGDNq3Mixo8ePIP9D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UGBwctZgSNB245MoVLkY2XFFkIB4Jdkp8aAZEICQkGfRJInp6jOAanqKgHgAi4B7mgGwSztKinuLiJRLLAtMS8S017TjURACH5BAUKAAoALO0AngAeAB4AAAS2EMhJa6g4axC671s4fZ1gXmJGBmYrpDDrwrA5DC8tBveti4Je7qcJ+oibG2GIrAgIhEFTBSWgppQqEwsYQAVXrjcK4nahA5lZG6hyCYVloFA4bHVz+sVwQBimA3QEEgMICAlhOnQFUhyHCQdIcAYFWQmXfzqTjBWPCZUwBAajoBQCngeJFqKjBqqOlwkIBXdtraOvEgGehn4FlAfBwbgpBbG9CH3CwYM0AQbIycnMuUBQlFF3FREAIfkEBQoACgAs7QCeAB4AHgAABLwQyEmrvTjrzWfoYCCKIDeSpXZ+FpuKAuq5r2DTQ5x6tjAJhAEt1WMRCgTfThLoAQIGw0G5BAyu1gMCUZ1chYTtoSsRXKGIxJhszhkSajKgDYSvu2ZCgABPyAlBNn1UO3uAEmkJBXiABBIFfUMgjUoBfQZLA40UkAkIiyWagIQAW1ugHANIhxUBpggGkhVHBUiyrmIISS2qtbYZAVoIB8QHSEhRUbV6HGHFxcnKBgMle88H0QXUVQFXtWcbEQAh+QQFCgAKACztAJ8AHgAdAAAEuhDIOUOgOGsqhtjgFhDEcIWoFBhGYaZocMwGAaMHots3WCRAQ6+CCQATh6HKcgL8gM0hc4IIKitTI3BwxV4Ix+5EYAGAkWKVgGywpgNrmVsMJ4MRwvTaXkWkAXtwOghRUgMeEoM8Vx2ITjoHhTeHLwABCDkFjJQUBJgHizACJY4TMzOhIaMDJRkypwWSGKwkXBqvBywEH0W0JKlFLLksNb8EBcbAGgTELAXPz8ckvDfMzi3QrV1wtHEoEQA7'," + /* 占位图 */
"threshold : 50," + /* 距离图片多少的位置处开始下载 */
"failure_limit : 10," + /* 最大不在加载范围图片数, 主要是瀑布流布局时设置 */
"effect: \"fadeIn\"," + /* 图片加载方式 */
"skip_invisible : true" +
"});" +
"});" +
"</script>" +
看方法的含义应该是网页框架加载完毕的时候再进行的图片的懒加载。placeholder代表的是占位图,就是我放的是holder.gif的二进制字符串,当时以为时间紧还有文件也不大,直接就用的二进制了。有想法的可以放本地加载引用试试。
3.替换 img标签的 src 属性
因为jquery的懒加载框架的加载不是用普通的 src 属性来加载的,需要替换成data-original,同时还要考虑 Video 跟 Audio 等标签里面的 src 属性不能替换,故写了个匹配替换方法如下:
/**
* 变img标签里面的 src 属性为 data-original
*/
public static String takeImgSrc(String htmlStr) {
String line=htmlStr;
String itemStr="";
String pattern = "<img.*?>";
// 创建 Pattern 对象
Pattern r = Pattern.compile(pattern);
// 现在创建 matcher 对象
Matcher m = r.matcher(line);
while (m.find()){
if (m.group(0)!=null){
if (m.group(0).contains("src")){
itemStr = m.group(0).replace("src","data-original");
line = line.replace(m.group(0), itemStr);
}
}
}
return line;
}
至此应该可以实现仿微信的 lazyload 了。不足的地方欢迎大家指正,共同进步