如何在ImageView中更新位图

3
protected Bitmap doInBackground(String...params) {
        // TODO Auto-generated method stub
         try {

              /* Open a new URL and get the InputStream to load data from it. */

              URL aURL = new URL(params[0]);
              URLConnection conn = aURL.openConnection();
              conn.connect();
              InputStream is = conn.getInputStream();

              /* Buffered is always good for a performance plus. */
              BufferedInputStream bis = new BufferedInputStream(is);

              /* Decode url-data to a bitmap. */
              Bitmap bm = BitmapFactory.decodeStream(bis);
              b1=bm;

              bis.close();
              is.close();

          } catch (IOException e) 
          {

              Log.e("DEBUGTAG", "Remote Image Exception", e);

          }

       return null;
   }

// TODO Auto-generated method stub
        super.onPostExecute(result);

        i1.setImageBitmap(b1);
        Animation rotation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
     i1.startAnimation(rotation);
    //System.out.println("imageview is working");
        try {
            System.out.println("thread going to sleep");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        i1.setImageBitmap(b1);
        }

我正在通过位图在ImageView中展示来自互联网的图像,现在我有另一张图像,通过更新URL来更新该位图,但我无法显示该图片,只能显示之前的图片,如果我在循环中运行它,则只显示最后一张图片。有什么建议吗?

1个回答

1

从您的OnCreate或任何您想要从网络初始化代码下载的位置调用此函数

new Thread(new Runnable() {
                public void run() {
                    Drawable drawable = createDrawableFromURL(_mDrawingURL);

                    Message msgURL = new Message();
                    msgURL.arg2 = URL_CONSTANT;
                    msgURL.obj = drawable;
                    _handler_url.sendMessage(msgURL);   
                }
            }).start();
        } catch (JSONException e) {
            e.printStackTrace();
        } 
    }

这种方法将用于从互联网下载图像:

private Drawable createDrawableFromURL(String urlString) {
        Drawable image = null;
        try
        {
            InputStream inputStream = new URL(urlString).openStream();
            image = Drawable.createFromStream(inputStream, null);
            inputStream.close();
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            image = null;
        } catch (IOException e) {
            e.printStackTrace();
            image = null;
        }
        return image;
    }

下载图片后,图像视图将会更新为该图片。

private Handler _handler_url = new Handler(){
        public void dispatchMessage(Message msg) {
            switch (msg.arg2) {
            case URL_CONSTANT:
                Drawable drawable = (Drawable) msg.obj;
                if(drawable!=null)
                    _mImageView.setImageDrawable(drawable);

                break;
            default:
                break;
            }
        };
    };

它类似于您的ASync任务,您也可以在线程上执行此操作。 - Sam-In-TechValens

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接