这篇打算直接告诉大家怎么用吧,其实这个也不是很难的框架,大致使用过程如下:
// 获取缓存图片目录 File cacheDir = StorageUtils.getOwnCacheDirectory(activity, "imageloader/Cache"); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( activity).memoryCacheExtraOptions(800, 760) // 保存每个缓存图片的最大长和宽 .threadPoolSize(3) .diskCache(new UnlimitedDiscCache(cacheDir)) // 线程池的大小 这个其实默认就是3 .memoryCacheSize(2 * 1024) // 设置缓存的最大字节 .denyCacheImageMultipleSizesInMemory() // 缓存显示不同大小的同一张图片 .imageDownloader( new BaseImageDownloader(activity, 5 * 1000, 30 * 1000)) // connectTimeout // s)超时时间 .build(); imageLoader.init(config);
第二步设置显示图片选项:
</pre><p></p><p><pre name="code" class="java">DisplayImageOptionsoptions = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_launcher)//设置图片在下载期间显示的图片 .showImageForEmptyUri(R.drawable.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_launcher)//设置图片加载/解码过程中错误时候显示的图片 .cacheInMemory(true)//设置下载的图片是否缓存在内存中 .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中 .displayer(new FadeInBitmapDisplayer(100)) .considerExifParams(true) .displayer(new FadeInBitmapDisplayer(100))// 图片加载好后渐入的动画时间 .displayer(new RoundedBitmapDisplayer(1)).build();//这个一定要设置,不设置的话会导致图片不能够铺满整个控件,这个是设置圆角效果的,如果大家不喜欢圆角可以设置为1几乎没有什么效果
下面是官方给的一个列子,会android看看下面的这个代码应该就知道怎么使用了吧,非常简单
public class ImageListActivity extends AbsListViewBaseActivity { DisplayImageOptions options; String[] imageUrls; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_image_list); Bundle bundle = getIntent().getExtras(); imageUrls = bundle.getStringArray(Extra.IMAGES); options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) .showImageForEmptyUri(R.drawable.ic_empty) .showImageOnFail(R.drawable.ic_error) .cacheInMemory(true) .cacheOnDisk(true) .considerExifParams(true) .displayer(new RoundedBitmapDisplayer(20)) .build(); listView = (ListView) findViewById(android.R.id.list); ((ListView) listView).setAdapter(new ItemAdapter()); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { startImagePagerActivity(position); } }); }
//注意下这里,当按下返回键的时候清除一下在内存中的图片,大家不过要过度依赖自动内存管理,有些时候还是手动去清除掉内存比较好, @Override public void onBackPressed() { AnimateFirstDisplayListener.displayedImages.clear(); super.onBackPressed(); } private void startImagePagerActivity(int position) { Intent intent = new Intent(this, ImagePagerActivity.class); intent.putExtra(Extra.IMAGES, imageUrls); intent.putExtra(Extra.IMAGE_POSITION, position); startActivity(intent); } private static class ViewHolder { TextView text; ImageView image; } class ItemAdapter extends BaseAdapter { private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener(); @Override public int getCount() { return imageUrls.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = convertView; final ViewHolder holder; if (convertView == null) { view = getLayoutInflater().inflate(R.layout.item_list_image, parent, false); holder = new ViewHolder(); holder.text = (TextView) view.findViewById(R.id.text); holder.image = (ImageView) view.findViewById(R.id.image); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.text.setText("Item " + (position + 1)); imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener); return view; } } private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener { static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { if (loadedImage != null) { ImageView imageView = (ImageView) view; boolean firstDisplay = !displayedImages.contains(imageUri); if (firstDisplay) { FadeInBitmapDisplayer.animate(imageView, 500); displayedImages.add(imageUri); } } } } }
这是一个demo,这个图片选项最好配置成一个单列类,便于管理和减少内存的溢出。