Picasso Android图片第三方框架的使用

Picasso的使用非常的方便,只需要添加一个jar包,调用其方法便可使用。

Picasso官方下载地址:

http://square.github.io/picasso/#download

使用一:最简单的加载图片

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

方法很简单,此处resource的类型可以是File,int,Uri,String

使用二:对图片进行裁剪

Picasso.with(context).load(url).resize(width,height).centerCrop().into(imageView);

对图片进行适当的剪裁,对防止OOM有着不错的作用。

使用三:设置图片的色彩模式

Picasso.with(context).load(file).config(Bitmap.Config.RGB_565).into(imageView)

一般设置成565,这样图片所占内存就会减小,但是效果却差不多

使用四:设置圆角图片

Picasso方便的让我们实现了Transformation方法,我们可通过这个类来对自己的Bitmap进行合乎自己需求的操作,设置比如一个圆角的图片。

		Picasso.with(context).load(resId).transform(new Transformation() {
			@Override
			public Bitmap transform(Bitmap source) {
				float roundPx = 23.0f;//角度大小
				Bitmap output = Bitmap.createBitmap(source.getWidth(),
						source.getHeight(), Config.ARGB_8888);
				Canvas canvas = new Canvas(output);
				final int color = 0xff424242;
				final Paint paint = new Paint();
				final Rect rect = new Rect(0, 0, source.getWidth(),source.getHeight());
				final RectF rectF = new RectF(rect);
				paint.setAntiAlias(true);
				canvas.drawARGB(0, 0, 0, 0);
				paint.setColor(color);
				canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
				paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
				canvas.drawBitmap(source, rect, rect, paint);
				source.recycle();
				return output;
			}

			@Override
			public String key() {
				return "corner()";
			}
		}).into(imageView);

实际效果:

使用五:设置加载中、加载失败图片

Picasso.with(context).load(path).placeholder(R.drawable.loading).error(R.drawable.no_photo).centerCrop().into(imageView);



通常,我在使用的时候为了为了能够有效的避免OOM的问题,我会对图片进行裁剪,设置RGB565操作。

比如Listview中的使用:

class AdapterMessage extends BaseAdapter {
		private LayoutInflater inflater;
		private Context context;

		class ViewHolder {
			ImageView ivhead;
			int width;
			int height;
		}

		public AdapterMessage(Context ct) {
			this.inflater = LayoutInflater.from(ct);
			context = ct;

		}

		public int getCount() {
			return lstFriends.size();
		}

		public Object getItem(int position) {
			return lstFriends.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder vh = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.message_item, null);
				vh = new ViewHolder();
				vh.ivhead = (ImageView) convertView.findViewById(R.id.head_image);
				RelativeLayout.LayoutParams params = (LayoutParams) vh.ivhead.getLayoutParams();
				vh.width = params.width;
				vh.height = params.height;
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}
			Picasso.with(context).load(R.id.img).resize(vh.width, vh.height)
					.centerCrop().config(Bitmap.Config.RGB_565).into(vh.ivhead);
			return convertView;
		}
	}


 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值