简单使用ImageLoader
来源:互联网 发布:securecrt mac 破解 编辑:程序博客网 时间:2024/06/05 18:46
首先本工程要加一个依赖包 Universal-Image-Loader1:创建MyApplication类继承Application 重写oncreate方法public class MyApplication extends Application { @Override public void onCreate() { super.onCreate();// 默认的ImageLoaderconfiguration 这只会加载图片 而不会又缓存等 ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this); ImageLoader.getInstance().init(configuration);//// 自定义的ImageLoaderConfiguration File file = StorageUtils.getIndividualCacheDirectory(this, Environment.getDownloadCacheDirectory().getPath()); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this) .threadPoolSize(3) .memoryCache(new LruMemoryCache(2 * 1024 * 1024))//内存缓存图片 2M .diskCache(new UnlimitedDiskCache(file))//配饰sdcard缓存路径 .diskCacheSize(50 * 1024 * 1024)// sdcard上缓存50M的图片 .diskCacheFileCount(100)//缓存文件的数量 100个 .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .build(); ImageLoader.getInstance().init(configuration); }}2:在清单文件注册 在Application里加一个name属性 名字是自定义的Application<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bawey.qizepu20170425"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:name=".MyApplication" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>3:调用时:在适配器类加载图片,下面是getView方法 public View getView(int position, View convertView, ViewGroup parent) { if (convertView==null){ convertView=View.inflate(context, R.layout.listview_item,null); } ImageView image_1 = (ImageView) convertView.findViewById(R.id.image_1);// 使用默认的ImageLoaderConfiguration// ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image_1);// 使用自定义的ImageLoaderConfiguration ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image_1,options); return convertView; }4:注意:如果用自定义的ImageLoaderConfiguration需要在适配器的构造方法中设置支持缓存private DisplayImageOptions options;public MyListViewAdapter(Context context, List<Foods.ResultBean.DataBean> list) { this.context=context; this.list=list; options=new DisplayImageOptions.Builder() .cacheInMemory(true)//是否内存缓存 .cacheOnDisk(true)//是否sdcard缓存 .build();//构建图片缓存的选项}适配器详细代码如下public class MyListViewAdapter extends BaseAdapter { private Context context; private List<Foods.ResultBean.DataBean> list; private DisplayImageOptions options; public MyListViewAdapter(Context context, List<Foods.ResultBean.DataBean> list) { this.context=context; this.list=list; options=new DisplayImageOptions.Builder() .cacheInMemory(true)//是否内存缓存 .cacheOnDisk(true)//是否sdcard缓存 .build();//构建图片缓存的选项 } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView==null){ convertView=View.inflate(context, R.layout.listview_item,null); } ImageView image_1 = (ImageView) convertView.findViewById(R.id.image_1); TextView text_1 = (TextView) convertView.findViewById(R.id.text_1); text_1.setText(list.get(position).getTitle());// 使用默认的ImageLoaderConfiguration// ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image_1);// 使用自定义的ImageLoaderConfiguration ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image_1,options); return convertView; }}
0 0
- ImageLoader的简单使用
- imageloader的简单使用
- ImageLoader的简单使用
- ImageLoader的简单使用
- ImageLoader的简单使用
- 简单使用ImageLoader
- ImageLoader简单使用
- ImageLoader的简单使用
- ImageLoader的简单使用
- ImageLoader的简单使用
- imageloader的简单使用
- ImageLoader的简单使用详情
- ImageLoader简单介绍和使用
- 简单的ImageLoader的使用
- 系统自带ImageLoader的简单使用
- 使用ImageLoader
- ImageLoader使用
- Imageloader使用
- AngularJS系列——扩展模块-动画、表单验证、路由
- kubernetes DaemonSet资源对象
- 一个简单自定义控件以及.9patch图片的使用
- 51nod 1007 正整数分组【01背包】
- 归并排序
- 简单使用ImageLoader
- C++11 新标准(五)
- Android数据安全之URLEncoder编码
- 。。
- 求素数(质数)算法
- ip用int类型存储
- chmod命令详细用法
- bzoj 1088: [SCOI2005]扫雷Mine (枚举+判定)
- NGUI源码分析(五) UIPanel