使用图像填充多个ImageView

时间:2012-06-18 18:19:35

标签: android android-asynctask

我需要从网络上填充3张图片视图。当我在手机上运行并执行我想要的操作时,下面的代码可以正常工作。然而,由于缺乏经验,我不知道这是否是一个做我想要的好方法。如果你看到一个潜在的问题或者有一点建议,我就会被存放。 谢谢参观。

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;

public class spotprice extends Activity
    {

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.spotprice);

                // TODO Auto-generated method stub

                URL[] images = new URL[3];
                try
                    {
                        images[0] = new URL("http://www.weblinks247.com/exrate/24hr-gbp-small.gif");
                        images[1] = new URL("http://www.weblinks247.com/exrate/24hr-euro-small.gif");
                        images[2] = new URL("http://www.weblinks247.com/exrate/24hr-rub-small.gif");

                    } catch (MalformedURLException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                new getImage().execute(images);
            }

        private class getImage extends AsyncTask<URL, Void, Drawable>
            {
                @Override
                protected Drawable doInBackground(URL... params)
                    {
                        // TODO Auto-generated method stub
                        Drawable d = null;
                        try
                            {
                                InputStream is = (InputStream) params[0]
                                        .getContent();
                                d = Drawable.createFromStream(is, "src name");
                            } catch (IOException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        return d;
                    }

                @Override
                protected void onPostExecute(Drawable result)
                    {
                        // TODO Auto-generated method stub

                        ImageView imView = (ImageView) findViewById(R.id.imageView1);
                        ImageView imView2 = (ImageView) findViewById(R.id.imageView2);
                        ImageView imView3 = (ImageView) findViewById(R.id.imageView3);
                        if (imView.getDrawable() == null)
                            {
                                imView.setImageDrawable(result);
                            }else 
                                {

                                }
                        if (imView2.getDrawable() == null)
                            {
                                imView2.setImageDrawable(result);
                            }else
                                {

                                }
                        if (imView3.getDrawable() == null)
                            {
                                imView3.setImageDrawable(result);
                            } else
                                {

                                }
                        super.onPostExecute(result);
                    }

            }

    }

1 个答案:

答案 0 :(得分:0)

这是另一个例子,您可以通过键入图像的网址来查看来自网络的图像。希望这会帮助你。 http://asantoso.wordpress.com/2008/03/07/download-and-view-image-from-the-web/