Picaso完美兼容OkHttp3.3,缓存优化两不误
为安在Fresco,Glide这么强大的配景下,我又想起了当初的Picasso,又为何写这篇文章?是因为比来项目给与了square公司的RxAndroid,Retrfit和OKhttp, 不得不联想到这个公司曾经还有款图片加载Picasso,所以给与了square公司的全家桶来进行项目开发,为了减少开发本钱和也防备Apk增大,终究一个公司的框架之前兼容性不用担忧,那么请让我们回顾一下Picass之路
首先先让我们看看主流图片加载库
Picasso,Square公司的开源项目 ,和Square的网络库一起能阐扬最大感化。占用内存小,自身不带缓存,需依赖OKhttps实现缓存,不撑持gif图片
Fresco,FB的明星项目,也是2015最火的项目之一,匿名共享缓存等机制保证低端机表示极佳,但是源代码基于C/C++,阅读困难度提升。效率高,sdk库占用包体积对照大
Glide,Google员工私人项目,但是Google很多项目在用,占用内存小,减低oom更靠谱,相对Picasso在Gif方面有优势,并自带缓存成果!
我做了一个尝试比拟 用一个普通listview加载50张图片,并快速滑动列表,下面分袂是glide和picasso消耗内存图
分析后得出 一个占用内存大 一个占用cpu资源大, 这种区别是由于picasso只缓存一张大图,每次加载按照imagview的巨细裁剪,因此消耗的cpu资源高,glide是分袂存储差别尺寸的小图,每次不用计算,因此消耗内存对照多,加载速度相对Picasso也快,但也很耗流量.
为了制止OOM, 我毫不踌躇选择了消耗内存较小的picasso, Fresco不用说都是加载速度第一的框架,给与c库 ,我没做集成测试,具体消耗几多cpu资源我无法给出数据,据说业界第一,但是对apk巨细要求的项目很可能不太合适,这里对Apk包体积要求不高的项目,Fresco是优先的首选。
喜欢glide的伴侣可以看看这篇文章 :
尝试测试并做了简单对照后,为何还要继续说Picasso,不是说他有多快多流畅,只是当你使用了square公司其他的开源项目,会发明他们城市依赖okhttp,okhttp的强大不言而喻,一个网络库可以无漏洞的对接Retrofit和Picasso.今天只介绍piacsso相关的,说说picasso(官方:https://github.com/square/picasso) 的一些常用技巧!
#使用方法:
配置gradle
dependencies { c compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.okhttp3:okhttp:3.3.1' compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' }据说目前的2.5.3已修复了2.52无法兼容okhttp3的问题,但我还是选择了2.52版本。
根基加载用法
Picasso.with(getApplication()) .load(url) .into(imageView);以上用法很简单,,加载图片时供给url插入到imageview即可,picasso其他强大功还没有太多的理解的同学请Follow Me!
#裁剪图片
1
Picasso.with(getApplication()).resize(width, height);
这句要领会呈现bug,误用!
请用Transformation来进行转义实现:
1
2
3
4
Picasso.with(getApplication())
.load(url)
.transform(new PaTransformation(width, height)).into(imageView);
Transformation可以拦截到picasoo返回的bitmap,拿着bitmap随心所欲!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class TamicTransformation implements Transformation {
private int width;
private int height;
private String key;
public PaTransformation(int width, int height) {
this(width, height, width + "*" + height);
}
public PaTransformation(int width, int height, String key) {
this.width = width;
this.height = height;
this.key = key;
}
@Override
public Bitmap transform(Bitmap source) {
略 拿着source进行裁剪缩放即可
if (result != source) {
source.recycle();
}
return result;
}
@Override
public String key() {
return key;
}
}
列如措置惩罚惩罚圆形头像
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class CircleTransformation implements Transformation {
private static final int STROKE_WIDTH = 5;
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size,source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint avatarPaint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);avatarPaint.setShader(shader);
Paint outlinePaint = new Paint();
outlinePaint.setColor(Color.WHITE);
outlinePaint.setStyle(Paint.Style.STROKE);
outlinePaint.setStrokeWidth(STROKE_WIDTH);
outlinePaint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, avatarPaint);
canvas.drawCircle(r, r, r - STROKE_WIDTH / 2, outlinePaint);
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "circle)";
}
}
接着设置衬着模式
1
2
Picasso.with(getApplication()) .fit().centerCrop()
清空缓存
新的版本2.52 已经无法直接拿到之前的cache,因此可以用Picasso.invalidate()的实现清楚缓存!
以前我们可以这样
1
2
Clear.clearCache(Picasso.with(context));
但此刻 不行了
稍加封装成了这样子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void clearCache(Uri uri, File file, String path) {
if (!TextUtils.isEmpty(uri.toString())) {
mPicasso.invalidate(uri);
return;
}
if (!NullUtils.isNull(file)) {
mPicasso.invalidate(file);
return;
}
if (!TextUtils.isEmpty(path)) {
mPicasso.invalidate(path);
}
}
固然也可以这样!
1
2
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30534.html