Android - Webview 縮放圖像以適應最大尺寸 (Android - Webview scale image to fit biggest dimension)


問題描述

Android ‑ Webview 縮放圖像以適應最大尺寸 (Android ‑ Webview scale image to fit biggest dimension)

In my ever so harsh endeavor to make my own apps, i have hit yet another brick wall.

Details

I decided today since the WebView already has build in zoom controls that i would migrate to it from a ImageView...my issue is that I still have active user with API 3 running...so I have to keep that in mind when writing update for this app

What am i trying to do?

Simply load an image from my server and display it fitting to the WebView window which can vary in size depending on the screen.

What have you tried

Try #1 (my personal attempt)

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

This leaves some pics with a lot of empty space on all pics

Try #2 (Found on 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");

This makes the images HUGE...and i mean HUGE

Try #3(Found on 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);

Other than needing and API higher than 3...even without those lines, it make the image again HUGE

Try #4 (again found HERE!!! <‑‑‑ at least i search first)

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

Still needs Froyo (API 7) or higher

_Try #5 (Tried while writing this question after thinking too long)

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

Whats Next?

I have no clue where to go from here...i have been killing myself all day over this one simple thing...please help before i pull my hair out

p.s. I am bald...so the hair this is a joke

‑‑‑‑‑

參考解法

方法 1:

Try #5 Worked!!!

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

(by Guy CothalGuy Cothal)

參考文件

  1. Android ‑ Webview scale image to fit biggest dimension (CC BY‑SA 3.0/4.0)

#scale #webview #image






相關問題

Java縮放像素? (Java scaled pixels?)

Android - Webview 縮放圖像以適應最大尺寸 (Android - Webview scale image to fit biggest dimension)

使用 CGAffineTransform 時限制 UIImageView 的大小 (Limitting UIImageView size while using CGAffineTransform)

Android WallpaperManager 將位圖縮放得太小 (Android WallpaperManager scales Bitmap too small)

如何在沒有像素模糊的情況下調整 html5 畫布的大小 (How to resize html5 canvas without pixel blur)

查看 Spark 中派生的機器學習模型 (view machine learning model derived in Spark)

圖像保持點位置的比例分辨率 (Scale resolution of image keeping point locations)

ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() 方法未命中 (ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() method not being hit)

在 scale_x_continuous 中使用日期與中斷 (Using date in scale_x_continuous with breaks)

如何在不破壞容器邊界半徑的情況下創建與 Safari 兼容的過渡變換比例 (How would I create a Safari-compatible Transition Transform Scale without breaking the container's border-radius)

如何在 Flutter 中相對於另一個小部件定位小部件? (how can I position a widget relative to another in Flutter?)

Android 導入的位圖在 Canvas 上太大 (Android imported Bitmaps too large on Canvas)







留言討論