Android框架之Picasso和Glide的使用及区别

            Picasso  Gilde

1.1行显示ImageView
Picasso.with(context).load(“http://i.imgur.com/DvpvklR.png“).into(imageView);

Many common pitfalls of image loading on Android are handled automatically by Picasso:

-Handling ImageView recycling and download cancelation in an adapter.
-Complex image transformations with minimal memory use.
-Automatic memory and disk caching.

2.加载不同位置的资源

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);


设置尺寸
如果你想对这张图片进行剪裁,可以使用resize方法:

Picasso.with(this).load("http://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg")  
                .resize(200,200)  
                .into(iv);  

注意这里的200表示200px,如果你想在resize时指定dp,可以使用如下方法:


Picasso.with(this).load("http://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg")  
                .resizeDimen(R.dimen.iv_width,R.dimen.iv_height)  
                .into(iv);  

3.自定义Picasso

Picasso picasso = new Picasso.Build(Context).build();

 .memoryCache(new LruCache(10*1024*1024)).defaultBitmapConfig(Bitmap.Config.RGB_565)
 .downloader(new OkHttpDownloader(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),10*1024*1024))
 .indicatorsEnabled(true)  //设置图片来源,给予指示!

4.Picasso常用属性

config(Bitmap.Config.RGB_565)//设置格式
 .memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE) //不缓存 也不在内存中获取
 .resize(100,100) .centerInside().placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).rotate(40).
 transform(new Transformation() {
 @Override

// public Bitmap transform(Bitmap source) {
// int max = Math.min(source.getHeight(), source.getWidth());
// Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, max, max);
// source.recycle();
// return bitmap;
// }
//
// @Override
// public String key() {
// return “key”;
// }
// })

Transformation transformation = new Transformation() {  
            @Override  
            public Bitmap transform(Bitmap source) {  
                int width = source.getWidth();  
                int height = source.getHeight();  
                int size = Math.min(width, height);  
                Bitmap blankBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);  
                Canvas canvas = new Canvas(blankBitmap);  
                Paint paint = new Paint();  
                paint.setAntiAlias(true);  
                canvas.drawCircle(size / 2, size / 2, size / 2, paint);  
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
                canvas.drawBitmap(source, 0, 0, paint);  
                if (source != null && !source.isRecycled()) {  
                    source.recycle();  
                }  
                return blankBitmap;  
            }  

            @Override  
            public String key() {  
                return "squareup";  
            }  
        };  
Picasso高级部分,自定义Picasson文件缓存位置!!!

downloader(new OkHttpDownloader(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),10*1024*1024))

Picasso

 介绍: 
        一个快速加载图片的框架!内部支持!三级缓存,图片的防错位功能


    注意:
         Picasso不能在子线程中进行调用!


     1.默认的缓存路径  File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE);

     2.默认的图片格式  ARGB 8888

     picasso不能加载gif

     Picasso.with(Context)


    自定义Picasso 

     Picasso picasso = new Picasso.Builder(this)
     .defaultBitmapConfig(Bitmap.Config.RGB_565)
     .downloader(new OkHttpDownloader(Environment.getExternalStorageDirectory()))
     .build();


       Glide

你可能感兴趣的:(Android框架之Picasso和Glide的使用及区别)