简单使用ImageLoader
2017-05-02 20:32
232 查看
首先本工程要加一个依赖包 Universal-Image-Loader 1:创建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, 8760 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; } }
相关文章推荐
- Image-Universal-ImageLoader 最简单的使用(新手快速上手)
- ImageLoader的简单使用
- Glide的添加与使用 加载图片比Imageloader简单了好多
- Android使用Universal-ImageLoader在ListView中加载网络图片简单示例
- imageloader的简单使用
- imageloader的简单使用
- Volley中ImageRequest、ImageLoader、NetworkImageView的简单使用
- 图片加载框架简单介绍<一> ImageLoader 的基本使用
- Glide 添加图片的简单使用 比ImageLoader更简单
- 简单的ImageLoader的使用
- ImageLoader简单使用
- 【ImageLoader】Universal-Image-Loader学习系列(一),下载及简单的使用
- ImageLoader的简单使用方法
- ImageLoader简单使用
- ImageLoader的简单使用
- ImageLoader的简单使用
- ImageLoader的简单使用
- ImageLoader简单介绍和使用
- Android-----第三方 ImageLoader 的简单配置和使用
- ImageLoader的简单使用详情