2012-07-25 100 views
2

在我製作自己的應用程序的過程中,我再次遇到另一堵磚牆。Android - Webview縮放圖像以適應最大尺寸

詳細

我今天決定自WebView已經有了建立,我會遷移到它從一個ImageView縮放控制......我的問題是,我仍然有API 3運行活躍用戶...所以我必須記住這個應用程序的更新時

我想要做什麼?

只需從我的服務器加載圖像,並顯示它嵌合到WebView窗口可以在大小取決於在屏幕上會有所不同。

你嘗試過什麼

嘗試#1(我個人的嘗試)

//iW & iH are the height and width of the image passed from the server 
//Calc the scale needed to fit width 
int scW = (int) ((iW/(float)wView.getMeasuredHeight()) * 100); 
//Calc the scale needed to fit height 
int scH = (int) ((iH/(float)wView.getMeasuredHeight()) * 100); 
//fit the bigger of the 2 
if (scW < scH) 
{ 
    wView.setInitialScale(scW); 
} 
else 
{ 
    wView.setInitialScale(scH); 
} 
String pUrl = "http://app.guycothal.com/Funnys/2012724133312929.png"; 
wView.loadUrl(pUrl); 

這讓一些圖片上的所有圖片了很多空的空間

嘗試#2(Foun在計算器上d)

String pUrl = "http://app.guycothal.com/Funnys/2012724133312929.png"; 
String html="<html><body><img src=\"" + pUrl + 
    "\" width=\"10%37\" height=\"10%37\"/></body></html>"; 
wView.loadData(html, "text/html", "utf-8"); 

這使得圖像巨大的......我的意思是巨大

嘗試#3(發現在計算器上)

wView.setWebViewClient(null); 
wView.getSettings().setJavaScriptEnabled(true); 
wView.getSettings().setLoadWithOverviewMode(true);//NEeds api 7 
wView.getSettings().setUseWideViewPort(true); 
wView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
wView.getSettings().setBuiltInZoomControls(true); 
wView.getSettings().setSupportZoom(true); 
wView.getSettings().setUseWideViewPort(true); 
wView.setScrollbarFadingEnabled(false);//Needs api 5 
String pUrl = "http://app.guycothal.com/Funnys/2012724133312929.png"; 
wView.loadUrl(pUrl); 

除需要和API高於3 ...甚至沒有那些線,它再次使圖像巨大

嘗試#4(再次發現這裏! < ---至少我先搜索)

wView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR); 
String pUrl = "http://app.guycothal.com/Funnys/2012724133312929.png"; 
wView.loadUrl(pUrl); 

尚需升級Froyo(API 7)或更高

_ 嘗試#5(一邊想着太長)

後寫這個問題,嘗試
//iW & iH are the height and width of the image passed from the server 
int iMW = imgView.getMeasuredWidth(); 
int iMH = imgView.getMeasuredHeight(); 
int scW = (int) ((iW/(float)iMW) * 100); 
int scH = (int) ((iH/(float)iMH) * 100); 
if (iMW < iW && iMH < iH) 
{ 
    if (scW < scH) 
    { 
     wView.setInitialScale(scW); 
    } 
    else 
    { 
     wView.setInitialScale(scH); 
    } 
} 
if (iMW < iW && iMH > iH) 
{ 
    wView.setInitialScale(scH); 
} 
if (iMW > iW && iMH < iH) 
{ 
    wView.setInitialScale(scW); 
} 
if (iMW < iW && iMH < iH) 
{ 
    if (scW > scH) 
    { 
     wView.setInitialScale(scW); 
    } 
    else 
    { 
     wView.setInitialScale(scH); 
    } 
} 
wView.loadUrl(pUrl); 

接下來是什麼?

我不知道該從哪裏下去......我一直在爲這一件簡單的事情而自殺......請在我拉出頭髮前幫忙

p.s.我是禿頭...所以這頭髮這是一個笑話

回答

3

嘗試#5工作!

 int iMW = imgView.getMeasuredWidth(); 
     int iMH = imgView.getMeasuredHeight(); 
     //Had the imw and iw backward...same for height 
     int scW = (int) ((iMW/(float)iW) * 100); 
     int scH = (int) ((iMH/(float)iH) * 100); 
     int fScale = 0; 
     if (iMW < iW && iMH < iH) 
     { 
      if (scW < scH) 
      { 
       fScale = scW; 
      } 
      else 
      { 
       fScale = scH; 
      } 
     } 
     if (iMW < iW && iMH > iH) 
     { 
      fScale = scW; 
     } 
     if (iMW > iW && iMH < iH) 
     { 
      fScale = scH; 
     } 
     //had signs backwards...DUH 
     if (iMW > iW && iMH > iH) 
     { 
      if (scW < scH) 
      { 
       fScale = scW; 
      } 
      else 
      { 
       fScale = scH; 
      } 
     } 
     wView.setInitialScale(fScale); 
     wView.loadUrl(pUrl); 
+0

謝謝。這幫助我解決了一個非常類似的問題:)) – 2013-02-01 22:32:41