private ExecutorService executorService = Executors.newFixedThreadPool(5);
  
         // 引入线程池来管理多线程
        private void loadImage3(final String url, final int id) {
                 executorService.submit(new Runnable() {
                         public void run() {
                                 try {
                                       final Drawable drawable = Drawable.createFromStream(
                                                        new URL(url).openStream(), "image.png");
                                        // 模拟网络延时
                                        SystemClock.sleep(2000);
                                        handler.post(new Runnable() {
                                                 public void run() {
                                                        ((ImageView) MainActivity.this.findViewById(id))
                                                                        .setImageDrawable(drawable);
                                                }
                                        });
                                } catch (Exception e) {
                                        throw new RuntimeException(e);
                                }
                         }
                });
         }

 

posted on 2014-04-14 11:38  青年程序猿  阅读(237)  评论(0编辑  收藏  举报