如何使用web视图以显示图像 [英] How to use a WebView to display an image

查看:143
本文介绍了如何使用web视图以显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个web视图来显示图像。

但在应用程序的第二/第三次创业的WebView 为空,在真实设备上。

当我注释掉 // wv.getSettings()setJavaScriptEnabled(真); 那么一切都很好,当.http我删除的onload =调整大小(本);的程序是,每次开始图像太

如何纠正。如何使用的JavaScript 并没有让空的WebView

也许我需要保存缓存第一初始化后?

 公共类TestwebViewimageActivity延伸活动{
私人的WebView WV;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    WV =(web视图)findViewById(R.id.webview);
    wv.getSettings()setJavaScriptEnabled(真)。
    wv.setWebViewClient(新HelloWebViewClient());
    wv.setBackgroundColor(00000000);
    。wv.getSettings()setBuiltInZoomControls(假);
    wv.setVerticalScrollBarEnabled(假);
    wv.setHorizo​​ntalScrollBarEnabled(假);
    wv.loadUrl(文件:///android_asset/4.html);
}

私有类HelloWebViewClient扩展WebViewClient {
    @覆盖
    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
        view.loadUrl(URL);
        返回true;
    }
}
}
 

我的.html 肥大展示在完整的窗口形象:

 < HTML>< HEAD>
< /头>
<身体GT;
< IMG SRC =myimage.png的onload =调整(本); />
<脚本类型=文/ JavaScript的>
功能调整大小(图){
    image.style ['高度'] = document.body.clientHeight +'像素';
    image.style ['宽度'] = document.body.clientWidth +'像素';

    image.style ['保证金'] = 0;
    document.body.style ['保证金'] = 0;
}
< / SCRIPT>
< /身体GT;
< / HTML>
 

解决方案

我忍不住想你会使用CSS比JavaScript来调整你的形象会更好。

我在用的大背景大小的一个例子:包含;属性自动缩放的背景,因此在专区内适合,我觉得这是你试图实现与您的JavaScript。

http://jsfiddle.net/4DNsL/

I want to create a WebView to display an image.

But on the second/third start of the application WebView is empty on real devices.

When I comment out //wv.getSettings().setJavaScriptEnabled(true); then all is good and when in .http I am deleting onload="resize(this);" an app is starting with image every time too.

How to correct this. How to use JavaScript and don't to have empty WebView?

Maybe I need to save the cache after first initialisation?

public class TestwebViewimageActivity extends Activity {
private WebView wv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    wv = (WebView) findViewById(R.id.webview);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new HelloWebViewClient());  
    wv.setBackgroundColor(0x00000000);
    wv.getSettings().setBuiltInZoomControls(false);
    wv.setVerticalScrollBarEnabled(false);
    wv.setHorizontalScrollBarEnabled(false);
    wv.loadUrl("file:///android_asset/4.html");
}

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}

my .html mast show image in full window:

<html><head>
</head>
<body>
<img src="myimage.png" onload="resize(this);" />
<script type="text/javascript">
function resize(image) {
    image.style['height'] = document.body.clientHeight + 'px';
    image.style['width'] = document.body.clientWidth + 'px';

    image.style['margin'] = 0;
    document.body.style['margin'] = 0;
}
</script>
</body>
</html>

解决方案

I can't help thinking you'd be better off using CSS than JavaScript to resize your image.

I made an example using the great background-size: contain; property that auto-scales the background so it fits within the div, I think this is what you were trying to achieve with your JavaScript.

http://jsfiddle.net/4DNsL/

这篇关于如何使用web视图以显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆