2. GridView中使用 UIL
GridView 中使用 UIL :
-- 核心代码 : 主要是在 GridView 中的适配器中使用 UIL;
-- 设置展示选项 :
private DisplayImageOptions options; options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) //设置加载时显示的图片 .showImageForEmptyUri(R.drawable.ic_empty) //设置 Uri 为 空时显示的图片 .showImageOnFail(R.drawable.ic_error) //设置加载失败显示的图片 .cacheInMemory(true) //设置内存缓存 .cacheOnDisk(true) //设置磁盘缓存 .considerExifParams(true) //考虑相机参数 .bitmapConfig(Bitmap.Config.RGB_565) //设置解码参数 .build();
-- 设置加载 处理 监听器 : 在加载图片时 可以设置加载监听器;
//加载 并 显示图片, 设置加载监听器 监听 开始加载 加载失败 加载完成 事件 ImageLoader.getInstance() .displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { holder.progressBar.setProgress(0); holder.progressBar.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { holder.progressBar.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { holder.progressBar.setVisibility(View.GONE); } }, new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { holder.progressBar.setProgress(Math.round(100.0f * current / total)); } });
-- 完整的 GridView 适配器代码 :
private static class ImageAdapter extends BaseAdapter { private static final String[] IMAGE_URLS = Constants.IMAGES_CN; private LayoutInflater inflater; private DisplayImageOptions options; ImageAdapter(Context context) { inflater = LayoutInflater.from(context); options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) //设置加载时显示的图片 .showImageForEmptyUri(R.drawable.ic_empty) //设置 Uri 为 空时显示的图片 .showImageOnFail(R.drawable.ic_error) //设置加载失败显示的图片 .cacheInMemory(true) //设置内存缓存 .cacheOnDisk(true) //设置磁盘缓存 .considerExifParams(true) //考虑相机参数 .bitmapConfig(Bitmap.Config.RGB_565) //设置解码参数 .build(); } @Override public int getCount() { return IMAGE_URLS.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; View view = convertView; if (view == null) { view = inflater.inflate(R.layout.item_grid_image, parent, false); holder = new ViewHolder(); assert view != null; holder.imageView = (ImageView) view.findViewById(R.id.image); holder.progressBar = (ProgressBar) view.findViewById(R.id.progress); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } //加载 并 显示图片, 设置加载监听器 监听 开始加载 加载失败 加载完成 事件 ImageLoader.getInstance() .displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { holder.progressBar.setProgress(0); holder.progressBar.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { holder.progressBar.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { holder.progressBar.setVisibility(View.GONE); } }, new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { holder.progressBar.setProgress(Math.round(100.0f * current / total)); } }); return view; } static class ViewHolder { ImageView imageView; ProgressBar progressBar; } }
-- 显示效果 :