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(p_w_picpathUrls[position], holder.p_w_picpath, 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[] p_w_picpathUrls = Constants.IMAGES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fr_p_w_picpath_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 p_w_picpathUrls.length;
}
@Override
public Object getItem(int position) {
return p_w_picpathUrls[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_p_w_picpath, parent, false);
holder.p_w_picpath = (ImageView) view.findViewById(R.id.p_w_picpath);
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(p_w_picpathUrls[position], holder.p_w_picpath, options,null);
return view;
}
}
//view缓存对象
private static class ViewHolder {
TextView text;
ImageView p_w_picpath;
}
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();
}
}
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、成都小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了禅城免费建站欢迎大家使用!
分享标题:Android-Universal-Image-Loader-master详解
标题路径:http://pwwzsj.com/article/pegidc.html