ImageLoder的简单使用

imageLoder是一个开源的第三方库,使用它我们可以很容易的实现图片的异步加载,可以到这里下载打开链接 imageLoder的jar包。下面通过代码的说明一下ImageLoader的使用。

先看一下实现的效果图:


上代码:



ListViewActivity

package com.example.mycsdndemo;

import java.util.List;
import java.util.Map;

import com.example.adapter.ListViewItemAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class ListViewActivity extends Activity {

	private ListView listView;
	private List<Map<String, Object>> datas;
	private ListViewItemAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
		listView = (ListView)findViewById(R.id.listview);
		datas = DateUtils.getDates();
		adapter = new ListViewItemAdapter(ListViewActivity.this, datas);
		listView.setAdapter(adapter);
	}
}

DataUtils

package com.example.mycsdndemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DateUtils {

	private static String[] paths = new String[] {
			"http://img.ptcms.csdn.net/article/201508/18/55d314de1b7e9_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/19/55d402f5625ce_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/19/55d42af31c5bc_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/21/55d688d8426b6_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/21/55d723d9dbbce_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/24/55dab0e5b56b3_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/25/55dc2f76a81b6_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/25/55dc490d979af_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/26/55dd63c91be2c_thumb.jpg",
			"http://img.ptcms.csdn.net/article/201508/26/55dd85ac719f8_thumb.jpg" };

	public static List<Map<String, Object>> getDates() {
		List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < paths.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("img_url", paths[i]);
			map.put("item", "item" + (i + 1));
			datas.add(map);
		}

		return datas;
		/**
 
		 */
	}
}

ListViewItemAdapter

package com.example.adapter;

import java.util.List;
import java.util.Map;

import com.example.mycsdndemo.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewItemAdapter extends BaseAdapter {

	Context context;
	LayoutInflater inflater;
	List<Map<String, Object>> datas;

	/**
	 * 
	 * 采用ImageLoader进行图片加载
	 */
	private ImageLoader imageLoader = ImageLoader.getInstance();  //imageLoder采用单例模式进行设计
    private DisplayImageOptions options;  //加载图片的时的选项
    
    
	
	public ListViewItemAdapter(Context context, List<Map<String, Object>> datas) {
		this.context = context;
		this.datas = datas;
		inflater = LayoutInflater.from(context);
		
		//1.完成ImageLoaderConfiguration的设置
		ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(context);
		//2.对图片加载的显示选项进行设置
		options = new DisplayImageOptions.Builder() 
		.showStubImage(R.drawable.icon_load) //设置加载过程中显示的图片
		.showImageForEmptyUri(R.drawable.icon_default) //当图片URL为空时 所显示的图片
		.showImageOnFail(R.drawable.icon_default) //加载出现错误的时候显示的图片
		.cacheInMemory() //默认不缓存到 内存
		.cacheOnDisc() //默认不缓存到SD卡
		.displayer(new RoundedBitmapDisplayer(10))//设置图片显示图片为圆角,圆角的半径
		.build();
		//3.使用ImageLoaderConfiguration对ImageLoader进行初始化
		imageLoader.init(configuration);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.listview_item, parent,false);
			holder.img =(ImageView) convertView.findViewById(R.id.listview_item_img);
			holder.text = (TextView) convertView.findViewById(R.id.listview_item_text);
			convertView.setTag(holder);
		}else{
		holder =(ViewHolder) convertView.getTag();
		}
		holder.text.setText(datas.get(position).get("item").toString());
		
		//图片加载过程中,不加监听事件
	//	imageLoader.displayImage(datas.get(position).get("img_url").toString(), holder.img, options);
		
		
		//图片加载过程中加监听事件,监听事件继承SimpleImageLoadingListener,共有四个方法,在不同情况下执行
		imageLoader.displayImage(datas.get(position).get("img_url").toString(), holder.img, options,new MyImageLoaderListener());
		return convertView;
	}
	
	
}
class MyImageLoaderListener extends SimpleImageLoadingListener{
	@Override
	public void onLoadingStarted(String imageUri, View view) {
		// TODO Auto-generated method stub
		super.onLoadingStarted(imageUri, view);
		System.out.println();
	}
	
	@Override
	public void onLoadingCancelled(String imageUri, View view) {
		// TODO Auto-generated method stub
		super.onLoadingCancelled(imageUri, view);
	}
	
	@Override
	public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
		// TODO Auto-generated method stub
		super.onLoadingComplete(imageUri, view, loadedImage);
	}
	
	@Override
	public void onLoadingFailed(String imageUri, View view,
			FailReason failReason) {
		// TODO Auto-generated method stub
		super.onLoadingFailed(imageUri, view, failReason);
	}
}
class ViewHolder {
	public ImageView img;
	public TextView text;
}

贴出了部分代码,布局很简单,没有贴出。ImageLoader的使用我已经在ListViewItemAdapter做了注释。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值