Picasso使用和Glide的区别

Picasso使用 和 Glide 的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*** 初始化单例Picasso对象*/
private void initPicasso() {
//配置Picasso
Picasso picasso = new Picasso.Builder(this)
//设置内存缓存大小,10M
.memoryCache(new LruCache(10 << 20))
//下载图片的格式,这样可以节省一半的内存
.defaultBitmapConfig(Bitmap.Config.RGB_565)
//配置下载器,这里用的是OkHttp,必需单独加OkHttp,同时设置了磁盘缓存的位置和大小
//.downloader(new UrlConnectionDownloader())
//.downloader(new OkHttpDownloader(getCacheDir(), 20 << 20))
.downloader(new MyOkHttpDownloader(this.getCacheDir() , 10<<20))
//设置图片左上角的标记
//红色:代表从网络下载的图片
//蓝色:代表从磁盘缓存加载的图片
//绿色:代表从内存中加载的图片
.indicatorsEnabled(true)
//.loggingEnabled(true)
.build();
Picasso.setSingletonInstance(picasso);
}

加载网络图片

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
Picasso.with(context).load(www.xxxxx.com).into(imageview)
.placeholder(R.drwable.ic).error(R.drawable.sdsd)加载时和加载失败
.noFade()无淡入淡出
.resize(width,height)重新调整大小
.resizeDimen(targetWidthResld,targetHeigthResld)//根据资源id拿到高度和宽度
.centerCrop 配合resize 异常
.fit 不能resize
.centerInside 配合resize异常
.rotate (90)旋转角度
.config(Bitmap.Config.RGB_565)
.transform(new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
Bitmap bm = Bitmap.createBitmap(source, 0, 0, size, size);//原始bitmap对象 原始图片x y 坐标起始点 x y 延伸尺寸 多余的被裁切
if(bm!=source){
source.recycle()//被裁切 回收
return bm;
}
@Override
public String key() {
return "square”;//返回transform的名称
}
}).memoryPolicy(MemoryPolicy.NO_CACAHE,MemPolicy.NO_STORE)

NO_CACAHE 是指图片加载跳过内存缓存查找 NO_STORE是指 图片存储时不往内存缓存中储存

设置tag

1
2
3
4
5
6
7
8
9
10
11
12
tag.(“main")
Picasso.with(mContext).cancelTag("json”);//终止
Picasso.with(this),pauseTag(“");
查看大图的时候在seebigpicActivity de AndroidManifest.xml 加上
android:process = “picture”在新进程中打开
加载资源文件
Picasso.with(context).load(R.drable.ic_lunch).into(imageview)
加载资产文件
Picasso.with(context).load(“file:///android_asset/im.png").into(imageview)
加载sd卡图片
Picasso.with(context).load(new File(filePath)).into(imageview)

picasso和glide的区别

1 with()参数不同

picasso 只能context

glide可以activity he fragment

2 默认图片质量不一样

picasso ARGB_8888

glide RGB_565

3 glide支持 GIF动态图片

4 缓存策略不同

picasso缓存的是全尺寸的图片

glide会为每种尺寸大小的imageview缓存一遍

加载库设置 导入 glide.okhttp.intregration网络请求集成包
初始化glide

1
2
3
glide.get(thsi).register(GlideUrl.class,InStream.class,new OKhttpLoader.Factor(OkhttpUtils.getokHttpSingletonInstance()));
```
glide设置图片加载监听器

into(new GlideDrawableImageViewTarget(image view){重写 onloadFailed onLoadStarted onResourceReady})

1
2
Glide清除内存缓存

Glide.get(Context).clearMemory();
new thread(new Runnable{
public void run(){
Glide.get(Context).cleardisCache();

}).start();

1
2
3
4
5
### 自定义设置缓存空间
在application里

MemoryCalculator ca = new MemoryCalculator(this);
ca.getMemoryCacheSize();
ca.getBitmapPoolSize();

1
2
自定义GlideModuel
重写

applyOption(Context context,GlideBuilder bulider);
builder.setDecodeFormat(DecodeFormat.PREFER_AGRB_8888);
int cacheSize = 10<<20; builder.setdiscache(new="" internalcachediskcachefactor(context,cachesize))="" 内部缓存="" new="" externalcachediskcachefactor(context,cachesize))="" 外部缓存=""

1
2

registerComponents((Context context,Glide glide);

1
2
然后在manifest中在application中加入

1
2
3
4
5
6
## transform旋转图片
transform(new 自定义的bitmapTransformation )
重写 transform()

getId(){
return get class.getname()

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
//图形转换的id给本图形转换起名字
public class BitmapRotateTransformation extends BitmapTransformation {
private float rotateRotationAngle = 0f;
public BitmapRotateTransformation(Context context, float rotateRotationAngle) {
super(context);
this.rotateRotationAngle = rotateRotationAngle;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Matrix matrix = new Matrix();
matrix.postRotate(rotateRotationAngle);
Bitmap result = Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
result.setConfig(Bitmap.Config.ARGB_8888);
}
return result;
}
@Override
public String getId() {
return getClass().getName();
}
}

实现圆角

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
39
40
41
public class BitmapCircleTransformation extends BitmapTransformation {
public BitmapCircleTransformation(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool , toTransform);
}
private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
//创建一个空白Bitmap,将在该Bitmap上铺设画布进行绘图
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_4444);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setAntiAlias(true);
//选择原图中的中心矩形,绘制在画布上
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override
public String getId() {
return getClass().getName();
}
}