2010-07-23 83 views
9

在我的應用程序中,我顯示圖片感謝webView。因此,對於每個圖像,我創建了使用webView加載的小型HTML代碼。所以我的html代碼基本上包含在一個img標籤中(帶有我的圖片的路徑)。WebView中的圖像適合屏幕

我的照片有不同的尺寸,這就是爲什麼我想設置我的webView縮放以適應圖片寬度到webView寬度。所以用戶不需要放大或縮小就可以看到整個圖像。

有沒有辦法實現它?

謝謝。

+0

我面臨同樣的問題。你解決了嗎? – JochenJung 2011-01-15 14:51:10

+1

我很抱歉,但我還沒有解決它。在這裏有解決方案:http://developer.android.com/guide/webapps/targeting.html。他們說我在我的HTML代碼中添加了標籤「width = device-width」,但它不起作用: (。我還在尋找解決方案 – Trox 2011-01-29 20:37:33

+0

讓我感到難過,你到現在爲止解決了這個問題嗎? – Yeung 2014-09-04 09:07:21

回答

0

是否有一個原因,你正在使用WebView來完成這個?使用ImageView,您可以設置scaleType以使圖像適合ImageView的大小。

+8

好吧,內置縮放和水平/垂直滾動的原因。 – Trox 2010-07-23 22:41:38

+0

或帶有Imageview的對話框?它的瘋狂的權利!?誰來做。 – JoxTraex 2012-01-21 13:58:24

3

這爲我做的伎倆:

webView.setInitialScale(30); 
WebSettings webSettings = webView.getSettings(); 
webSettings.setUseWideViewPort(true); 
+0

謝謝你的回答。如果我使用這個,現在我的大圖片(大約1000px * 2000px)很好居中,但我的小圖片(寬度爲320px)變得非常小,我必須放大才能看到它們。 – Trox 2011-02-04 16:40:39

1

您可以使用一個小的jQuery在網頁中實現它。

喜歡的東西:

$(document).ready(function(){ 
    var windowWidth = $(window).width()+"px"; 
    $("#imagID").width(windowWidth); 
}); 
4

如果您creaing的HTML代碼(你說你是),你可以欺騙:

在html代碼:

img src="xxx" width="100% > 
+0

多麼美麗和工作的黑客;-) – 2012-02-28 19:32:24

1

我發現了一種營養。

使用此計算。

設備寬度*(152 /密度)

檢索設備寬度和密度使用displayMetrics.widthPixels和displayMetrics.densityDpi

組作爲寬度img標記計算

值152是最初是160,但當160使用時,它看起來像圖像比屏幕大。

結果是,圖像最初適合屏幕和,放大工作正常。

1

是不是有一個原因,你不只是使用一些JavaScript將圖像傳遞到Android應用程序,並調出與該對話框中的圖像的自定義對話框,以便它根據內容進行縮放。

就我個人而言,我認爲這種解決方案更適合您的方法。

2

什麼工作對我來說是這樣的:我讀的是,爲了適應你應該添加到您的HTML或XML領域內的屏幕寬度:

width="100%" 

所以我所做的是,代替縮放和縮放圖像,我得到了XML,把它放在一個StringBuilder中,找到了src =「https://blablabla.com/image」。PNG」,即字段內和剛剛之前的‘SRC’子我插入‘寬度=’100%‘’,則y設置我與StringBuilder的web視圖,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); 
} 

希望這幫助,花了我幾個小時才弄清楚如何解決這個問題。