android webview 图片缩放,Android WebView,缩放图像以适合屏幕

这篇博客介绍了如何在Android的WebView中使图片自动缩放以适应屏幕宽度。作者分享了多种方法,包括在HTML数据中添加CSS样式、设置WebView的加载模式、修改WebView设置以及在XML或HTML内容中插入宽度属性等。这些解决方案有助于解决WebView加载图片时出现的空白区域问题,并确保图片能正确缩放显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android WebView,缩放图像以适合屏幕

我所拥有的:我正在从URL加载图像。 我只是做WebView.getSettings()

我得到的是:图像未在WebView的整个宽度上显示,它周围有空白(例如,如果您在桌面浏览器中打开一些iamge)

我尝试了什么:将LayoutAlgorithm设置为SINGLE_COLUMN。 完美工作,但是缩放不起作用。 (我在WebView.getSettings()中启用了此功能,不知道为什么。设置setUseWideViewPort(true);加载图像时没有任何空格,但已完全放大。

9个解决方案

85 votes

您也可以这样做。

在数据字符串的开头(取决于您的Web数据格式)添加img的CSS样式。

为了快速对WebView中的数据进行处理,我这样做了。

WebView content = (WebView) findViewById(R.id.webView1);

content.loadDataWithBaseURL(null, "" + post.getContent(), "text/html", "UTF-8", null);

这几乎与bansal21ankit所说的一样,但是它可以在HTML中的每个图像上工作,而无需额外的工作。

编辑(关于帖子内容的说明):

您可以具有任何text/html值,而不是示例中的style。

此处的发布内容只是text/html内容的示例,该内容是从某些数据源加载的,然后与style部分连接在一起,该部分使给定内容中的任何图像都适合屏幕。

Tony answered 2020-07-19T17:57:57Z

47 votes

我有同样的问题,这样做很好:

Display display = getWindowManager().getDefaultDisplay();

int width = display.getWidth();

String data = "

Example";

data = data + "

%5C%22%22+url+%22%5C%22
";

webView.loadData(data, "text/html", null);

编辑:由于这仍然是公认的答案,因此,这是一个更好的解决方案(以下全部归功于Tony):

WebView content = (WebView) findViewById(R.id.webView1);

content.loadDataWithBaseURL(null, "" + post.getContent(), "text/html", "UTF-8", null);

Sebastian Breit answered 2020-07-19T17:57:10Z

39 votes

您应该缩放webView以适合屏幕:

WebView data = (WebView) getViewById(R.id.webview1);

data.getSettings().setLoadWithOverviewMode(true);

data.getSettings().setUseWideViewPort(true);

thepoosh answered 2020-07-19T17:58:17Z

9 votes

对我有用的是:我读到为了适合屏幕的宽度,您应该将此添加到字段中的HTML或xml中:

width="100%"

所以我要做的是,没有缩放和缩放图像,而是得到了xml,将其放在StringBuilder中,在字段内找到src =“ [https://blablabla.com/image.png”], 在“ src”子字符串之前,我插入了“ width =“ 100%””,然后使用StringBuilder设置了我的webView,mi代码是这样的:

public void setWebViewWithImageFit(String content){

// content is the content of the HTML or XML.

String stringToAdd = "width=\"100%\" ";

// Create a StringBuilder to insert string in the middle of content.

StringBuilder sb = new StringBuilder(content);

int i = 0;

int cont = 0;

// Check for the "src" substring, if it exists, take the index where

// it appears and insert the stringToAdd there, then increment a counter

// because the string gets altered and you should sum the length of the inserted substring

while(i != -1){

i = content.indexOf("src", i + 1);

if(i != -1) sb.insert(i + (cont * stringToAdd.length()), stringToAdd );

++cont;

}

// Set the webView with the StringBuilder: sb.toString()

WebView detailWebView = (WebView) findViewById(R.id.web_view);

detailWebView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);

}

希望这会有所帮助,我花了一些时间才想出解决方法。

Jesus Almaral answered 2020-07-19T17:58:46Z

6 votes

有点晚了,但是我希望这会有所帮助,您可以做的是:

String html = "

%5C%22%22";

mWebView.loadData(html, "text/html", null);

这将使图像与WebView的宽度完全相似,剩下的就是您可以调整WebView本身。

Ankit Bansal answered 2020-07-19T17:59:10Z

4 votes

码:

web = (WebView) findViewById(R.id.at_image_web);

web.getSettings().setBuiltInZoomControls(true);

web.getSettings().setUseWideViewPort(true);

web.getSettings().setJavaScriptEnabled(true);

web.getSettings().setLoadWithOverviewMode(true);

web.loadUrl(linkImage);

hai than hoang answered 2020-07-19T17:59:30Z

1 votes

为我工作:webView = (WebView) findViewById(R.id.webView);

WebSettings webSettings = webView.getSettings();

webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

Jenkyn answered 2020-07-19T17:59:50Z

0 votes

我认为这可以解决您的问题:

WebView web;

web = (WebView) findViewById(R.id.webkit);

web.setWebViewClient(new Callback());

web.getSettings().setJavaScriptEnabled(true);

web.getSettings().setLoadWithOverviewMode(true);

web.getSettings().setUseWideViewPort(true);

web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

web.setScrollbarFadingEnabled(false);

user755278 answered 2020-07-19T18:00:10Z

0 votes

此代码对我有用:

String strData = "

" + "" +

"" + mListItem.get(position).getTitbits();

holder.webViewStatus.loadDataWithBaseURL(null, strData, "text/html", "UTF-8", null);

answered 2020-07-19T18:00:30Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值