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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值