Picasso使用解析

Picasso介绍

Picasso是Square公司开源的Android图形异步加载和缓存框架

Picasso特征:

  • 自带内存和硬盘二级缓存功能
  • 加载本地资源、资产、SD卡及ContentProvider中的图片
  • 在Adapter中需要取消已经不存在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso解决了图片加载错位的问题
  • 使用图片压缩尽可能的减少内存消耗
  • 图片转换操作,如变换大小,旋转等,提供了接口来让用户可以自定义转换操作

Picasso导包

在创建的项目的gradle文件中添加如下代码
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    testCompile 'junit:junit:4.12'
    compile 'com.squareup.picasso:picasso:2.5.2'
}

Picasso基本使用

	//加载资源图片
        Picasso.with(context).load(R.mipmap.ic_launcher).into(imageView);
        //加载资产目录图片
        Picasso.with(context).load("file:///android_asset/ic_launcher.png").into(imageView);
        //加载SDCard图片
        Picasso.with(context).load(new File("path")).into(imageView);
        //加载网络图片
        Picasso.with(context).load("url").into(imageView);

Picasso常见设置

设置占位图片:

Picasso可以设置下载前和下载出错时的图像,在下载出错的图像被设置前,Picasso会尝试三次请求,三次都失败会显示error图片
Picasso.with(context)
                .load("url")
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .into(imageView);

Picasso常用设置:

 Picasso.with(context)
                .load("url")
                .placeholder(R.mipmap.ic_launcher)//下载前的图片
                .error(R.mipmap.ic_launcher)//错误图片
                .noFade()//无淡入淡出
                .resize(width,height)//重新调整图片的大小
                .resizeDimen(widthRes,heightRes)//重新调整图片的大小,从资源文件获取宽高
                //图片剪切   图片剪切的类型有centerCro,centerInside,fit    前两者都要配合resize()使用  fit则不能喝resize使用
                .centerCrop()//按照控件的大小显示,超出部分则被剪切掉
                .centerInside()//按照控件大小按比例压缩图片,使图片全部显示在控件上
                .fit()//将图片完全的填充到控件上

                //自定义图形转换   更好的适配布局   减少存储空间
                .transform(new Transformation() {
                    @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 bitmap = Bitmap.createBitmap(source,x,y,size,size);
                        return bitmap;
                    }

                    @Override
                    public String key() {
                        return "cricle";//返回的自定义图片的名称
                    }
                })
                .rotate(degrees)//图片旋转  参数为旋转角度
                //设置图片质量 RGB_565用来优化内存    默认情况下Android使用的ARGB_8888
                .config(Bitmap.Config.RGB_565)
                //查看大图时放弃内存缓存memory  cache  Picasso默认会使用设备的15%的内存作为内存图片缓存,且现有的API无法清空内存缓存
                //在查看大图时放弃使用内存缓存,图片从网络下载完成后会自动缓存到磁盘中,加载会从磁盘中加载,这样可以加速内存回收
                .memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)//NO_CACHE指图片加载跳过从内存缓存查找,NO_STORE指图片存储时不往内存缓存中存储
                .tag("tag")//设置tag  以方便后面处理 Picasso.with(context).pauseTag("tag");  Picasso.with(context).resumeTag("tag");   Picasso.with(context).cancelTag("tag");
                .into(imageView);

另:新进程中查看大图
列表页的内存已经非常稳定,但是查看大图时,大图占有的内存加上现有进程中的内存非常容易造成OOM,在新进程中打开Activity成为比较取巧的避免OOM的方式
在清单文件中配置
 <activity android:name=".PicActivity"    android:process=":picture"/>
这样既可在新进程中打开此Activity,因此Picasso也将在新的进程中创建基于新ApplicationContext的单例

Picasso在Application中的设置

private void initPicasso() {
        Picasso picasso = new Picasso.Builder(this)
                //设置缓存大小 10M
                .memoryCache(new LruCache(10 << 20))
                //设置图片格式  这样可以节省一半内存
                .defaultBitmapConfig(Bitmap.Config.RGB_565)
                //配置下载器 如果没有配置则默认使用 UrlConnectionDownloader
                //如果使用OkHttpDownloader 则需要单独配置 同时设置磁盘缓存的位置和大小
                .downloader(new OkHttpDownloader(context.getCacheDir(),10<<20))
//                .downloader(new UrlConnectionDownloader(context)
                //设置调试标记  左上角小三角标记  红色代表图片从网络加载
                // 绿色代表图片从内存加载  蓝色代表图片从磁盘加载
                .indicatorsEnabled(true)
                .build();
        //设置单例的Picasso对象
        Picasso.setSingletonInstance(picasso);
    }
将此方法在onCreate()方法中初始化






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值