Android 图片加载框架之Picasso

    一、简介

        Picasso是Square公司出品的一个强大的图片下载缓存图片
        1)在adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解决了这个问题。
        2)使用复杂的图片压缩转换来尽可能的减少内存消耗
        3)自带内存和硬盘二级缓存功能


    二、下载地址

        https://github.com/square/picasso


    三、功能

        1)基本用法

            Picasso.with(context).load(imageUrl).into(imageView);

        2)图片路径load()

            load SD卡资源:load("file://"+ Environment.getExternalStorageDirectory().getPath()+"/test.jpg")
            load assets资源:load("file:///android_asset/f003.gif") 
            load drawable资源:load("android.resource://com.frank.glide/drawable/news")
            load http资源:load("http:www.123.com/1234567890.jpg") 

        3)资源加载的方法

            - placeholder(xxx). 设置资源加载过程中的显示的Drawable。
            - error(xxx).设置load失败时显示的Drawable。
            - into(xxx) 设置资源加载到的目标 包括ImageView Target等

        4)图片裁剪

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

        5)工具类

public class PicassoUtil {
    //加载本地图片
    public static void setImg(Context context, int resId, ImageView imgView){
        Picasso.with(context)
                .load(resId)
                .config(Bitmap.Config.RGB_565)//8位RGB位图
                .fit()
                .into(imgView);
    }
    //按照一定的宽高加载本地图片,带有加载错误和默认图片
    public static void setImg(Context context,int resId,ImageView imgView,int weight,int height){
        Picasso.with(context)
                .load(resId)//加载本地图片
                .config(Bitmap.Config.RGB_565)//8位RGB位图
                .resize(weight,height)//设置图片的宽高
                .into(imgView);//把图片加载到控件上
    }
    //加载网络图片到imgview,带有加载错误和默认图片
    public static void setImg(Context context, String imgurl, int resId, ImageView imgView){
        Picasso.with(context)
                .load(imgurl)//加载网络图片的url
                .config(Bitmap.Config.RGB_565)//8位RGB位图
                .placeholder(resId)//默认图片
                .error(resId)//加载错误的图片
                .fit()//图片的宽高等于控件的宽高
                .into(imgView);//把图片加载到控件上
    }
    public static void setImg(Context context, String imgurl, ImageView imgView){
        Picasso.with(context)
                .load(imgurl)//加载网络图片的url
                .config(Bitmap.Config.RGB_565)//8位RGB位图
                .fit()//图片的宽高等于控件的宽高
                .into(imgView);//把图片加载到控件上
    }
    //加载网络图片到Viewpager
    public static void setImg(Context context, String imgurl, ViewPager imgView){
        Picasso.with(context)
                .load(imgurl)//加载网络图片的url
                .config(Bitmap.Config.RGB_565)//8位RGB位图
                .fit()//图片的宽高等于控件的宽高
                .into((Target) imgView);//把图片加载到控件上
    }
    //加载网络图片到Viewpager,带有加载错误和默认图片
    public static void setImg(Context context, String imgurl, int resId, ViewPager imgView){
        Picasso.with(context)
                .load(imgurl)//加载网络图片的url
                .config(Bitmap.Config.RGB_565)//8位RGB位图
                .placeholder(resId)//默认图片
                .error(resId)//加载错误的图片
                .fit()//图片的宽高等于控件的宽高
                .into((Target) imgView);//把图片加载到控件上
    }
    //按照设定的宽高加载网络图片到imgview
    public static void setImg(Context context, String imgurl,ImageView imgView,int weight,int height){
        Picasso.with(context)
                .load(imgurl)//加载网络图片的url
                .config(Bitmap.Config.RGB_565)//8位RGB位图
                .resize(weight,height)//设置图片的宽高
                .into(imgView);//把图片加载到控件上
    }
    //按照设定的宽高加载网络图片到imgview,带有加载错误和默认图片
    public static void setImg(Context context, String imgurl, int resId,int weight,int 
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值