java 将图像放入 webview 并适合其宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10225532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:07:03  来源:igfitidea点击:

Put an image inside a webview and fit it width

javaandroidwebview

提问by Addev

I'd like to use a WebView for displaying images potentially big (in order to seize it memory management). For the test i'm loading this code in the WebView

我想使用 WebView 来显示可能很大的图像(为了抓住它的内存管理)。对于测试,我正在 WebView 中加载此代码

<head></head>
<body>
    <img alt="test" src="file:///android_asset/cute-cat-sleeping.jpg">
</body>

The problem is that if I load it in a web "as is". The WebView only allow to zoom out until the first dimension of the image is fit in screen. In this example the image height is totally shown into the WebView and then no more zoom allowed:

问题是,如果我“按原样”将其加载到网络中。WebView 只允许缩小,直到图像的第一个维度适合屏幕。在这个例子中,图像高度完全显示在 WebView 中,然后不再允许缩放:

No zoom no setUseWideViewPort

没有缩放没有 setUseWideViewPort

As you can see this mode doesn't allow a correct display of the global image despite that when the image is zoomed the behaviour in the right and bottom corners are correct as you can see here

如您所见,此模式不允许正确显示全局图像,尽管缩放图像时右下角的行为是正确的,如您在此处看到的

correct zoom display

正确的缩放显示

So I tried the (WebView).getSettings().setUseWideViewPort(true)and the result is that I can zoom out more than the image's width

所以我尝试了(WebView).getSettings().setUseWideViewPort(true)结果是我可以缩小超过图像的宽度

with setUseWideViewPort

使用 setUseWideViewPort

and the zoomed behaviour is not correct in the bottom-right borders:

并且右下角的缩放行为不正确:

zoom with setUseWideViewPort

使用 setUseWideViewPort 缩放

Summarizing: I'd like the max zoom out possible to be imageWidth=webview width and zoom behaviour like in the second image:

总结:我希望最大缩小可能是 imageWidth=webview 宽度和缩放行为,如第二张图像:

enter image description here

在此处输入图片说明

Complete and/or usefull answers will be bountied

完整和/或有用的答案将被奖励

EDIT: Bounty opened for Vinayak.B

编辑:赏金为 Vinayak.B 开放

回答by Vinayak Bevinakatti

Using Viewport Metadatamay help you to do this

使用视口元数据可以帮助您做到这一点

The viewport is the area in which your web page is drawn. Although the viewport's visible area matches the size of the screen, the viewport has its own dimensions that determine the number of pixels available to a web page

视口是绘制网页的区域。虽然视口的可见区域与屏幕的大小相匹配,但视口有自己的尺寸,决定了网页可用的像素数

Also try below

也试试下面

WebSettings settings = webView.getSettings();
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);

回答by judgement

Load Html file in Webview and put your image in asset folder and read that image file using Html.

在 Webview 中加载 Html 文件并将图像放入资产文件夹并使用 Html 读取该图像文件。

<htm>
  <table>
   <tr>
         <td>
           <img src="cat.gif" width="100%" alt="Hello">
         </td>
   </tr>
</table>
</html>

Now Load that Html file in Webview

现在在 Webview 中加载该 Html 文件

webview.loadUrl("file:///android_asset/abc.html"); 

Or another way

或者其他方式

if you use LinearLayout, can take a background parameter that can be a colour or a resource.

如果您使用 LinearLayout,则可以采用背景参数,该参数可以是颜色或资源。

So in main.xml file contains webview. you simply added a

所以在 main.xml 文件中包含了 webview。你只是添加了一个

android:background="@+drawable/backgroundmain"

and use

并使用

   web.setBackgroundColor(0);

To make the webview transparent to see the background image behind.

使 webview 透明以查看后面的背景图像。

回答by Qadir Hussain

here is the solution set the img tag width to 100% in HTML code and then load it in your webview using loadDataWithBaseURL(null, htnlString, "text/html", "UTF-8", null);method

这是解决方案,在 HTML 代码中将 img 标签宽度设置为 100%,然后使用loadDataWithBaseURL(null, htnlString, "text/html", "UTF-8", null);方法将其加载到您的 webview 中

try this code

试试这个代码

String htnlString = "<!DOCTYPE html><html><body style = \"text-align:center\"><img src=\"http://shiaislamicbooks.com/books_snaps/UR335/1.jpg\" alt=\"pageNo\" width=\"100%\"></body></html>";
yourWebView.loadDataWithBaseURL(null, htnlString, "text/html", "UTF-8", null);

回答by Sri Krishna Chaitanya

Edit your Webview:

编辑您的网络视图:

WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);