https://github.com/square/picasso
基本用法
// 基本用法 // 普通加载图片 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度 Picasso.with(PicassoActivity.this) .load("http://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg") .rotate(180) .into(ivPicassoResult3);
ListView中使用 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); } } }
图片transform
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'jp.wasabeef:picasso-transformations:2.1.0'
// If you want to use the GPU Filters
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'
dip单位转像素px 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 PicassoTransformationsAdapter extends BaseAdapter { private Context mContext; private List<String> mData; public PicassoTransformationsAdapter(Context context, List<String> 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); } } }