2012-05-01 114 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

請格式化您的代碼 –