/** * webview 显示本地图片,自适应布局大小,图片可缩放 * * @param mContext * @param webview * @param imageLocalUrl * @param isAdapterScreenWidth 是否自适应屏幕宽度 * @param color 0-255 */ public static void showLocalImage( Context mContext, final WebView webview, final String imageLocalUrl, int color, boolean isAdapterScreenWidth, boolean doubleClickEabled) { boolean fileExist = FileUtils.isExists(imageLocalUrl); if (fileExist) { String bgcolor = ColorUtils.toBrowserColor(color); ZogUtils.printLog(WebViewUtils.class, "bgcolor:" + bgcolor); String adapterScreenWidth = ""; if (isAdapterScreenWidth) { adapterScreenWidth = " width:99.9%;"; } String style = "<style>" + "* { margin:0; padding:0; background-color:" + bgcolor + "; }" + "img { " + adapterScreenWidth + " margin:0; padding:0; }" + "div{" + adapterScreenWidth + // " border: thin solid #F00;" + " margin:0; padding:0;" + " }/*这里的width height 大于图片的宽高*/" + "table{ height:100%; width:100%; text-align:center;}" + " </style>"; String body = " <body>" + " <div>" + " <table>" + " <tr>" + " <td>" + " <img src=\"file://" + imageLocalUrl + "\"" + // " width=" + width + " margin=" + 0 + " padding=" + 0 + // " height="+height+ " />" + " </td>" + " </tr>" + " </table>" + " </div>" + " </body>" + ""; String data = style + body; webview.loadDataWithBaseURL("file://" + imageLocalUrl, data, "text/html", "utf-8", null); // webview.loadUrl(imageUrl);//直接显示网上图片 webview.setVerticalScrollBarEnabled(false); // 取消Vertical ScrollBar显示 webview.setHorizontalScrollBarEnabled(false); // 取消Horizontal ScrollBar显示 webview.getSettings().setBuiltInZoomControls(true); // 显示放大缩小 controler webview.getSettings().setSupportZoom(true); // 可以缩放 setZoomControlGone(webview); // 去掉zoom按钮 if (doubleClickEabled) { // 双击缩放 webview.getSettings().setUseWideViewPort(true); webview.getSettings().setLoadWithOverviewMode(true); } webview.setSaveEnabled(true); } }