Picasso的使用

1.在项目里面引用jar包
加入jar包

2.代码实现
编写一个简单的demo,可以是listview
引入代码

特性以及示例代码:
ADAPTER 中的下载:Adapter的重用会被自动检测到,Picasso会取消上次的加载

1   @Override 
2   public void getView(int position, View convertView, ViewGroup parent) {
3     SquaredImageView view = (SquaredImageView) convertView;
4     if (view == null) {
5       view = new SquaredImageView(context);
6     }
7     String url = getItem(position);
8     Picasso.with(context).load(url).into(view);
    }

图片转换:转换图片以适应布局大小并减少内存占用

 1  Picasso.with(context)
2     .load(url)
3     .resize(50, 50)
4     .centerCrop()
5     .into(imageView);

 ```
 将CropSquareTransformation 的对象传递给transform 方法即可。


 ```
 1  public class CropSquareTransformation implements Transformation {
2     @Override 
3   public Bitmap transform(Bitmap source) {
4       int size = Math.min(source.getWidth(), source.getHeight());
5       int x = (source.getWidth() - size) / 2;
6       int y = (source.getHeight() - size) / 2;
7       Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
8       if (result != source) {
9         source.recycle();
10      }
11      return result;
12    }
13    @Override 
    public String key() { 
    return "square()"; 
      }
    }

Place holders-空白或者错误占位图片:picasso提供了两种占位图片,未加载完成或者加载发生错误的时需要一张图片作为提示。

1   Picasso.with(context)
2       .load(url)
3       .placeholder(R.drawable.user_placeholder)
4       .error(R.drawable.user_placeholder_error)
5   .into(imageView);

对于不透明的图片可以使用RGB_565来优化内存。

Picasso.with( imageView.getContext() )
.load(url)
.config(Bitmap.Config.RGB_565)
.into(imageView);

默认情况下,Android使用ARGB_8888
Android中有四种,分别是:
ALPHA_8:每个像素占用1byte内存
ARGB_4444:每个像素占用2byte内存
ARGB_8888:每个像素占用4byte内存
RGB_565:每个像素占用2byte内存

Picasso使用的方法汇总:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Picasso.with(context).load(url).into(view);

Picasso.with(context).load(url).resize(50,50).centerCrop().into(imageView)

//这里的placeholder将resource传入通过getResource.getDrawable取资源,所以可以是张图片也可以是color id
Picasso.with(context).load(url).placeholder(R.drawable.user_placeholder).error(R.drawable.user_placeholder_error).into(imageView);

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);

Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);

//资源文件下载 除了加载网络图片picasso还支持加载Resources, assets, files, content providers中的资源文件。
Picasso.with(context).load(new File(...)).into(imageView3);

//这里显示notification的图片
Picasso.with(activity).load(Data.URLS[new Random().nextInt(Data.URLS.length)]).resizeDimen(R.dimen.notification_icon_width_height,R.dimen.notification_icon_width_height).into(remoteViews, R.id.photo, NOTIFICATION_ID, notification);

//这里是通过设置tag标签,就是当前传过来的context,这样就可以根据这个context tag来pause和resume显示了
Picasso.with(context).load(url).placeholder(R.drawable.placeholder).error(R.drawable.error).fit().tag(context).into(view);

//监听onScrollStateChanged的时候调用执行
picasso.resumeTag(context);
picasso.pauseTag(context);
Picasso.with(context).load(contactUri).placeholder(R.drawable.contact_picture_placeholder).tag(context).into(holder.icon);



//这个onpause方法里的这段代码还是很有意思的
@Override protected void onPause() {
    super.onPause();
    if (isFinishing()) {

      // Always cancel the request here, this is safe to call even if the image has been loaded.
      // This ensures that the anonymous callback we have does not prevent the activity from
      // being garbage collected. It also prevents our callback from getting invoked even after the
      // activity has finished.

      Picasso.with(this).cancelRequest(imageView);
    }
  }

// Trigger the download of the URL asynchronously into the image view.

    Picasso.with(context)
        .load(url)
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.error)
        .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
        .centerInside()
        .tag(context)
        .into(holder.image);

//Picasso.with使用的是单例模式

Picasso.with(this).cancelTag(this);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值