Android-Universal-Image-Loader-master 详解
Android-Universal-Image-Loader-master 使用
下载地址:https://github.com/nostra13/Android-Universal-Image-Loader
步骤:
1)、先要配置ImageLoaderConfiguration这个类实现全局ImageLoader的实现情况,
可知Application中配置。
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
注:更多配置项到ImageLoaderConfiguration.Builder中查看。
2)、初始化全局配置
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
3)、初始化显示图片格式DisplayImageOptions的设置
DisplayImageOptions 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();
4)、加载图片到控件上
ImageLoader.getInstance().displayImage(imageUrls[position], holder.image, options,null);
注:displayImage此方法有多个重载形式,根据情况选择其一。
5)实例:
5.1)配置全局参数并初始化:
/*******************************************************************************
public class UILApplication extends Application {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressWarnings("unused")
@Override
public void onCreate() {
if (Constants.Config.DEVELOPER_MODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
}
super.onCreate();
initImageLoader(getApplicationContext());
}
//初始化ImageLoader全局配置
public static void initImageLoader(Context context) {
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
}
}
5.2)、
public class MyActivity extends Activity {
private LayoutInflater inflater;
private DisplayImageOptions options;
private ListView listView;
String[] imageUrls = Constants.IMAGES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fr_image_list);
inflater=LayoutInflater.from(this);
init();
listView= (ListView) findViewById(android.R.id.list);
listView.setAdapter(new MyBaseAdapter());
}
private class MyBaseAdapter extends BaseAdapter{
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return imageUrls[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
view = inflater.inflate(R.layout.item_list_image, parent, false);
holder.image = (ImageView) view.findViewById(R.id.image);
holder.text = (TextView) view.findViewById(R.id.text);
view.setTag(holder);
}else{
holder=(ViewHolder) view.getTag();
}
holder.text.setText("Item " + (position + 1));
//实例化ImageLoader并加载图片
ImageLoader.getInstance().displayImage(imageUrls[position], holder.image, options,null);
return view;
}
}
//view缓存对象
private static class ViewHolder {
TextView text;
ImageView image;
}
private void init() {
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();
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。