Picasso加载图片

您所在的位置:网站首页 listview图片错位 Picasso加载图片

Picasso加载图片

2023-05-08 22:56| 来源: 网络整理| 查看: 265

目前主流的图片加载库有Universal Image Loader,Picasso,Glide,Fresro

其中picasso是Square公司开源的一个Android图形缓存库,可以实现图片下载和缓存功能。

官方地址: http://square.github.io/picasso/

github地址: https://github.com/square/picasso

运行效果

Picasso加载图片_图片加载

一,引入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的Adapter万能适配包

mRecyclerview.setAdapter(mAdapter = new JesseAdapter(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); } } });

对于不透明的图片可以使用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位图

源码下载



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3