您的位置:首页 > 移动开发 > Android开发

android Universal-Image-Loader框架学习(下)

2015-05-31 08:44 423 查看
一.GridView显示图片加进度框:

在Universal-Image-Loader框架可以让加载图片变的简单,下面我们看一下如何在GridView中使用附加上进度条。

效果图:





主界面:

<span style="font-size:18px;">public class GridActivity extends Activity{

private GridView grid;
public static final String[] IMAGES = new String[] {
"http://img.dapixie.com/uploads/allimg/111105/1-111105145231.jpg",
"http://img3.3lian.com/2014/c2/61/d/1.jpg",
"http://img3.3lian.com/2014/c2/61/d/2.jpg",
"http://img3.3lian.com/2014/c2/61/d/3.jpg",
"http://img3.3lian.com/2014/c2/61/d/4.jpg",
"http://img3.3lian.com/2014/c2/61/d/5.jpg",
"http://img3.3lian.com/2014/c2/61/d/6.jpg",
"http://img3.3lian.com/2014/c2/61/d/7.jpg",
"http://img3.3lian.com/2014/c2/61/d/8.jpg",
"http://img3.3lian.com/2014/c2/61/d/9.jpg",
"http://img3.3lian.com/2014/c2/61/d/10.jpg",
"http://img3.3lian.com/2014/c2/61/d/11.jpg",
"http://img3.3lian.com/2014/c2/61/d/12.jpg" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_activity);

grid = (GridView)findViewById(R.id.grid);
grid.setAdapter(new ImageAdapter(this));

}
@Override
protected void onDestroy() {
super.onDestroy();
ImageLoader.getInstance().clearMemoryCache();// 清空缓存
ImageLoader.getInstance().clearDiskCache();//清空缓存
ImageLoader.getInstance().stop();// 停止
}

private static class ImageAdapter extends BaseAdapter {

private static final String[] IMAGE_URLS = GridActivity.IMAGES;

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)
.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.grid_listview_item, 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) {
//设置进度条为0
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) {
//current当前进度,total总进度
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});

return view;
}
}

static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}</span>


主界面布局:

<span style="font-size:18px;"><?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" >

<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="4dip"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="4dip"
android:padding="4dip" />

</LinearLayout>
</span>


grid_item布局:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dip">

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="120dip"
android:adjustViewBounds="true"
android:contentDescription="@string/descr_image"
android:scaleType="centerCrop" />

<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:layout_gravity="bottom"
style="@style/ProgressBarStyle" />
</FrameLayout></span>


进度条样式:

<span style="font-size:18px;"><style name="ProgressBarStyle" parent="@android:style/Widget.Holo.ProgressBar.Horizontal"/></span>


ImageLoader配置:

<span style="font-size:18px;">public class MyApp extends Application {

@Override
public void onCreate() {
super.onCreate();
initImageLoader1(getApplicationContext());

}
/*
* 自定义配置
*/
public static void initImageLoader(Context context) {
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(
context);
config.threadPoolSize(3);//线程池内加载的数量
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();// 不缓存图片的多种尺寸在内存中
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());// 将保存的时候的URI名称用MD5
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs();// Remove for release app
// 初始化ImageLoader使用
ImageLoader.getInstance().init(config.build());
}

/*
* 默认的配置,一般没有特殊的要求的时候就使用默认就好了。
*/
public static void initImageLoader1(Context context) {
// 创建默认的ImageLoader配置参数
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(context);
// 初始化ImageLoader使用
ImageLoader.getInstance().init(configuration);
}
}</span>


这个例子是从上篇文章继续写的,也是在它项目里继续写的。

如果你想在滑动中停止加载,而在停止滑动后再继续加载图片,Universal-Image-Loader也提供了这样的功能,PauseOnScrollListener这个类就是监听。

listView.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), pauseOnScroll, pauseOnFling));
PauseOnScrollListener的第一个参数是图片加载对象ImageLoader,第二个是控制是否在滑动过程中暂停加载图片,如果需要暂停传true就行了,第三个参数控制猛的滑动界面的时候图片是否加载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: