从 URL 显示图像 - 大小和屏幕方向问题

Display image from URL - sizing and screen orientation problems(从 URL 显示图像 - 大小和屏幕方向问题)

本文介绍了从 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 显示图像 - 大小和屏幕方向问题

基础教程推荐