需求: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来实现