您的位置:首页 > 理论基础 > 计算机网络

Android网络图片加载缓存处理库的使用

2016-03-26 01:01 645 查看


Android网络图片加载缓存处理库的使用

 
网络图片加载库的特点及优势
Android中图片处理存在的难点:
• OOM内存溢出
• 图片尺寸和缩略图处理的平衡
• 网络图片的加载与缓存机制
这里将会选择两款比较优秀的开源图片处理库框架:Universal-ImageLoaderPicasso为大家进行讲解。
Universal-ImageLoader的简介和特点:
Universal-ImageLoader是目前Android主流的图片处理库框架之一,作者是白俄罗斯的Sergey Tarasevich。
在Android图片处理中需要考虑的问题很多,例如OOM、图片缓存和网络图片加载、多线程问题及图片压缩处理等等复杂的问题。但是Universal-ImageLoader已经帮我们把这些问题处理好了,对外提供了相应的完善的请求API,我们只需要按照要求使用即可。
Universal-ImageLoader特点:
• 支持本地图片和网络图片多线程异步加载和缓存处理
个性化的配置自己项目的ImageLoader
• 图片加载过程的监听回调
• 自动对加载的图片针对当前剩余内存进行裁剪优化,防止OOM
• 较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片
缺点:没有对本地文件压缩处理的相关API方法以及默认都是Src模式设置图片没有针对Background属性开放API
Picasso的简介和特点
Picasso是Square公司开源的一个Android图形缓存库。可以实现图片下载和缓存功能。
特点:
加载网络或本地图片并自动缓存处理
链式调用
• 图形转换操作,如变换大小,旋转等,提供了接口来让用户可以自定义转换操作
• 在Adapter中回收和取消当前的下载功能
总结:
• 都有高效的网络图片下载和缓存性能
• Universal-ImageLoader功能多,灵活使用配置
• Picasso使用复杂的图片压缩转换来尽可能的减少内存消耗
• 在Adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解决了这个问题。
Universal-ImageLoader的用法和案例
1、Universal-ImageLoader的配置

可以全局配置:在Application里进行配置。

</pre><pre name="code" class="java">public class MyApplication extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
this)
.memoryCacheExtraOptions(480, 800)
// max width, max height,即保存的每个缓存文件的最大长宽
.discCacheExtraOptions(480, 800, null)
// Can slow ImageLoader, use it carefully (Better don't use
// it)/设置缓存的详细信息,最好不要设置这个
.threadPoolSize(3)
// 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
// You can pass your own memory cache
// implementation/你可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024)
.discCacheSize(50 * 1024 * 1024)
.discCacheFileNameGenerator(new Md5FileNameGenerator())
// 将保存的时候的URI名称用MD5 加密
.tasksProcessingOrder(QueueProcessingType.LIFO)
.discCacheFileCount(100)
// 缓存的文件数量
.discCache(
new UnlimitedDiscCache(new File(Environment
.getExternalStorageDirectory()
+ "/myApp/imgCache")))
// 自定义缓存路径
.defaultDisplayImageOptions(getDisplayOptions())
.imageDownloader(
new BaseImageDownloader(this, 5 * 1000, 30 * 1000))
.writeDebugLogs() // Remove for release app
.build();// 开始构建
ImageLoader.getInstance().init(config);
}

private DisplayImageOptions getDisplayOptions() {
DisplayImageOptions options;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher) // 设置图片在下载期间显示的图片
.showImageForEmptyUri(R.drawable.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.ic_launcher) // 设置图片加载/解码过程中错误时候显示的图片
.cacheInMemory(true)// 设置下载的图片是否缓存在内存中
.cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中
.considerExifParams(true) // 是否考虑JPEG图像EXIF参数(旋转,翻转)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
// .delayBeforeLoading(int delayInMillis)//int
// delayInMillis为你设置的下载前的延迟时间
// 设置图片加入缓存前,对bitmap进行设置
// .preProcessor(BitmapProcessor preProcessor)
.resetViewBeforeLoading(true)// 设置图片在下载前是否重置,复位
.displayer(new RoundedBitmapDisplayer(20))// 是否设置为圆角,弧度为多少
.displayer(new FadeInBitmapDisplayer(100))// 是否图片加载好后渐入的动画时间
.build();// 构建完成
return options;
}
}
在menifest文件中为application添加资源定义的name标签
<application
<span style="color:#FF0000;"><strong>android:name="com.xiaokai.MyApplication"</strong></span>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jike.imageloaderdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>


可以针对单一加载图片的地方配置。
例如:可配置图片缓存保存路径、线程池内加载的数量缓存的文件数量每个缓存文件的最大长宽加载过程中和加载失败时显示的图片等等。
2、用Universal-ImageLoader加载网络图片和本地图片

Universal-ImageLoader支持网络图片的加载和本地图片的加载,而且可以自动缓存、自动根据当前手机环境进行压缩处理防止出现OOM。
也可以监听整个图片的加载过程,可控
/**
* 1、 Universal-ImageLoader的配置
* <p/>
* 2、用Universal-ImageLoader加载网络图片和本地图片
*
* @author Administrator
*/
public class MainActivity extends Activity {
private ImageLoader loader;
private ImageView iv_img;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loader = ImageLoader.getInstance();
iv_img = (ImageView) this.findViewById(R.id.iv_img);
String uri = "file:///" + "本地路径";
// loader.displayImage(
// "http://s1.jikexueyuan.com/current/static/images/logo.png",
// iv_img);
loader.displayImage(
"http://huodong.jikexueyuan.com/Public/home/static/2016jikejie/1111/img/board.png",
iv_img, new ImageLoadingListener() {

@Override
public void onLoadingStarted(String arg0, View arg1) {
Log.i("info", "onLoadingStarted");
}

@Override
public void onLoadingFailed(String arg0, View arg1,
FailReason arg2) {
Log.i("info", "onLoadingFailed");
}

@Override
public void onLoadingComplete(String arg0, View arg1,
Bitmap arg2) {
Log.i("info", "onLoadingComplete");
}

@Override
public void onLoadingCancelled(String arg0, View arg1) {
Log.i("info", "onLoadingCancelled");
}
});
}

}为应用添加访问权限:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

效果图入下:

Picasso的用法和案例
 
1、Picasso的几个重要方法的介绍

图片异步加载
Picasso.with(context).load("http://baidu.com/logo.png").into(imageView);
图片转换:转换图片以适应布局大小并减少内存占用Picasso.with(context).load(url).resize(50,50) .centerCrop() .into(imageView);
Adapter 中的下载:Adapter的重用会被自动检测到,Picasso会取消上次的加载空白或者错误占位图片设置方法及本地资源文件的加载方法
2、用Picasso加载网络图片和本地图片

Picasso采用链式调用加载处理图片方式。
除了加载网络图片,picasso还支持加载Resources, assets, files, content providers中的本地资源文件
代码如下:
/**
* 1、Picasso的几个重要方法的介绍
*
* 2、用Picasso加载网络图片和本地图片
*
*/

public class MainActivity extends Activity {
private ImageView iv_img;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_img = (ImageView) this.findViewById(R.id.iv_img);
Picasso.with(this)
.load("http://alad.res.meizu.com/alad/business/business/1240_01.jpg")
.rotate(70)
.into(iv_img);
Picasso.with(this)
.load("http://alad.res.meizu.com/alad/business/business/1240_01.jpg")
.rotate(70)
.resize(50, 50).into(iv_img);
Picasso.with(this)
.load("http://alad.res.meizu.com/alad/business/business/1240_01.jpg")
.rotate(70)
.error(R.mipmap.ic_launcher).into(iv_img);
}
}为应用添加访问权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


效果如下:

来源:http://www.jikexueyuan.com/course/805.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android 图片 缓存 网络