首页 > 代码库 > ListView异步加载网络图片

ListView异步加载网络图片

1.在新建的项目中加入如下权限:

<uses-permission android:name="android.permission.INTERNET"/>

2.MainActivity.java

import java.util.LinkedList;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.widget.ListView;import com.example.loadimages.R;import com.lxw.adapter.ImageAdapter;import com.lxw.bean.Picture;public class MainActivity extends Activity {    private ListView listview;    private LinkedList<Picture> data;    private ImageAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        loadData();        listview = (ListView) findViewById(R.id.listview);        adapter = new ImageAdapter(data, getLayoutInflater(), this);        listview.setAdapter(adapter);    }    public void loadData() {        if (data =http://www.mamicode.com/= null)            data = new LinkedList<Picture>();        for (int i = 0; i < URLS.length; i++) {            data.add(new Picture(URLS[i]));        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    private static final String[] URLS = {            "http://img5.imgtn.bdimg.com/it/u=1446812229,652623722&fm=21&gp=0.jpg",            "http://img1.imgtn.bdimg.com/it/u=4011018736,2992079449&fm=21&gp=0.jpg",            "http://img3.imgtn.bdimg.com/it/u=613517753,3662840626&fm=21&gp=0.jpg",            "http://img4.duitang.com/uploads/item/201306/18/20130618235128_HyJNB.thumb.600_0.jpeg",            "http://img4.imgtn.bdimg.com/it/u=572663234,2967721388&fm=21&gp=0.jpg",            "http://img4.imgtn.bdimg.com/it/u=2456284265,2038282593&fm=21&gp=0.jpg",            "http://img3.imgtn.bdimg.com/it/u=1628117601,1249996752&fm=21&gp=0.jpg",            "http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg",              "http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg",              "http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg",              "http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg",              "http://lh3.ggpht.com/_lLj6go_T1CQ/TCD8PW09KBI/AAAAAAAAQdc/AqmOJ7eg5ig/s144-c/Juvenile%20Gannet%20despute.jpg",              "http://lh6.ggpht.com/_ZN5zQnkI67I/TCFFZaJHDnI/AAAAAAAABVk/YoUbDQHJRdo/s144-c/P9250508.JPG",              "http://lh4.ggpht.com/_XjNwVI0kmW8/TCOwNtzGheI/AAAAAAAAC84/SxFJhG7Scgo/s144-c/0014.jpg",              "http://lh6.ggpht.com/_lnDTHoDrJ_Y/TBvKsJ9qHtI/AAAAAAAAG6g/Zll2zGvrm9c/s144-c/000007.JPG",              "http://lh6.ggpht.com/_qvCl2efjxy0/TCIVI-TkuGI/AAAAAAAAOUY/vbk9MURsv48/s144-c/DSC_0844.JPG",              "http://lh4.ggpht.com/_4f1e_yo-zMQ/TCe5h9yN-TI/AAAAAAAAXqs/8X2fIjtKjmw/s144-c/IMG_1786.JPG",              "http://lh6.ggpht.com/_iFt5VZDjxkY/TB9rQyWnJ4I/AAAAAAAADpU/lP2iStizJz0/s144-c/DSCF1014.JPG",              "http://lh5.ggpht.com/_hepKlJWopDg/TB-_WXikaYI/AAAAAAAAElI/715k4NvBM4w/s144-c/IMG_0075.JPG",              "http://lh6.ggpht.com/_OfRSx6nn68g/TCzsQic_z3I/AAAAAAABOOI/5G4Kwzb2qhk/s144-c/EASTER%20ISLAND_Hanga%20Roa_31.5.08_46.JPG",              "http://lh6.ggpht.com/_ZGv_0FWPbTE/TB-_GLhqYBI/AAAAAAABVxs/cVEvQzt0ke4/s144-c/IMG_1288_hf.jpg",              "http://lh6.ggpht.com/_a29lGRJwo0E/TBqOK_tUKmI/AAAAAAAAVbw/UloKpjsKP3c/s144-c/31012332.jpg",              "http://lh3.ggpht.com/_55Lla4_ARA4/TB6xbyxxJ9I/AAAAAAABTWo/GKe24SwECns/s144-c/Bluebird%20049.JPG",              "http://lh3.ggpht.com/_iVnqmIBYi4Y/TCaOH6rRl1I/AAAAAAAA1qg/qeMerYQ6DYo/s144-c/Kwiat_100626_0016.jpg",              "http://lh6.ggpht.com/_QFsB_q7HFlo/TCItd_2oBkI/AAAAAAAAFsk/4lgJWweJ5N8/s144-c/3705226938_d6d66d6068_o.jpg",              "http://lh5.ggpht.com/_JTI0xxNrKFA/TBsKQ9uOGNI/AAAAAAAChQg/z8Exh32VVTA/s144-c/CRW_0015-composite.jpg",              "http://lh6.ggpht.com/_loGyjar4MMI/S-InVNkTR_I/AAAAAAAADJY/Fb5ifFNGD70/s144-c/Moving%20Rock.jpg",              "http://lh4.ggpht.com/_L7i4Tra_XRY/TBtxjScXLqI/AAAAAAAAE5o/ue15HuP8eWw/s144-c/opera%20house%20II.jpg",              "http://lh3.ggpht.com/_rfAz5DWHZYs/S9cstBTv1iI/AAAAAAAAeYA/EyZPUeLMQ98/s144-c/DSC_6425.jpg",              "http://lh6.ggpht.com/_iGI-XCxGLew/S-iYQWBEG-I/AAAAAAAACB8/JuFti4elptE/s144-c/norvig-polar-bear.jpg",              "http://lh3.ggpht.com/_M3slUPpIgmk/SlbnavqG1cI/AAAAAAAACvo/z6-CnXGma7E/s144-c/mf_003.jpg",              "http://lh4.ggpht.com/_loGyjar4MMI/S-InQvd_3hI/AAAAAAAADIw/dHvCFWfyHxQ/s144-c/Rainbokeh.jpg",              "http://lh4.ggpht.com/_yy6KdedPYp4/SB5rpK3Zv0I/AAAAAAAAOM8/mokl_yo2c9E/s144-c/Point%20Reyes%20road%20.jpg",              "http://lh5.ggpht.com/_6_dLVKawGJA/SMwq86HlAqI/AAAAAAAAG5U/q1gDNkmE5hI/s144-c/mobius-glow.jpg",              "http://lh3.ggpht.com/_QFsB_q7HFlo/TCItc19Jw3I/AAAAAAAAFs4/nPfiz5VGENk/s144-c/4551649039_852be0a952_o.jpg",              "http://lh6.ggpht.com/_TQY-Nm7P7Jc/TBpjA0ks2MI/AAAAAAAABcI/J6ViH98_poM/s144-c/IMG_6517.jpg",              "http://lh3.ggpht.com/_rfAz5DWHZYs/S9cLAeKuueI/AAAAAAAAeYU/E19G8DOlJRo/s144-c/DSC_4397_8_9_tonemapped2.jpg",              "http://lh4.ggpht.com/_TQY-Nm7P7Jc/TBpi6rKfFII/AAAAAAAABbg/79FOc0Dbq0c/s144-c/david_lee_sakura.jpg",              "http://lh3.ggpht.com/_TQY-Nm7P7Jc/TBpi8EJ4eDI/AAAAAAAABb0/AZ8Cw1GCaIs/s144-c/Hokkaido%20Swans.jpg",              "http://lh3.ggpht.com/_1aZMSFkxSJI/TCIjB6od89I/AAAAAAAACHM/CLWrkH0ziII/s144-c/079.jpg",              "http://lh5.ggpht.com/_loGyjar4MMI/S-InWuHkR9I/AAAAAAAADJE/wD-XdmF7yUQ/s144-c/Colorado%20River%20Sunset.jpg",              "http://lh3.ggpht.com/_0YSlK3HfZDQ/TCExCG1Zc3I/AAAAAAAAX1w/9oCH47V6uIQ/s144-c/3138923889_a7fa89cf94_o.jpg",              "http://lh6.ggpht.com/_K29ox9DWiaM/TAXe4Fi0xTI/AAAAAAAAVIY/zZA2Qqt2HG0/s144-c/IMG_7100.JPG",              "http://lh6.ggpht.com/_0YSlK3HfZDQ/TCEx16nJqpI/AAAAAAAAX1c/R5Vkzb8l7yo/s144-c/4235400281_34d87a1e0a_o.jpg",              "http://lh4.ggpht.com/_8zSk3OGcpP4/TBsOVXXnkTI/AAAAAAAAAEo/0AwEmuqvboo/s144-c/yosemite_forrest.jpg",              "http://lh4.ggpht.com/_6_dLVKawGJA/SLZToqXXVrI/AAAAAAAAG5k/7fPSz_ldN9w/s144-c/coastal-1.jpg",              "http://lh4.ggpht.com/_WW8gsdKXVXI/TBpVr9i6BxI/AAAAAAABhNg/KC8aAJ0wVyk/s144-c/IMG_6233_1_2-2.jpg",              "http://lh3.ggpht.com/_loGyjar4MMI/S-InS0tJJSI/AAAAAAAADHU/E8GQJ_qII58/s144-c/Windmills.jpg",              "http://lh4.ggpht.com/_loGyjar4MMI/S-InbXaME3I/AAAAAAAADHo/4gNYkbxemFM/s144-c/Frantic.jpg",              "http://lh5.ggpht.com/_loGyjar4MMI/S-InKAviXzI/AAAAAAAADHA/NkyP5Gge8eQ/s144-c/Rice%20Fields.jpg",              "http://lh3.ggpht.com/_loGyjar4MMI/S-InZA8YsZI/AAAAAAAADH8/csssVxalPcc/s144-c/Seahorse.jpg",              "http://lh3.ggpht.com/_syQa1hJRWGY/TBwkCHcq6aI/AAAAAAABBEg/R5KU1WWq59E/s144-c/Antelope.JPG",              "http://lh5.ggpht.com/_MoEPoevCLZc/S9fHzNgdKDI/AAAAAAAADwE/UAno6j5StAs/s144-c/c84_7083.jpg",              "http://lh4.ggpht.com/_DJGvVWd7IEc/TBpRsGjdAyI/AAAAAAAAFNw/rdvyRDgUD8A/s144-c/Free.jpg",              "http://lh6.ggpht.com/_iO97DXC99NY/TBwq3_kmp9I/AAAAAAABcz0/apq1ffo_MZo/s144-c/IMG_0682_cp.jpg",              "http://lh4.ggpht.com/_7V85eCJY_fg/TBpXudG4_PI/AAAAAAAAPEE/8cHJ7G84TkM/s144-c/20100530_120257_0273-Edit-2.jpg"};}

 

3.ImageAdapter.java

package com.lxw.adapter;import java.util.LinkedList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import com.example.loadimages.R;import com.lxw.bean.Picture;import com.lxw.manager.BitmapManager;public class ImageAdapter extends BaseAdapter {    LinkedList<Picture> data = http://www.mamicode.com/new LinkedList<Picture>();    LayoutInflater inflator;    Context context;    public ImageAdapter(LinkedList<Picture> data, LayoutInflater inflator,            Context context) {        this.data =http://www.mamicode.com/ data;        this.inflator = inflator;        this.context = context;    }    @Override    public int getCount() {        return data.size();    }    @Override    public Object getItem(int position) {        return data.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder;        if (convertView == null) {            convertView = inflator.inflate(R.layout.row, null);            ImageView imageView = (ImageView) convertView                    .findViewById(R.id.image);            holder = new ViewHolder();            holder.imageView = imageView;            convertView.setTag(holder);        } else {            holder = (ViewHolder) convertView.getTag();        }        Picture item = data.get(position);        holder.imageView.setImageDrawable(this.context.getResources()                .getDrawable(R.drawable.ic_launcher));        BitmapManager.INSTANCE.loadBitmap(item.getImageUrl(), holder.imageView,                300, 300);        return convertView;    }    class ViewHolder {        ImageView imageView;    }}

 

 

4.BitmapManager.java

package com.lxw.manager;import java.io.IOException;import java.io.InputStream;import java.lang.ref.SoftReference;import java.net.MalformedURLException;import java.net.URL;import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.WeakHashMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import android.annotation.SuppressLint;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Handler;import android.os.Message;import android.util.Log;import android.widget.ImageView;public enum BitmapManager {    INSTANCE;    private final Map<String, SoftReference<Bitmap>> cache;    private final ExecutorService pool;    private Map<ImageView, String> imageViews = Collections            .synchronizedMap(new WeakHashMap<ImageView, String>());    private Bitmap placeholder;    BitmapManager() {        cache = new HashMap<String, SoftReference<Bitmap>>();        pool = Executors.newFixedThreadPool(5);    }    public void setPlaceholder(Bitmap bmp) {        placeholder = bmp;    }    public Bitmap getBitmapFromCache(String url) {        if (cache.containsKey(url)) {            return cache.get(url).get();        }        return null;    }    @SuppressLint("HandlerLeak")    public void queueJob(final String url, final ImageView imageView,            final int width, final int height) {        /* Create handler in UI thread. */        final Handler handler = new Handler() {            @Override            public void handleMessage(Message msg) {                String tag = imageViews.get(imageView);                if (tag != null && tag.equals(url)) {                    if (msg.obj != null) {                        imageView.setImageBitmap((Bitmap) msg.obj);                    } else {                        imageView.setImageBitmap(placeholder);                        Log.d(null, "fail " + url);                    }                }            }        };        pool.submit(new Runnable() {            @Override            public void run() {                final Bitmap bmp = downloadBitmap(url, width, height);                Message message = Message.obtain();                message.obj = bmp;                Log.d("domn", "Item downloaded:");                handler.sendMessage(message);            }        });    }    public void loadBitmap(final String url, final ImageView imageView,            final int width, final int height) {        imageViews.put(imageView, url);        Bitmap bitmap = getBitmapFromCache(url);        // check in UI thread, so no concurrency issues        if (bitmap != null) {            Log.d(null, "Item loaded from cache: " + url);            imageView.setImageBitmap(bitmap);        } else {            imageView.setImageBitmap(placeholder);            queueJob(url, imageView, width, height);        }    }    private Bitmap downloadBitmap(String url, int width, int height) {        try {            Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(                    url).getContent());            bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);            cache.put(url, new SoftReference<Bitmap>(bitmap));            return bitmap;        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        Log.d("bitap", "null");        return null;    }}

 

 

5.Picture.java

package com.lxw.bean;public class Picture {    private String imageUrl;        public Picture(String imageUrl) {        this.imageUrl = imageUrl;    }    public String getImageUrl() {        return imageUrl;    }    public void setImageUrl(String imageUrl) {        this.imageUrl = imageUrl;    }}

 

6.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><ListViewandroid:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>

 

 

7.row.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:contentDescription="@string/app_name" /></LinearLayout>

 

8.运行!

 

ListView异步加载网络图片