Android通过webview显示本地图片模糊

发布于 2024-11-17 18:06:19 字数 192 浏览 2 评论 0原文

我有一个从 asset/ 文件夹加载图像的网络视图。图像显示为缩放 100%(或更多),并且与真实图像在 100% 时的清晰度不同

如何设置 webview 以显示缩小的图像而不降低质量?

另外,我正在使用 webview,因为 ImageView 需要我重新发明平滑滚动、点击缩放、捏合缩放等,因此

Insight 赞赏

I have a webview loading an image from the assets/ folder. The image is displayed zoomed 100% (or more) and does not have the same clarity as the real image has at 100%

How can I set the webview to display the image zoomed out AND not downscale the quality?

Also, I am using a webview because ImageView would require me to reinvent smooth scrolling, tap to zoom, pinch to zoom, etc

Insight appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我纯我任性 2024-11-24 18:06:19

我记得那个问题。 Webview 可能出于内存问题自动对较大图像进行下采样。如果你想控制质量,你可能必须使用 ImageView。您也可以尝试使用图库应用程序。

I recall that problem. Webview automatically downsamples larger images probably out of memory concerns. If you want control of the quality you probably have to use ImageView. You could also try to use the gallery application instead.

莫言歌 2024-11-24 18:06:19

我找到了在网络视图中显示高质量图像的解决方案。 (让大家知道我讨厌使用网络视图!)

根本原因:
开箱即用,在 web 视图中没有 XXHDPI 和 XHDPI 的概念 - 如果您确实在 XXHDPI 或 XHDPI 设备上使用这些图像,那么该图像太大了!所以你最终会使用 HDPI/MDPI 图像......至少我这样做了,然后图像看起来很模糊。

解决方案描述:
在您的 HTML 中(我以编程方式在活动中创建它),启用缩放,然后以编程方式检测逻辑密度。计算比例(即 XXHDPI 设备上的 300%)并将该值放入您的 HTML 中,否则您的字体将很小!在您的可绘制资源中,确保您使用的图形与设备的分辨率相匹配(即 XXHDPI 设备上的 XXHDPI 图像) - 这是替换 XXHDPI res 文件夹中的 MDPI 图像的提醒。

编码解决方案:

// constants needed to put together a nice Android webview-friendly page
private static final String HTML_STYLE = "<style>img{image-rendering: optimizeQuality;} html{font-size: [TBD]% !important}</style>";
private static final String HTML_META = "<meta name='viewport' content='initial-scale=1.0, maximum-scale=1.0, user-scalable=no; target-densitydpi=device-dpi' />";
private static final String HTML_HEAD = "<html><head><title>description</title>"+HTML_META+HTML_STYLE+"</head><body bgcolor=\"black\"><font color=\"white\">";
private static final String HTML_TAIL = "</font></body></html>";

// content to show in the webview
final String strBody = "<ul><li>stuff1</li><li>stuff2</li><li>stuff3</li></ul><p align=\"center\"><img src=\"file:///android_res/drawable/image.png\"></p>";

// get the logical font size
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int iFontScale = (int) (displaymetrics.scaledDensity * 100.0f);

// alter the HTML to programmatically insert the font scale
final String strCompleteHTML = (HTML_HEAD+strBody+HTML_TAIL).replace("[TBD]", String.valueOf(iFontScale));

// configure the webview and load the HTML
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setHorizontalScrollBarEnabled(false);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL("file:///android_asset/", strCompleteHTML, "text/html", "utf-8", null);

I found the solution to showing good quality images in a webview. (let it be known that I loathe working with the webview!)

Root Cause:
Out of the box, in a webview there's no concept of XXHDPI and XHDPI - if you do use those images on an XXHDPI or XHDPI device then the image is too damn large ! So you end up using HDPI/MDPI images... at least I did and then the images look blurred.

Solution Description:
In your HTML (I programmatically create it within the activity), enable scaling and then programmatically detect the logical density. Calculate the scale (i.e. 300% on a XXHDPI device) and put that value into your HTML otherwise your font will be TINY! Within your drawable resources, ensure you're using graphics that match the resolution of the device (i.e. XXHDPI images on an XXHDPI device) - this is a reminder to replace the MDPI images in your XXHDPI res folder.

Coded Solution:

// constants needed to put together a nice Android webview-friendly page
private static final String HTML_STYLE = "<style>img{image-rendering: optimizeQuality;} html{font-size: [TBD]% !important}</style>";
private static final String HTML_META = "<meta name='viewport' content='initial-scale=1.0, maximum-scale=1.0, user-scalable=no; target-densitydpi=device-dpi' />";
private static final String HTML_HEAD = "<html><head><title>description</title>"+HTML_META+HTML_STYLE+"</head><body bgcolor=\"black\"><font color=\"white\">";
private static final String HTML_TAIL = "</font></body></html>";

// content to show in the webview
final String strBody = "<ul><li>stuff1</li><li>stuff2</li><li>stuff3</li></ul><p align=\"center\"><img src=\"file:///android_res/drawable/image.png\"></p>";

// get the logical font size
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int iFontScale = (int) (displaymetrics.scaledDensity * 100.0f);

// alter the HTML to programmatically insert the font scale
final String strCompleteHTML = (HTML_HEAD+strBody+HTML_TAIL).replace("[TBD]", String.valueOf(iFontScale));

// configure the webview and load the HTML
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setHorizontalScrollBarEnabled(false);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL("file:///android_asset/", strCompleteHTML, "text/html", "utf-8", null);
酒几许 2024-11-24 18:06:19

不确定为什么需要所有这些相当复杂的答案?

我通过使用 400px 图像解决了 200px 图像模糊的问题,只需在 CSS 中将其缩小 2 倍即可。 DIV 元素设置为 200px 且背景大小:contain/200px 50px;为我解决了

Not sure why all those rather complicated answers are needed?

I solved the problem where a 200px image was blurry by using a 400px image and just downsize it by the factor 2 in CSS. DIV element set to 200px and background-size:contain/200px 50px; solved it for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文