首页 > 代码库 > 使用AsyncTask异步加载图片

使用AsyncTask异步加载图片

主要看看AsyncTask如何使用:

MainActivity.java

[java] view plaincopyprint?
  1. public class MainActivity extends Activity {  
  2.     private static final String TAG = "MainActivity";  
  3.     private List<String>  datas;  
  4.     private LayoutInflater inflater;  
  5.     private Context context;  
  6.     private MyAdapter adapter;  
  7.     private ListView listview;  
  8.     private List<String> imgUrls;  
  9.     private List<TextView> list = new ArrayList<TextView>();  
  10.     private List<String> sortUrl = new ArrayList<String>();  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  15.         setContentView(R.layout.activity_main);  
  16.         inflater = LayoutInflater.from(this);  
  17.         context = MainActivity.this;  
  18.         listview = (ListView) findViewById(R.id.listview);  
  19.         initData();  
  20.         adapter= new MyAdapter();  
  21.         listview.setAdapter(adapter);  
  22.     }  
  23.   
  24.     private void initData() {  
  25.         imgUrls = new ArrayList<String>();  
  26.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/mpst/public/p2199507156.jpg");  
  27.         imgUrls.add("http://img5.douban.com/lpic/s27397768.jpg");  
  28.         imgUrls.add("http://img3.douban.com/lpic/s27505695.jpg");  
  29.         imgUrls.add("http://img3.douban.com/lpic/s27461340.jpg");  
  30.         imgUrls.add("http://img3.douban.com/lpic/s27451442.jpg");  
  31.         imgUrls.add("http://img5.douban.com/lpic/s27453059.jpg");  
  32.         imgUrls.add("http://img3.douban.com/lpic/s27452925.jpg");  
  33.         imgUrls.add("http://img5.douban.com/lpic/s27452846.jpg");  
  34.         imgUrls.add("http://img3.douban.com/lpic/s27325332.jpg");  
  35.         imgUrls.add("http://img3.douban.com/lpic/s27325315.jpg");  
  36.         imgUrls.add("http://img5.douban.com/lpic/s27389537.jpg");  
  37.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/mpst/public/p2199638985.jpg");  
  38.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/mpst/public/p2199637691.jpg");  
  39.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2195768476.jpg");  
  40.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2180002996.jpg");  
  41.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2182978810.jpg");  
  42.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2164841498.jpg");  
  43.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2161445839.jpg");  
  44.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2183221886.jpg");  
  45.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2164383301.jpg");  
  46.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2199638985.jpg");  
  47.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2199407040.jpg");  
  48.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2176900061.jpg");  
  49.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2180405730.jpg");  
  50.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2197059721.jpg");  
  51.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2196353608.jpg");  
  52.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2187391526.jpg");  
  53.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2185073849.jpg");  
  54.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2196328516.jpg");  
  55.         imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2166850749.jpg");  
  56.         imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2183957412.jpg");  
  57.         imgUrls.add("http://img3.douban.com/spic/s27459164.jpg");  
  58.         imgUrls.add("http://img3.douban.com/spic/s27480483.jpg");  
  59.         imgUrls.add("http://img3.douban.com/spic/s27460951.jpg");  
  60.         imgUrls.add("http://img3.douban.com/spic/s27453772.jpg");  
  61.         imgUrls.add("http://img3.douban.com/spic/s27410904.jpg");  
  62.         imgUrls.add("http://img5.douban.com/spic/s27466008.jpg");  
  63.         imgUrls.add("http://img3.douban.com/spic/s3259484.jpg");  
  64.         imgUrls.add("http://img3.douban.com/spic/s3081692.jpg");  
  65.         imgUrls.add("http://img3.douban.com/spic/s1400630.jpg");  
  66.         imgUrls.add("http://img5.douban.com/spic/s4715377.jpg");  
  67.         imgUrls.add("http://img3.douban.com/spic/s6201192.jpg");  
  68.         imgUrls.add("http://img5.douban.com/spic/s2650727.jpg");  
  69.         imgUrls.add("http://img3.douban.com/spic/s3636124.jpg");  
  70.         imgUrls.add("http://img5.douban.com/spic/s3595798.jpg");  
  71.         imgUrls.add("http://img3.douban.com/spic/s27078194.jpg");  
  72.         imgUrls.add("http://img3.douban.com/view/event_poster/large/public/2a35a85a873e8e7.jpg");  
  73.         imgUrls.add("http://img3.douban.com/view/event_poster/large/public/ce66e0569742fd7.jpg");  
  74.         imgUrls.add("http://img3.douban.com/view/event_poster/large/public/4c44752e83900d9.jpg");  
  75.         imgUrls.add("http://img3.douban.com/view/event_poster/large/public/d22f7b9e5c8c92c.jpg");  
  76.     } 

  77.    private class MyAdapter extends BaseAdapter{  
  78.         @Override  
  79.         public int getCount() {  
  80.             return imgUrls.size();  
  81.         }  
  82.         @Override  
  83.         public Object getItem(int position) {  
  84.             return imgUrls.get(position);  
  85.         }  
  86.         @Override  
  87.         public long getItemId(int position) {  
  88.             return position;  
  89.         }  
  90.         @Override  
  91.         public View getView(int position, View convertView, ViewGroup arg2) {  
  92.             ViewHolder viewHolder = null;  
  93.             if(convertView == null){  
  94.                 viewHolder = new ViewHolder();  
  95.                 convertView = inflater.inflate(R.layout.item, null);  
  96.                 viewHolder.image = (ImageView) convertView.findViewById(R.id.img);  
  97.                 convertView.setTag(viewHolder);  
  98.             }else{  
  99.                 viewHolder = (ViewHolder) convertView.getTag();  
  100.             }  
  101.             ImageTask imageTask = new ImageTask(viewHolder.image);  
  102.             iamgeTask.execute(imgUrls.get(position));  
  103.             return convertView;  
  104.         }  
  105.         class ViewHolder{  
  106.             ImageView image;  
  107.         }  
  108.     }  
  109. }   
加载图片的类:

ImageTask.java

[java] view plaincopyprint?
  1. public class ImageTask extends AsyncTask<String, Void, Bitmap> {  
  2.     private ImageView iv;  
  3.     public ImageTask(ImageView iv){  
  4.         this.iv = iv;  
  5.     }  
  6.     @Override  
  7.     protected Bitmap doInBackground(String... param) {  
  8.         String imgUrl = param[0];  
  9.         try {  
  10.             URL url = new URL(imgUrl);  
  11.             try {  
  12.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  13.                 InputStream in = conn.getInputStream();  
  14.                 Bitmap bitmap = BitmapFactory.decodeStream(in);  
  15.                 if(bitmap!=null){  
  16.                     return bitmap;  
  17.                 }  
  18.             } catch (IOException e) {  
  19.                 // TODO Auto-generated catch block  
  20.                 e.printStackTrace();  
  21.             }  
  22.         } catch (MalformedURLException e) {  
  23.             // TODO Auto-generated catch block  
  24.             e.printStackTrace();  
  25.         }  
  26.         return null;  
  27.     }  
  28.   
  29.     @Override  
  30.     protected void onPostExecute(Bitmap result) {  
  31.         super.onPostExecute(result);  
  32.         if(result != null){  
  33.             iv.setImageBitmap(result);  
  34.         }  
  35.     }  
  36.   
  37.     @Override  
  38.     protected void onPreExecute() {  
  39.         super.onPreExecute();  
  40.     }  
  41. }  

使用AsyncTask异步加载图片