首页 > Web开发 > 详细

WebView图片点击查看

时间:2018-01-23 10:33:42      阅读:238      评论:0      收藏:0      [点我收藏+]

需求:WebView中的图片点击后放大全屏查看

this.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogHelp.d("lyg", "shouldOverrideUrlLoading:" + url);
                if (!TextUtils.isEmpty(url) && isAutoUrl() && (url.startsWith("http://") || url
                        .startsWith("xuehu://"))) {
                    URLBus.doURL(url, "", getContext(), CustomWebView.this, isOpenNewActivity);
                    return true;
                } else {
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                EventBus.getDefault().post(new WebViewCloseEvent());
                mLists.clear();
                addImageListener();
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }
        });
//---------webview图片点击开始------------------
    private void addImageListener() {
        //in some case, src of an img tag might be base64 string but no an url
        //we can get image url by data-src if exists
        loadUrl("javascript:(function(){ "
                + " var objs = document.getElementsByTagName(\"img\"); "
                + " for(var i=0;i<objs.length;i++)  "
                + " {"
                + "     if(objs[i].getAttributeNode(\"onclick\").specified) continue; "
                + "     window.imageListener.addImage(objs[i].src, objs[i].dataset.src); "
                + "     objs[i].onclick=function()  "
                + "    {  "
                + "      window.imageListener.openImage(this.src, this.dataset.src);  "
                + "     }  "
                + " } "
                + " })()");
    }

通过重写js来实现

 

WebView图片点击查看

原文:https://www.cnblogs.com/anni-qianqian/p/8333985.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!