1

我需要在 Android 的 SplashScreen 上显示动画。我想根据屏幕的高度宽度调整它的大小。我可以成功地使用 webView 和 javascript 来适应屏幕。但我无法调整它的大小:)

我写我的代码,也许有人会需要它..

这是 index.html 中的 html 代码(我可以用 Java 编写它,但我不能显示使用的动画 gif)

<html>
<head>
<title></title>
</head>
<body style="padding: 0; margin: 0">
<script type="text/javascript">

function resize(image)
{
    var differenceHeight = document.body.clientHeight - image.clientHeight;
    var differenceWidth  = document.body.clientWidth  - image.clientWidth;
    if (differenceHeight < 0) differenceHeight = differenceHeight * -1;
    if (differenceWidth  < 0) differenceWidth  = differenceWidth * -1;

    if (differenceHeight > differenceWidth)
    {
        image.style['height'] = document.body.clientHeight + 'px';
    }
    else
    {
        image.style['width'] = document.body.clientWidth + 'px';
    }

    // Optional: remove margins or compensate for offset.
    image.style['margin'] = 0;
    document.body.style['margin'] = 0;
}

</script>
<img src="file:///android_asset/html/screen1.gif"
     onload="resize(this);"/>
</body>
</html>

这是我的 WebView:

    WebView webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setVerticalScrollBarEnabled(false);
    webView.loadUrl("file:///android_asset/html/index.html");
4

1 回答 1

0

您需要比较图像的高宽比与显示器的高宽比。

var imageRatio = image.clientHeight / image.clientWidth;
var clientRatio = document.body.clientHeight / document.body.clientWidth;

if (imageRatio > clientRatio)
{
    image.style['height'] = document.body.clientHeight + 'px';
}
else
{
    image.style['width'] = document.body.clientWidth + 'px';
}
于 2012-12-02T17:09:53.060 回答