【Android 基礎】ListView中使用ImageLoader

演示效果:

這裏寫圖片描述
ImageLoader體驗還是不錯的。

準備工作

0)準備demo中展示的圖片鏈接(我用python在新浪某個網頁爬下來的圖片鏈接。

public class Constants {
    public static final String[] IMAGES = new String[]{
            "http://n.sinaimg.cn/news/20170106/cOhv-fxzkfuh5815912.jpg",
            "http://n.sinaimg.cn/news/20170106/KIiF-fxzkfvn0614393.jpg",
            "http://n.sinaimg.cn/news/20170105/QdWr-fxzkssy0738915.jpg",
            "http://n.sinaimg.cn/news/20170109/QTig-fxzkfuk2956935.jpg",
            "http://n.sinaimg.cn/photo/20170103/NcMh-fxzencv4059029.jpg",
            "http://n.sinaimg.cn/news/20170106/nx0G-fxzkfvn0462945.jpg",
            "http://n.sinaimg.cn/news/20170106/t3J_-fxzkfuh5640887.jpg",
            "http://n.sinaimg.cn/news/20170106/wU62-fxzkssy0884459.jpg",
            "http://n.sinaimg.cn/news/20170109/aijL-fxzkfuh6185632.jpg",
            "http://n.sinaimg.cn/news/20161230/9AkB-fxzencv2570123.jpg",
            "http://n.sinaimg.cn/news/20161201/PZkk-fxyiayr8634345.jpg",
            "http://n.sinaimg.cn/news/20161130/TWfj-fxyawmp0642096.jpg",
            "http://n.sinaimg.cn/news/20161102/Uiei-fxxfyex5828108.jpg",
            "http://n.sinaimg.cn/news/20161018/0d60-fxwvpar8352557.jpg",
            "http://n.sinaimg.cn/news/20160921/WqBQ-fxvyqwa3647033.jpg",
            "http://n.sinaimg.cn/news/20160830/hath-fxvkkcf6323525.jpg",
            "http://n.sinaimg.cn/news/20160909/saUR-fxvukhz1444567.jpg",
            "http://n.sinaimg.cn/photo/20160817/ZkS0-fxuxwyx8877444.jpg",
            "http://n.sinaimg.cn/news/20160807/NFcm-fxutfpk4938233.jpg",
            "http://n.sinaimg.cn/history/20161228/t29C-fxyxusa5682441.jpg",
            "http://n.sinaimg.cn/news/transform/20160804/7QRM-fxutfpk4726401.jpg",
            "http://n.sinaimg.cn/news/20170103/XEuK-fxzencv3831736.jpg",
            "http://n.sinaimg.cn/history/20170106/QmWk-fxzkfvn0469366.jpg",
            "http://n.sinaimg.cn/history/20170106/ZCve-fxzkssy0887004.jpg",
            "http://n.sinaimg.cn/history/20170106/GP9l-fxzkssy0887319.jpg",
            "http://n.sinaimg.cn/news/20170103/Fdqw-fxzencv3829645.jpg",
            "http://n.sinaimg.cn/news/20161213/Ej7n-fxypunk6453889.jpg"
    };
}

1)導入universal-image-loader-1.9.5.jar到項目中 。
2)創建MyApplication繼承Application,在oncreate()中初始化ImageLoader。
(1)初始化ImageLoaderConfiguration。

 // 初始化參數
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)               // 線程優先級
                .denyCacheImageMultipleSizesInMemory()                  // 當同一個Uri獲取不同大小的圖片,緩存到內存時,只緩存一個。默認會緩存多個不同的大小的相同圖片
                .discCacheFileNameGenerator(new Md5FileNameGenerator()) // 將保存的時候的URI名稱用MD5
                .tasksProcessingOrder(QueueProcessingType.LIFO)         // 設置圖片下載和顯示的工作隊列排序
                .writeDebugLogs()                                       // 打印debug log
                .build();

(2)ImageLoader全局配置

// 全局初始化此配置
        ImageLoader.getInstance().init(config);

3)將創建的MyApplication在AndroidManifest.xml中註冊。
4)在AndroidManifest.xml中添加聯網權限和寫sdk權限。

在ListView中加載圖片的步驟

(1)初始化佈局

<?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"
>

    <include layout="@layout/titlebar"></include>

    <ListView
        android:id="@+id/lv_imageloader"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

(2)初始化listview並設置適配器

public class ImageloaderListviewActivity extends Activity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.lv_imageloader)
    ListView lvImageloader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_imageloader_listview);
        ButterKnife.bind(this);

        initData();
    }

    private void initData() {
        // 初始化標題
        tvTitle.setText("Imageloader應用在Listview中");

        ImageloaderListviewAdapter imageloaderListviewAdapter = new ImageloaderListviewAdapter(this);

        lvImageloader.setAdapter(imageloaderListviewAdapter);
    }
}

(3)初始化適配器

public class ImageloaderListviewAdapter extends BaseAdapter {
    private Context mContext;
    private final ImageLoader imageLoader;
    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.error)          // 設置圖片下載期間顯示的圖片
            .showImageForEmptyUri(R.drawable.error)  // 設置圖片Uri爲空或是錯誤的時候顯示的圖片
            .showImageOnFail(R.drawable.error)       // 設置圖片加載或解碼過程中發生錯誤顯示的圖片
            .cacheInMemory(true)                        // 設置下載的圖片是否緩存在內存中
            .cacheOnDisk(true)                          // 設置下載的圖片是否緩存在SD卡中
            .displayer(new RoundedBitmapDisplayer(20))  // 設置成圓角圖片
            .build();                                   // 創建配置過得DisplayImageOption對象;

    public ImageloaderListviewAdapter(Context context) {
        mContext = context;

        // 初始化imageloader
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return Constants.IMAGES.length;
    }

    @Override
    public Object getItem(int position) {
        return Constants.IMAGES[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 獲取或創建viewhoder
        Viewholder holder;

        if(convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_imageloader_listview, null);

            holder = new Viewholder(convertView);

            convertView.setTag(holder);
        }else {
            holder = (Viewholder) convertView.getTag();
        }

        // 獲取當前item數據

        // 顯示數據
        holder.name.setText("item"+(position + 1));

        imageLoader.displayImage(Constants.IMAGES[position],holder.iv,options);

        return convertView;
    }

    class Viewholder{
        @Bind(R.id.iv_imageloader_listview)
        ImageView iv;

        @Bind(R.id.tv_imageloader_name)
        TextView name;

        public Viewholder(View view) {
            ButterKnife.bind(this,view);
        }
    }
}
發佈了89 篇原創文章 · 獲贊 37 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章