{"id":970,"date":"2023-03-11T20:13:53","date_gmt":"2023-03-11T12:13:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=970"},"modified":"2023-04-29T13:27:09","modified_gmt":"2023-04-29T05:27:09","slug":"screenshot-of-android-native-webview","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/screenshot-of-android-native-webview\/","title":{"rendered":"Android\u539f\u751fWebView\u622a\u56fe"},"content":{"rendered":"<p>Android\u539f\u751fWebView\u622a\u56fe\uff0c\u4ee5Android 5.0\u4e3a\u7248\u672c\u5206\u754c\u7ebf\uff0c\u622a\u56fe\u91c7\u7528\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n<h2>\u521d\u59cb\u5316WebView\u81ea\u9002\u5e94\u663e\u793a<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/**\n * \u81ea\u9002\u5e94\n * @param webView\n *\/\npublic static void initWebViewWithOverview(WebView webView) {\n    \/\/\u652f\u6301JavaScript\n    WebSettings webSettings = webView.getSettings();\n    webSettings.setJavaScriptEnabled(true);\n    \/\/\u6269\u5927\u6bd4\u4f8b\u7684\u7f29\u653e\n    webSettings.setUseWideViewPort(true);\n    \/\/\u8bbe\u7f6e\u53ef\u4ee5\u652f\u6301\u7f29\u653e\n    webSettings.setSupportZoom(true);\n    \/\/\u8bbe\u7f6e\u51fa\u73b0\u7f29\u653e\u5de5\u5177\n    webSettings.setBuiltInZoomControls(true);\n    \/\/\u81ea\u9002\u5e94\u5c4f\u5e55\n    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);\n    webSettings.setLoadWithOverviewMode(true);\n    \/\/\u4e0d\u663e\u793a\u6c34\u5e73\u6eda\u52a8\u6761\n    webView.setHorizontalScrollBarEnabled(false);\n    \/\/\u4e0d\u663e\u793a\u5782\u76f4\u6eda\u52a8\u6761\n    webView.setVerticalScrollBarEnabled(false);\n}<\/code><\/pre>\n<h2>Android 5.0\u4ee5\u4e0b\u7248\u672c<\/h2>\n<pre><code class=\"language-java\">\/**\n * Android5.0\u4ee5\u4e0b\u7248\u672c\n * \u5bf9WebView\u8fdb\u884c\u622a\u5c4f\uff0c\u867d\u7136\u4f7f\u7528\u8fc7\u671f\u65b9\u6cd5\uff0c\u4f46\u5728\u5f53\u524dAndroid\u7248\u672c\u4e2d\u6d4b\u8bd5\u53ef\u884c\n * @param webView\n * @return\n *\/\nprivate static Bitmap captureWebViewKitKat(WebView webView) {\n    Picture picture = webView.capturePicture();\n    int width = picture.getWidth();\n    int height = picture.getHeight();\n    if (width &gt; 0 &amp;&amp; height &gt; 0) {\n        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);\n        Canvas canvas = new Canvas(bitmap);\n        picture.draw(canvas);\n        return bitmap;\n    }\n    return null;\n}<\/code><\/pre>\n<h2>Android 5.0\u53ca\u4ee5\u4e0a\u7248\u672c<\/h2>\n<p>\u5728Android 5.0\u53ca\u4ee5\u4e0a\u7248\u672c\uff0cAndroid\u5bf9WebView\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u4e3a\u4e86\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u548c\u63d0\u9ad8\u6027\u80fd\uff0c\u4f7f\u7528WebView\u52a0\u8f7d\u7f51\u9875\u65f6\u53ea\u7ed8\u5236\u663e\u793a\u90e8\u5206\u3002\u5982\u679c\u6211\u4eec\u4e0d\u505a\u5904\u7406\uff0c\u4ecd\u7136\u4f7f\u7528\u4e0a\u8ff0\u4ee3\u7801\u622a\u56fe\u7684\u8bdd\uff0c\u5c31\u4f1a\u51fa\u73b0\u53ea\u622a\u5230\u5c4f\u5e55\u5185\u663e\u793a\u7684WebView\u5185\u5bb9\uff0c\u5176\u5b83\u90e8\u5206\u662f\u7a7a\u767d\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u901a\u8fc7\u8c03\u7528<code>WebView.enableSlowWholeDocumentDraw()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5173\u95ed\u8fd9\u79cd\u4f18\u5316\uff0c\u4f46\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8be5\u65b9\u6cd5\u9700\u8981\u5728WebView\u5b9e\u4f8b\u88ab\u521b\u5efa\u524d\u5c31\u8981\u8c03\u7528\uff0c\u5426\u5219\u6ca1\u6709\u6548\u679c\u3002\u6240\u4ee5\u6211\u4eec\u5728WebView\u5b9e\u4f8b\u88ab\u521b\u5efa\u524d\u52a0\u5165\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {\n    android.webkit.WebView.enableSlowWholeDocumentDraw();\n}<\/code><\/pre>\n<p>\u6839\u636eGoogle\u6587\u6863\u4e2d\u63cf\u8ff0\uff0ccapturePicture()\u65b9\u6cd5\u5df2\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u63a8\u8350\u6211\u4eec\u901a\u8fc7WebView\u7684onDraw(Canvas)\u53bb\u83b7\u53d6\u56fe\u50cf\uff0c\u6240\u4ee5\u6211\u4eec\u62ff\u5230\u7f51\u9875\u7684\u5bbd\u9ad8\u540e\uff0c\u5373\u53ef\u8c03\u7528WebView.draw(Canvas)\u65b9\u6cd5\u751f\u6210webView\u622a\u56fe\u3002<\/p>\n<pre><code class=\"language-java\">\/**\n * \u5355\u56fe\n * Android5.0\u53ca\u4ee5\u4e0a\u7248\u672c\n * @param webView\n * @return\n *\/\nprivate static Bitmap captureWebViewLollipop(WebView webView) {\n    int width = webView.getWidth();\n    int height = webView.getHeight();\n    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);\n    Canvas canvas = new Canvas(bitmap);\n    webView.draw(canvas);\n    return bitmap;\n}\n\n\/*\nprivate static Bitmap captureWebViewLollipop(WebView webView) {\n    float scale = webView.getScale();\n    int width = webView.getWidth();\n    int height = (int) (webView.getContentHeight() * scale + 0.5);\n    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);\n    Canvas canvas = new Canvas(bitmap);\n    webView.draw(canvas);\n    return bitmap;\n}\n*\/\n\n\/**\n * \u957f\u56fe\n * \u89e3\u51b35.0+\u4e0a\uff0c\u622a\u53d6\u7684\u5feb\u7167\u53ea\u663e\u793aWebView\u4e2d\u663e\u793a\u51fa\u6765\u7684\u90e8\u5206\uff0c\u6ca1\u6709\u663e\u793a\u51fa\u6765\u7684\u90e8\u5206\u662f\u7a7a\u767d\u7684\n * @param webView\n * @return\n *\/\npublic static Bitmap captureLongWebViewLollipop(WebView webView){\n    int height = webView.getContentHeight();\n    int width = webView.getWidth();\n    int pH = webView.getHeight();\n    Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);\n    Canvas canvas = new Canvas(bm);\n    int top = height;\n    while (top &gt; 0) {\n        if (top &lt; pH) {\n            top = 0;\n        } else {\n            top -= pH;\n        }\n        canvas.save();\n        canvas.clipRect(0, top, width, top + pH);\n        webView.scrollTo(0, top);\n        webView.draw(canvas);\n        canvas.restore();\n    }\n    return bm;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android\u539f\u751fWebView\u622a\u56fe\uff0c\u4ee5Android 5.0\u4e3a\u7248\u672c\u5206\u754c\u7ebf\uff0c\u622a\u56fe\u91c7\u7528\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\u3002 \u521d\u59cb\u5316W [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[67],"class_list":["post-970","post","type-post","status-publish","format-standard","hentry","category-android-ui","tag-webview"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=970"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/970\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}