1. 程式人生 > >用imageLoader在listView展示圖片

用imageLoader在listView展示圖片

public class mApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//改變圖片形式
DisplayImageOptions options = new DisplayImageOptions.Builder().
//支援記憶體快取
cacheInMemory(true).
//支援磁碟快取
cacheOnDisk(true).
displayer(new CircleBitmapDisplayer()).
build();
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).
memoryCacheSizePercentage(10).
diskCacheSize(501024

1024).
tasksProcessingOrder(QueueProcessingType.FIFO).
defaultDisplayImageOptions(options).
build();
ImageLoader instance = ImageLoader.getInstance();
instance.init(build);
}
}
//在MyBase裡的程式碼
class MyBase extends BaseAdapter {
private Context context;
private List<News.Data> list;
public MyBase(Context context) {
this.context = context;
list=new ArrayList<News.Data>();
}

public void setList(List<News.Data> list) {
    this.list = list;
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return list.size();
}

@Override
public News.Data getItem(int position) {

    return list.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder;
    if(convertView==null){
        convertView=View.inflate(context,R.layout.item,null);
        holder=new Holder();
        holder.image=convertView.findViewById(image);
        holder.text=convertView.findViewById(text);
        convertView.setTag(holder);
    }else {
        holder = (Holder) convertView.getTag();
    }

    DisplayImageOptions options  =new DisplayImageOptions.Builder().
            //配置色彩
            bitmapConfig(Bitmap.Config.RGB_565).
            //配置是否記憶體快取
            cacheInMemory(true).
            //配置磁碟是否快取
             cacheOnDisk(true).
            //載入顯示圖片
             showImageOnLoading(R.mipmap.ic_launcher).
            //載入錯誤圖片
             showImageOnFail(R.mipmap.ic_launcher).
            //載入空圖片
             showImageForEmptyUri(R.mipmap.ic_launcher).
            //配置圖片如何縮放
            imageScaleType(ImageScaleType.EXACTLY).
            //顯示效果:圓形
             displayer(new RoundedBitmapDisplayer(20)).build();
    ImageLoader.getInstance().displayImage(getItem(position).getPic_url(),holder.image,options);
    holder.text.setText(list.get(position).getNews_title());
    return convertView;
}
class Holder{
    TextView text;
    ImageView image;
}