Android WebView 还是 ImageView? [英] Android WebView or ImageView?

查看:36
本文介绍了Android WebView 还是 ImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 URL 下载和显示图像的应用程序.使用

I have an App that downloads and displays images from URL's. This works fine using

URLConnection conn = urls[i].openConnection();
conn.connect();
if(conn.getContentLength() > 0)
is = conn.getInputStream();
bis = new BufferedInputStream(is);
Bitmap temp = BitmapFactory.decodeStream(bis);

我注意到 WebView 的下载和显示图像比上面的代码快得多,所以我想我会尝试.

I noticed that WebView's download and display images much faster then the above code so I thought i would try.

String url = "http:\image.jpg"
int mWidth = getSystemService(Context.WINDOW_SERVICE).getDefaultDisplay().getWidth()
webView.loadData("<html><body> <img src="" + url + "" width="" + mWidth + "px" height="auto"></body></html>", "text/html", null);

虽然这样可以更快地获取图像,但它不会将图像缩放到屏幕大小,但大图像总是超出 WebView 的宽度,需要滚动才能正确查看.

Although this gets the image much faster it does not scale the image to the size of the screen, the large images always go beyond the WebView's width and require scrolling to view properly.

有谁知道我可以如何解决问题以在 WebView 中正确显示图像,或者首先如何加快图像的下载速度?

Does anyone know how i can fix the issue to display the image properly in the WebView or how to speed up the download of the image in the first place?

我希望加快下载速度,但如果我可以正确缩放图像,我很乐意使用 WebView.

I would prefer to have the download speeded up, but i am happy to use the WebView if i can scale the image correctly.

推荐答案

通过 CSS 设置图片元素的宽度为 100%,高度应根据图片的宽高比缩放.

Set the image elements width to 100% via CSS, height should scale according to the aspect/ratio of the image.

这篇关于Android WebView 还是 ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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