{"id":971,"date":"2023-03-11T20:14:44","date_gmt":"2023-03-11T12:14:44","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=971"},"modified":"2023-04-29T13:27:02","modified_gmt":"2023-04-29T05:27:02","slug":"android-native-webview-screenshot-supports-scaling","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-native-webview-screenshot-supports-scaling\/","title":{"rendered":"Android\u539f\u751fWebView\u622a\u56fe\uff08\u652f\u6301\u7f29\u653e\uff09"},"content":{"rendered":"<p>Android\u539f\u751fWebView\u622a\u56fe\uff0c\u652f\u6301Android 5.0\u53ca\u4ee5\u4e0a\uff0c\u652f\u6301\u7f29\u653e<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/**\n * \u7f29\u653e\u56fe\n * @param context\n * @param webView\n *\/\npublic static void captureScaleWebViewLollipop(final Context context, final WebView webView) {\n    final float scale = webView.getScale();\n    final int height = (int) (webView.getContentHeight() * scale);\n    final int width = (int) (webView.getWidth() * scale);\n    if (scale &gt; 3) {\n        NToasty.shortToastError(context, context.getResources().getString(R.string.webview_scale_too_large));\n        return;\n    }\n    new Handler().postDelayed(new Runnable() {\n        @Override\n        public void run() {\n            Log.i(&quot;yezhou&quot;, &quot;width: &quot; + width + &quot;, height: &quot; + height + &quot;, scale: &quot; + scale);\n            int pH = webView.getHeight();\n            int pW = webView.getWidth();\n            final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);\n            Canvas canvas = new Canvas(bitmap);\n            int top = 0;\n            int left = 0;\n            while (top &lt; height) {\n                left = 0;\n                while (left &lt; width) {\n                    canvas.save();\n                    if (left + pW &lt; width &amp;&amp; top + pH &lt; height) {\n                        canvas.clipRect(left, top, left + pW, top + pH);\n                        \/\/\u56e0\u7f29\u653e\u540e\u5185\u5bb9\u5bbd\u9ad8\u5927\u6982\u7387\u4e0d\u662fWebView\u5bbd\u9ad8\u7684\u6574\u6570\u500d\uff0c\u5bfc\u81f4\u672b\u7aef\u65e0\u6cd5\u6ed1\u52a8\u81f3\u6b64\u4f4d\u7f6e\n                        webView.scrollTo(left, top);\n                    } else if (left + pW &gt; width &amp;&amp; top + pH &lt; height) {\n                        canvas.clipRect(width - pW, top, width, top + pH);\n                        webView.scrollTo(width - pW, top);\n                    } else if (left + pW &lt; width &amp;&amp; top + pH &gt; height) {\n                        canvas.clipRect(left, height - pH, left + pW, height);\n                        webView.scrollTo(left, height - pH);\n                    } else {\n                        canvas.clipRect(width - pW, height - pH, width, height);\n                        webView.scrollTo(width - pW, height - pH);\n                    }\n                    webView.draw(canvas);\n                    canvas.restore();\n                    left += pW;\n                }\n                top += pH;\n            }\n            saveWebViewBitmap(context, bitmap);\n        }\n    }, 100);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android\u539f\u751fWebView\u622a\u56fe\uff0c\u652f\u6301Android 5.0\u53ca\u4ee5\u4e0a\uff0c\u652f\u6301\u7f29\u653e \/** * \u7f29\u653e\u56fe * @p [&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-971","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\/971","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=971"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/971\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}