Display image from URL - sizing and screen orientation problems(从 URL 显示图像 - 大小和屏幕方向问题)
问题描述
我正在尝试显示来自 URL 的图像,该图像可能大于屏幕尺寸.我有它的工作,但我希望它缩放以适应屏幕,当屏幕方向改变时我也遇到问题.图像很小,我希望它也可以将其宽度缩放到屏幕上.(在这两种情况下,我都希望图像用滚动条填充屏幕宽度(如果需要高度).
I am trying to display an image from a URL, which may be larger than the screen dimensions. I have it kind of working, but I would like it to scale to fit the screen, and I also have problems when the screen orientation changes. The image is tiny, and I would like it to scale its width to the screen as well. (In both cases, I would like the image to fill the screen width with scrollbars (if necessary for height).
这是我的 ImageView:
Here is my ImageView:
<ImageView android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true">
</ImageView>
这是加载图像的 java 代码:(为简单起见,删除了一些错误处理代码)
Here is the java code which loads the image: (some error handling code removed for simplicity)
Object content = null;
try{
URL url = new URL("http://farm1.static.flickr.com/150/399390737_7a3d508730_b.jpg");
content = url.getContent();
}
catch(Exception ex)
{
ex.printStackTrace();
}
InputStream is = (InputStream)content;
Drawable image = Drawable.createFromStream(is, "src");
Image01.setImageDrawable(image);
我尝试了 android:scaleType 的不同设置.如果以前有人问过这个问题,我很抱歉.我已经阅读了许多关于该主题的教程,但它们似乎对我不起作用.不确定它是否与加载图像的方式有关.(来自网络而不是本地资源)
I have tried different settings for android:scaleType. I'm sorry if this question has been asked before. I've gone through a number of tutorials on the subject, but they don't seem to work for me. Not sure if it has anything to do with the way the image is loaded. (from the web instead of a local resource)
另一个问题是有时图像甚至无法加载.没有运行时错误,我在 ImageView 中什么也没有.
Another issue is that sometimes the image doesn't even load. There are no runtime errors, I just get nothing in the ImageView.
如果您需要更多信息或说明,请告诉我.
Please let me know if you need more information or clarification.
推荐答案
有时候图片加载不出来"的问题和上下文有关,所以我用这个函数来解决这个问题
the issue about that "sometimes the image doesn't even load" is related to the context so I used this functions to solve that issue
public Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
private Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
所以要用你的图像填充屏幕宽度,你必须有这样的代码
so to fill the screen width with your image you must have a code like this
try{
String url = "http://farm1.static.flickr.com/150/399390737_7a3d508730_b.jpg";
Drawable image =ImageOperations(this,url);
Image01.setImageDrawable(image);
}
catch(Exception ex)
{
ex.printStackTrace();
}
Image01.setMinimumWidth(width);
Image01.setMinimumHeight(height);
Image01.setMaxWidth(width);
Image01.setMaxHeight(height);
更新::如果你加载一个大尺寸的图片,显然你将不得不等待更多的时间,UnknowHostException 可能会导致下载问题.
UPDATE:: if you load a big size image obviously you will have to wait more time, and download problems could be caused for UnknowHostException.
是的,您是对的,您将图像保存在本地,本地访问比下载更快.
yes you are right you will save your image locally, the local access is faster than the download.
为避免旋转更改问题,请在 Manifest.xml 中设置 configChanges="keyboardHidden|orientation" 属性
to avoid problems on rotation change set your configChanges="keyboardHidden|orientation" property inside your Manifest.xml
<activity android:name=".myActivity"
...
android:configChanges="keyboardHidden|orientation" >
...
/>
这篇关于从 URL 显示图像 - 大小和屏幕方向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 URL 显示图像 - 大小和屏幕方向问题
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01