博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android:强大的图像下载和缓存库Picasso
阅读量:4478 次
发布时间:2019-06-08

本文共 1915 字,大约阅读时间需要 6 分钟。

1.Picasso一个简短的引论

Picasso它是Square该公司生产的一个强大的图像下载并缓存画廊。官方网站:

仅仅须要一句代码就能够将图片下载并设置到ImageView上。

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

2.主要特点

2.1Adapter downloads

使用ListView,GridView的时候,自己主动检測Adapter的重用(re-use),取消下载。使用缓存。

@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.2图像处理与变换

将图像进行变换,以更好的适应布局控件等,减小内存开销。

Picasso.with(context)  .load(url)  .resize(200, 200)  .centerCrop()  .into(imageView)
当然,我们也能够写自己的变换类,可是必须实现Transformation接口,如:

/**	 * 自己定义接口。实现图像缩小为原来的一半	 */	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(this).load("http://i.imgur.com/DvpvklR.png")				.transform(new CropSquareTransformation()).into(iv_test2);
效果图例如以下:

2.3。

支持设置载入之前的图片。和载入失败后的图片。

如:

Picasso.with(this)	    .load("http://i.imgur.com/DvpvklR.png")	    .placeholder(R.drawable.abc)	    .error(R.drawable.ic_launcher)	    .transform(new CropSquareTransformation())	    .into(iv_test1);

ImageView创建时显示abc.png,假设载入成功,显示的是DvpvklR.png。假设载入失败,显示ic_launcher.png.

2.4支持载入本地图片和sdcard中的图片文件等。

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

Picasso下载地址:

它可能不被用于商业目的,未经同意

版权声明:本文博主原创文章。博客,未经同意不得转载。

转载于:https://www.cnblogs.com/zfyouxi/p/4852759.html

你可能感兴趣的文章
boost1.53中的lock-free
查看>>
链表_leetcode203
查看>>
基于ajax 的 几个例子 session ,ajax 实现登录,验证码 ,实现ajax表单展示
查看>>
连接不上sql server服务器的解决方案
查看>>
2013年终总结
查看>>
Start to study Introduction to Algorithms
查看>>
正则表达式
查看>>
Mysql的DATE_FORMAT()日期格式转换
查看>>
SparkStreaming入门及例子
查看>>
Web应用增加struts2支持
查看>>
java程序——凯撒加密
查看>>
Windows Store App之数据存储
查看>>
English class 82 The Importance of traveling
查看>>
python用递归函数解汉诺塔游戏
查看>>
Redis与Python交互
查看>>
Maximum-SubsequenceSum
查看>>
常用的一些shell变量
查看>>
Android无法删除项目+导入项目报错
查看>>
poj 2349(最小生成树应用)
查看>>
python接口自动化测试二十五:执行所有用例,并生成HTML测试报告
查看>>