2012-05-01 115 views
28

我有什么:我正在从URL加载图像。我简单地做(WebView).loadUrl(imageurl, extraheaders)Android WebView,缩放图像以适合屏幕

我能得到什么:图片没有在网页视图的整个宽度显示,它有空白各地(比如,如果你在你的桌面浏览器中打开一个小iamge)

什么我尝试过:将LayoutAlgorithm设置为SINGLE_COLUMN。完美的作品,但缩放不起作用。 (我有它在WebView.getSettings()不要启用知道为什么没有任何空白空间设置setUseWideViewPort(true);负荷的形象,但它是在完全放大

+0

它最好不要使用LayoutAlgortihm.SINGLE_COLUMN,因为它已经过时,现在已经过时。 –

回答

29

我有同样的问题,这样做的工作只是罚款:

Display display = getWindowManager().getDefaultDisplay(); 
int width=display.getWidth(); 

String data="<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>"; 
data=data+"<body><center><img width=\""+width+"\" src=\""+url+"\" /></center></body></html>"; 
webView.loadData(data, "text/html", null); 

编辑: 托尼以下建议另一种解决方案:

WebView content = (WebView) findViewById(R.id.webView1); 
content.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + post.getContent(), "text/html", "UTF-8", null); 
+0

谢谢,是否有任何方式发送额外的头文件(例如referer),而使用这种结构? – artouiros

+2

请参阅下面的托尼答案,这是跨平台和设备工作的那个 – 2cupsOfTech

29

你应该缩放web视图以适应屏幕:。

WebView data = (WebView) getViewById(R.id.webview1); 
data.getSettings().setLoadWithOverviewMode(true); 
data.getSettings().setUseWideViewPort(true); 
+4

如果图片尺寸较大,那么屏幕不会使图像适合屏幕,但屏幕适合图像(它看起来像某人缩小以查看整个内容) – supermus

+1

@supermus当启用LoadWithOverviewMode和UseWideViewPort时,图像*将*适合屏幕。我为API级别16开发,您的声明对于此版本不是真实的。我赞成这个答案。 –

+0

不完全相同的问题,但这工作对我来说,我用我加载到web视图的HTML文件中的图像。 。 webView.getSettings()setLoadWithOverviewMode(真); webView.getSettings()。setUseWideViewPort(true); webView.getSettings()。setJavaScriptEnabled(true); webView.getSettings()。setSaveFormData(true); webView.getSettings()。setBuiltInZoomControls(true); webView.loadUrl(“file:///android_asset/your_file.html”); – magorich

0

我认为这将解决您的问题: -

 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); 
+2

这不是滚动条问题,我的空白空间就像屏幕的50%。还是一样。这就像图像比屏幕尺寸缩小得多。 – artouiros

4

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

String html = "<html><body><img src=\"" + URL + "\" width=\"100%\" height=\"100%\"\"/></body></html>"; 
mWebView.loadData(html, "text/html", null); 

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

+1

这可能有点晚,但使用此方法缩放cotrols不起作用。但它很酷,很迷人:) –

65

你也可以做这样的事情。

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

<style>img{display: inline; height: auto; max-width: 100%;}</style> 

要快速做到WebView中的数据我做到了这一点。

WebView content = (WebView) findViewById(R.id.webView1); 
content.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + post.getContent(), "text/html", "UTF-8", null); 

这几乎是像什么bansal21ankit说,而是它会在你的HTML每张图片上工作,而无需额外的工作。


编辑(澄清帖子的内容):

你可以有任何text/html值,而不是post.getContent()从例子。

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

+5

这应该是最好的答案!谢谢 !玛莎真主! –

+2

我觉得这是非常好的想法! – zIronManBox

+1

确实是一个救星..! – kgandroid

1

这对我的工作: webView = (WebView) findViewById(R.id.webView); WebSettings webSettings = webView.getSettings(); webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

4

什么工作对我来说是这样的:我读的是,为了适应屏幕宽度,你应该添加到您的HTML或XML领域内:

width="100%" 

所以我做的是,而不是缩放和缩放图像,我得到的XML,把它放在一个StringBuilder,找到src =“https://blablabla.com/image.png”是在字段并在“src”子字符串之前插入“width =”100%“”,然后y使用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); 
} 

希望这会有所帮助,我花了几个小时才弄清楚如何解决这个问题。

3

代码:

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); 
0

此代码的工作对我来说:

String strData = "<head>" + "<style>" + ".smal-video-pnl,.smal-video-thumb,.detail-hldr{margin-left: 0px;margin-right:0px;}" + "</style>" + 
      "</head>" + mListItem.get(position).getTitbits(); 
holder.webViewStatus.loadDataWithBaseURL(null, strData, "text/html", "UTF-8", null); 
+2

请格式化您的代码 –