Picasso是Android中强大的图像下载和缓存库。
Picasso Introduction(介绍):
在Android应用程序中图片的添加有必须的环境和良好的视觉风格,Picasso可以很轻松的加载图片在你的应用程序中-通常在一行代码中。- Picasso.with(context).load(“http://i.imgur.com/DvpvklR.png”).into(imageView);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
1.处理图片回收和下载取消在Adapter中。
2.复杂的图像转换以最小的内存使用。
3.自动存储和磁盘缓存。
Picass Features(特点):
1.ADAPTER DOWNLOADS(适配器中加载):
适配器自动发现和重用以前的取消下载。
- @Override public void getView(int position, View convertView, ViewGroup parent) {
- SquaredImageView view = (SquaredImageView) convertView;
- if (view == null) {
- view = new SquaredImageView(context);
- }
- String url = getItem(position);
- Picasso.with(context).load(url).into(view);
- }
@Override public void getView(int position, View convertView, ViewGroup parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);
}
String url = getItem(position);
Picasso.with(context).load(url).into(view);
}
2.IMAGE TRANSFORMATIONS(图片转换):
转换图片以更好地适应到布局和减少内存的大小。- Picasso.with(context)
- .load(url)
- .resize(50, 50)
- .centerCrop()
- .into(imageView)
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
您还可以指定为更高级的效果自定义转换。
- public class CropSquareTransformation implements Transformation {
- @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();
- }
- return result;
- }
- @Override public String key() { return “square()”; }
- }
public class CropSquareTransformation implements Transformation {
@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();
}
return result;
}
@Override public String key() { return "square()"; }
}
该类的实例传递给transform方法。
- Picasso.with(context)
- .load(url)
- .placeholder(R.drawable.user_placeholder)
- .error(R.drawable.user_placeholder_error)
- .into(imageView);
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
一个要求请求将重试三次之后错误的占位符被显示。
4.RESOURCE LOADING(资源加载)
Resources, assets, files, content providers都支持作为图像源。
- Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
- Picasso.with(context).load(”file:///android_asset/DvpvklR.png”).into(imageView2);
- Picasso.with(context).load(new File(…)).into(imageView3);
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
5.DEBUG INDICATORS()
对于开发可以启用彩色指示的图像源的显示,在Picasso实例上调用setIndicatorsEnabled(true)。红色:代表从网络中获取。
蓝色:代表从磁盘中获取。
绿色:代表从缓存中获取。
6.Download(下载)
Picasso的源码在GitHub中:https://github.com/square/picasso
引入
MAVEN:
- <dependency>
- <groupId>com.squareup.picasso</groupId>
- <artifactId>picasso</artifactId>
- <version>2.5.2</version>
- </dependency>
<dependency>
<groupId>com.squareup.picasso</groupId>
<artifactId>picasso</artifactId>
<version>2.5.2</version>
</dependency>
GRADLE
- compile ‘com.squareup.picasso:picasso:2.5.2’
compile 'com.squareup.picasso:picasso:2.5.2'
使用例子
根据前面的介绍,简单的加载一张图片很简单,那就写一个用ListView加载图片的情况吧。
- public class MainActivity extends AppCompatActivity {
- String urlPicasso = ”http://img1.imgtn.bdimg.com/it/u=998216620,3976144567&fm=21&gp=0.jpg”;
- String urlGlide = ”http://h.hiphotos.baidu.com/image/h%3D200/sign=cd65e7fa13d5ad6eb5f963eab1cb39a3/377adab44aed2e7394aa5a128f01a18b87d6fa49.jpg”;
- String urlVideo = ”http://www.iqiyi.com/v_19rrm83ayw.html”;
- ListView mListView;
- MyAdapter mAdpater;
- List<String> mData = new ArrayList<>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mListView = (ListView) findViewById(R.id.lv);
- for (int i = 0; i < 20; i++) {
- mData.add(urlGlide);
- }
- mAdpater = new MyAdapter();
- mListView.setAdapter(mAdpater);
- }
- class MyAdapter extends BaseAdapter {
- @Override
- public int getCount() {
- return mData.size();
- }
- @Override
- public Object getItem(int position) {
- return mData.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder = new ViewHolder();
- convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.image_item, null);
- holder.mImageView = (ImageView) convertView.findViewById(R.id.id_image);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- Picasso.with(MainActivity.this).load(mData.get(position)).into(holder.mImageView);
- return convertView;
- }
- class ViewHolder {
- ImageView mImageView;
- }
- }
- }
public class MainActivity extends AppCompatActivity {
String urlPicasso = "http://img1.imgtn.bdimg.com/it/u=998216620,3976144567&fm=21&gp=0.jpg";
String urlGlide = "http://h.hiphotos.baidu.com/image/h%3D200/sign=cd65e7fa13d5ad6eb5f963eab1cb39a3/377adab44aed2e7394aa5a128f01a18b87d6fa49.jpg";
String urlVideo = "http://www.iqiyi.com/v_19rrm83ayw.html";
ListView mListView;
MyAdapter mAdpater;
List<String> mData = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.lv);
for (int i = 0; i < 20; i++) {
mData.add(urlGlide);
}
mAdpater = new MyAdapter();
mListView.setAdapter(mAdpater);
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.image_item, null);
holder.mImageView = (ImageView) convertView.findViewById(R.id.id_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(MainActivity.this).load(mData.get(position)).into(holder.mImageView);
return convertView;
}
class ViewHolder {
ImageView mImageView;
}
}
}
这是使用Picasso在ListView中简单的加载图片,布局中ImageView的宽高设置为自适应,下面是显示的效果。
下面通过resize()方法改变图片的尺寸,图片会根据设置的值改变大小,效果如下。
- Picasso.with(MainActivity.this).load(mData.get(position)).resize(200, 200).into(holder.mImageView);
Picasso.with(MainActivity.this).load(mData.get(position)).resize(200, 200).into(holder.mImageView);
下面自定义Transformation,实现图片的转换。
- class MyTransfrom implements Transformation {
- @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();
- }
- return result;
- }
- @Override
- public String key() {
- return “key”;
- }
- }
class MyTransfrom implements Transformation {
@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();
}
return result;
}
@Override
public String key() {
return "key";
}
}
这里需要注意,返回的key一定要设置,不然会报错。
通过transform()方法设置,下面是运行的效果。
- MyTransfrom transfrom = new MyTransfrom();
- Picasso.with(MainActivity.this).load(mData.get(position)).transform(transfrom).into(holder.mImageView);
MyTransfrom transfrom = new MyTransfrom();
Picasso.with(MainActivity.this).load(mData.get(position)).transform(transfrom).into(holder.mImageView);
通过比较可以发现,图片的大小发生了改变,picasso-transformations是一个自定义的transform,库提供了多种图像变换。
可以设置memoryPolicy()方法指定内存缓存的策略。
- Picasso.with(MainActivity.this).load(mData.get(position)).memoryPolicy(MemoryPolicy.NO_CACHE).transform(transfrom).into(holder.mImageView);
Picasso.with(MainActivity.this).load(mData.get(position)).memoryPolicy(MemoryPolicy.NO_CACHE).transform(transfrom).into(holder.mImageView);
还有很多没有使用到,先就这么多。