我需要在安卓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");
发布于 2012-12-02 17:09:53
您需要比较图像的高度和宽度的比率与显示器的高度和宽度的比率。
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';
}
https://stackoverflow.com/questions/13671468
复制相似问题