使用文本自动调整大小调整 WebView 中的所有图像
Resize ALL images in WebView with Text Autosizing
我将网络视图设置为:
WebView wv = (WebView) findViewById(R.id.display_post_activity_content);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
} else {
wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
wv.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");
content
是静态的 HTML,有一些内联 CSS。没有 JavaScript。
这在文本换行方面做得很好。它还会调整较大图像的大小以适应屏幕宽度。
但是,有些内联图像小于设备宽度。文字环绕这些。
我想让它调整所有图像(即使是小图像)的宽度(保持纵横比)。是否可以使用 WebView 来实现?
我可以用 Html.fromHtml()
和 ImageGetter
来做到这一点,但是 WebView
的性能要好得多。
好的,所以我自己解决了这个问题:
// Content to be rendered
content = "<p>Some Html </p> with <img></img> tags";
// Set width and height
content = content.replace("<img", "<img height=\"auto\" width=\"100%\" ");
// Render
wv.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");
瞧,保留纵横比的全宽图像!
我将网络视图设置为:
WebView wv = (WebView) findViewById(R.id.display_post_activity_content);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
} else {
wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
wv.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");
content
是静态的 HTML,有一些内联 CSS。没有 JavaScript。
这在文本换行方面做得很好。它还会调整较大图像的大小以适应屏幕宽度。
但是,有些内联图像小于设备宽度。文字环绕这些。
我想让它调整所有图像(即使是小图像)的宽度(保持纵横比)。是否可以使用 WebView 来实现?
我可以用 Html.fromHtml()
和 ImageGetter
来做到这一点,但是 WebView
的性能要好得多。
好的,所以我自己解决了这个问题:
// Content to be rendered
content = "<p>Some Html </p> with <img></img> tags";
// Set width and height
content = content.replace("<img", "<img height=\"auto\" width=\"100%\" ");
// Render
wv.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");
瞧,保留纵横比的全宽图像!