WebView中图片点击放大,浏览显示

1、给这个html中所有的img标签添加点击事件,通过JS来调用java的方法。

 mWebView.setWebViewClient(new PrWebViewClient(mWebView) {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //添加图片监听事件
                addImageClickListener();
                if (ivError.getVisibility() != View.VISIBLE) {
                    mWebView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    super.onReceivedError(view, errorCode, description, failingUrl);
                }
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                if (request.isForMainFrame() || TextUtils.equals(request.getUrl().toString(), view.getUrl())) {
                }
            }

            @Override
            public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
             
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                           SslError error) {
                handler.cancel();
            }
        });

              private void addImageClickListener() {
        // 这段js函数的功能就是,遍历所有的img几点,并添加onclick函数,在还是执行的时候调用本地接口传递url过去
              String url = "javascript:(function(){"
                + "var imgs = document.getElementsByTagName(\"img\");"
                + "var imgUrl='';"
                + "var arr = new Array();"
                + "for(var i=0;i

2、获取图片的URL

 mWebView.addJavascriptInterface(new JavascriptInterface(),"imagelistner");
    /**
     * js通信接口
     */
    public class JavascriptInterface {
        public JavascriptInterface() {
        }

        @android.webkit.JavascriptInterface
        public void openImage(String[]  arr, int postion) {
            ArrayList imgUrlList = new ArrayList<>();
            for (int i =0; i < arr.length; i ++) {
                imgUrlList.add(arr[i]);
            }
             //实现自己的图片浏览页面
          ImagePagerActivity.showActivity(context, imgsUrl, postion);
        }
        @android.webkit.JavascriptInterface
        public void openImage(String img, String chickUrl, int postion) {
            String[] imgs = img.split(",");
           ArrayList imgsUrl = new ArrayList<>();
          for (int i =0; i < imgs.length; i ++) {
                imgsUrl.add(imgs[i]);
            }
            //实现自己的图片浏览页面
          ImagePagerActivity.showActivity(context, imgsUrl, postion);
        }

        //注解 很重要,必不可少
        @android.webkit.JavascriptInterface
        public void openImage(String img) {
            String[] imgs = img.split(",");
            int postion = 0;
            ArrayList imgsUrl = new ArrayList<>();
            for (int i =0; i < imgs.length; i ++) {
                if (imgs[i].equals(chickUrl)){
                    postion = i;
                }
                imgsUrl.add(imgs[i]);
            }
            //实现自己的图片浏览页面
          ImagePagerActivity.showActivity(context, imgsUrl, postion);
        }
    }

你可能感兴趣的:(WebView中图片点击放大,浏览显示)