Picasso加载图片

目前主流的图片加载库有Universal Image Loader,Picasso,Glide,Fresro
其中picassoSquare公司开源的一个Android图形缓存库,可以实现图片下载和缓存功能。

运行效果


一,引入Picasso
新建android studio项目,在module的build.grale添加
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'//引入picasso
}
二, Picasso 不仅实现了图片异步加载的功能,还解决了 android中加载图片时需要解决的一些常见问题:

1.adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解决了这个问题

2.使用复杂的图片压缩转换来尽可能的减少内存消耗

3.自带内存和硬盘二级缓存功能

三,一行代码就可以异步加载图片
    private void initPicasso() {
//加载图片
Picasso.with(this).load(url).into(mIv);
//mIv2控件不能设置成wrap_content,也就是必须有大小才行,fit()才让图片的宽高等于mIv2的宽高,设置fit(),不能再调用resize()
Picasso.with(this).load(url).fit().into(mIv2);
//将加载的图片指定高宽显示
Picasso.with(this).load(url).resize(200, 300).centerCrop().into(mIv3);
//将加载的图片进行截取显示
// Picasso.with(this).load(url).transform(new CropSquareTransformation()).into(mIv3);
//默认显示placeholder,加载错误显示ic_launcher
// Picasso.with(this).load(url).placeholder(R.mipmap.placeholder).error(R.mipmap.ic_launcher).into(mIv4);
//默认显示placeholder,加载错误显示ic_launcher,指定placeholder大小显示
Picasso.with(this).load(url).placeholder(R.mipmap.placeholder).fit().error(R.mipmap.ic_launcher).into(mIv4);
}
自定义裁剪类
public class CropSquareTransformation implements Transformation {

//截取从宽度和高度最小作为bitmap的宽度和高度
@Override
public Bitmap transform(Bitmap source) {
int size=Math.min(source.getWidth(),source.getHeight());
int x=(source.getWidth()-size)/2;
int y=(source.getHeight()-size)/2;
Bitmap result=Bitmap.createBitmap(source,x,y,size,size);
if (result!=source){
source.recycle();//释放bitmap
}
return result;
}

@Override
public String key() {
return "square()";
}
}
说明:除了通过网络下载图片,Picasso还可以载入本地图片资源
Picasso.with(context).load(R.mipmap.ic_launcher).into(imageView);
Picasso.with(context).load(url).into(imageView);
Picasso.wiht(context).load(new File(...)).into(imageView);
四,结合RecyclerView使用Picasso,不会RecyclerView的请点击这里:  

RecyclerView的Adapter万能适配包

mRecyclerview.setAdapter(mAdapter = new JesseAdapter<Pic>(this, R.layout.item_data, mDatas) {
@Override
public void onBindView(JesseHolder holder, int position) {
ImageView iv_data = holder.getView(R.id.iv_data);
// Get the image URL for the current position.
String url = mDatas.get(position).getImg();
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(mContext)
.load(url)
.placeholder(R.mipmap.placeholder)//默认显示
.error(R.mipmap.ic_launcher)//加载错误显示
.fit()//将placeholder高宽作为显示的高宽
.tag(mTag)//设tag,解决错位
.into(iv_data);

TextView tv_name = holder.getView(R.id.tv_name);
tv_name.setText(mDatas.get(position).getName());
}
});
五,优化
1,RecyclerView滑动不加载,滑动停止加载
mRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {//滑动停止的时候加载图片
Picasso.with(mContext).resumeTag(mTag);
} else {//滑动时候不加载图片
Picasso.with(mContext).pauseTag(mTag);
}
}
});
2,设置图片
对于不透明的图片可以使用RGB_565来优化内存。
Picasso.with(context).load(url).config(Bitmap.Config.RGB_565).into(imageView);
默认情况下,Android使用ARGB_8888
ALPHA_8 代表8位Alpha位图
ARGB_4444 代表16位ARGB位图
ARGB_8888 代表32位ARGB位图
RGB_565 代表8位RGB位图
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值