一、简介
Picasso是Square公司出品的一个强大的图片下载和缓存图片库
1)在adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解决了这个问题。
2)使用复杂的图片压缩转换来尽可能的减少内存消耗
3)自带内存和硬盘二级缓存功能
二、下载地址
点击下载链接
三、功能
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);
四、使用步骤
(1)导入jar包或在module的gradle文件中添加
compile 'com.squareup.picasso:picasso:2.5.2'
五、例子
1)基本用法
普通加载
Picasso.with(PicassoActivity.this)
.load("http://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg")
.into(ivPicassoResult1);
裁剪
// 裁剪的方式加载图片
Picasso.with(PicassoActivity.this)
.load("http://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg")
.resize(100,100)
.into(ivPicassoResult2);
旋转180度
// 选择180度
Picasso.with(PicassoActivity.this)
.load("http://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg")
.rotate(180)
.into(ivPicassoResult3);
2)listview中使用
// 初始化listview
PicassoListviewAdapter picassoListviewAdapter = new PicassoListviewAdapter(this);
lvPicasso.setAdapter(picassoListviewAdapter);
public class PicassoListviewAdapter extends BaseAdapter {
private Context mContext;
public PicassoListviewAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return Constants.IMAGES.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = View.inflate(mContext, R.layout.item_picasso_listview,null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
// 名称
holder.name.setText("item"+(position+1));
// 加载图片
Picasso.with(mContext)
.load(Constants.IMAGES[position])
.placeholder(R.drawable.atguigu_logo)
.error(R.drawable.atguigu_logo)
.into(holder.iv);
return convertView;
}
class ViewHolder{
@Bind(R.id.iv_picasso_item)
ImageView iv;
@Bind(R.id.tv_picasso_name)
TextView name;
public ViewHolder(View view) {
ButterKnife.bind(this,view);
}
}
}
3)转换案例
private void initData() {
// 标题
tvTitle.setText("Picasso的转换操作");
List data = new ArrayList<>();
for (int i = 1; i<= 36; i++){
data.add(i+"");
}
// 初始化listview
PicassoTransformationsAdapter picassoTransformationsAdapter = new PicassoTransformationsAdapter(PicassoTransfromationsActivity.this,data);
lvPicassoTransfromations.setAdapter(picassoTransformationsAdapter);
}
public class PicassoTransformationsAdapter extends BaseAdapter {
private Context mContext;
private List mData;
public PicassoTransformationsAdapter(Context context, List data) {
mContext = context;
mData = data;
}
@Override
public int getCount() {
return mData == null ? 0 : mData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = View.inflate(mContext, R.layout.item_picasso_transformations,null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
// 显示名称
holder.name.setText("item"+(position + 1));
int integer = Integer.parseInt(mData.get(position));
switch (integer) {
case 1: {
int width = Utils.dip2px(mContext, 133.33f);
int height = Utils.dip2px(mContext, 126.33f);
Picasso.with(mContext)
.load(R.drawable.check)
.resize(width, height)
.centerCrop()
.transform((new MaskTransformation(mContext, R.drawable.mask_starfish)))
.into(holder.image);
break;
}
case 2: {
int width = Utils.dip2px(mContext, 150.0f);
int height = Utils.dip2px(mContext, 100.0f);
Picasso.with(mContext)
.load(R.drawable.check)
.resize(width, height)
.centerCrop()
.transform(new MaskTransformation(mContext, R.drawable.chat_me_mask))
.into(holder.image);
break;
}
case 3:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(300, 100, CropTransformation.GravityHorizontal.LEFT,
CropTransformation.GravityVertical.TOP))
.into(holder.image);
break;
case 4:
Picasso.with(mContext).load(R.drawable.demo)
// 300, 100, CropTransformation.GravityHorizontal.LEFT, CropTransformation.GravityVertical.CENTER))
.transform(new CropTransformation(300, 100)).into(holder.image);
break;
case 5:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(300, 100, CropTransformation.GravityHorizontal.LEFT,
CropTransformation.GravityVertical.BOTTOM))
.into(holder.image);
break;
case 6:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(300, 100, CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.TOP))
.into(holder.image);
break;
case 7:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(300, 100))
.into(holder.image);
break;
case 8:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(300, 100, CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.BOTTOM))
.into(holder.image);
break;
case 9:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(300, 100, CropTransformation.GravityHorizontal.RIGHT,
CropTransformation.GravityVertical.TOP))
.into(holder.image);
break;
case 10:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(300, 100, CropTransformation.GravityHorizontal.RIGHT,
CropTransformation.GravityVertical.CENTER))
.into(holder.image);
break;
case 11:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(300, 100, CropTransformation.GravityHorizontal.RIGHT,
CropTransformation.GravityVertical.BOTTOM))
.into(holder.image);
break;
case 12:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation((float) 16 / (float) 9,
CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.CENTER))
.into(holder.image);
break;
case 13:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation((float) 4 / (float) 3,
CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.CENTER))
.into(holder.image);
break;
case 14:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(3, CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.CENTER))
.into(holder.image);
break;
case 15:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(3, CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.TOP))
.into(holder.image);
break;
case 16:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation(1, CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.CENTER))
.into(holder.image);
break;
case 17:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation((float) 0.5, (float) 0.5,
CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.CENTER))
.into(holder.image);
break;
case 18:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation((float) 0.5, (float) 0.5,
CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.TOP))
.into(holder.image);
break;
case 19:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation((float) 0.5, (float) 0.5,
CropTransformation.GravityHorizontal.RIGHT,
CropTransformation.GravityVertical.BOTTOM))
.into(holder.image);
break;
case 20:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropTransformation((float) 0.5, 0, (float) 4 / (float) 3,
CropTransformation.GravityHorizontal.CENTER,
CropTransformation.GravityVertical.CENTER))
.into(holder.image);
break;
case 21:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropSquareTransformation())
.into(holder.image);
break;
case 22:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new CropCircleTransformation())
.into(holder.image);
break;
case 23:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new ColorFilterTransformation(Color.argb(80, 255, 0, 0)))
.into(holder.image);
break;
case 24:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new GrayscaleTransformation())
.into(holder.image);
break;
case 25:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new RoundedCornersTransformation(30, 0,
RoundedCornersTransformation.CornerType.BOTTOM_LEFT))
.into(holder.image);
break;
case 26:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new BlurTransformation(mContext, 25, 1))
.into(holder.image);
break;
case 27:
Picasso.with(mContext)
.load(R.drawable.demo)
.transform(new ToonFilterTransformation(mContext))
.into(holder.image);
break;
case 28:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new SepiaFilterTransformation(mContext))
.into(holder.image);
break;
case 29:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new ContrastFilterTransformation(mContext, 2.0f))
.into(holder.image);
break;
case 30:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new InvertFilterTransformation(mContext))
.into(holder.image);
break;
case 31:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new PixelationFilterTransformation(mContext, 20))
.into(holder.image);
break;
case 32:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new SketchFilterTransformation(mContext))
.into(holder.image);
break;
case 33:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
.into(holder.image);
break;
case 34:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new BrightnessFilterTransformation(mContext, 0.5f))
.into(holder.image);
break;
case 35:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new KuwaharaFilterTransformation(mContext, 25))
.into(holder.image);
break;
case 36:
Picasso.with(mContext)
.load(R.drawable.check)
.transform(new VignetteFilterTransformation(mContext, new PointF(0.5f, 0.5f),
new float[]{0.0f, 0.0f, 0.0f}, 0f, 0.75f))
.into(holder.image);
break;
}
return convertView;
}
class ViewHolder{
@Bind(R.id.iv_picasso)
ImageView image;
@Bind(R.id.tv_picasso)
TextView name;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
工具类
public class Utils {
public static int dip2px(Context context, float dp) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
}
提供一个工具类
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 height, ImageView imgView){
Picasso.with(context)
.load(imgurl)//加载网络图片的url
.config(Bitmap.Config.RGB_565)//8位RGB位图
.placeholder(resId)//默认图片
.error(resId)//加载错误的图片
.resize(weight,height)//设置图片的宽高
.into(imgView);//把图片加载到控件上
}
}