2010-07-23 84 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); 
} 

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