image - Android - Webview缩放图像以适应最大尺寸

标签 image webview scale

在我制作自己的应用程序的艰苦努力中,我又碰壁了。

详情

我今天决定,因为 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(在 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");

这使得图像变得巨大...我的意思是巨大

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

除了需要 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);

下一步是什么?

我不知道从这里该何去何从......我整天都在为这件简单的事情自杀......在我拔头发之前请帮忙

附注我是秃头...所以头发这是个笑话

最佳答案

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

关于image - Android - Webview缩放图像以适应最大尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641571/

相关文章:

android - 在 webview 中检查输入

html - 缩略图图像未水平对齐

java - 在 Java 中绘制带轮廓的字符串有什么更好的方法吗?

webview - Android Marshmallow 在后台流式传输(缓冲区?)

webview - 如果我设置了 targetSdkVersion 28,AdvancedWebView (delight-im/Android-AdvancedWebView) 将停止显示网页

r - 缩放数据行

ruby-on-rails - Ruby/Rails 图像处理库

javascript - 如何用JavaScript显示图像?

d3.js - NVD3.js (d3.js) 尺度中断

c# - MonoGame - 正确的 SpriteFont 缩放