Android WebView or ImageView?(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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android WebView 还是 ImageView?
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01