安卓 - Webview自适应缩放图片到最大尺寸

3

在我努力开发自己的应用程序时,又遇到了一个难题。

详情

今天我决定从ImageView迁移到WebView,因为WebView已经内置了缩放控件。我的问题是,我仍然有正在运行API3的活跃用户,所以在编写此应用程序的更新时必须牢记这一点。

我想做什么?

简单地从我的服务器加载图像并将其显示适合于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(来自StackOverFlow)

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

这会使图片变得非常大...我指的是非常大

第三种方法(从StackOverFlow上找到)

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

除了需要高于3的API之外...即使没有那些行,它仍然会让图像变得非常大

尝试 #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);

接下来呢?

我不知道接下来该怎么做...我已经为这个简单的事情烦了一整天了...请在我把头发都拔光之前帮我一下。

附言:我已经秃了...所以关于头发的那句话只是个玩笑

1个回答

3
尝试第五次成功了!!!
        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);

谢谢。这帮助我解决了一个非常相似的问题 :)) - Felipe Lima

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接