android picasso源码下载,Picasso:一个专为Android制作的强大的图片下载和缓存库

Picasso:一个专为Android打造的强大的图片下载和缓存库

简介

在Android应用中,图片消费了大量的资源,却为应用提供了很好的视觉体验。幸运的是,Picasso为你的应用提供了非常容易的图片加载方式——通常一行代码就可以搞定!

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

Picasso处理了Android上图片加载的许多坑:

1)在Adapter中,处理了ImageView的循环利用和取消下载。

2)耗费最小的内存处理了复杂的图形变换。

3)在内存和磁盘中自动缓存图片。

011728310.png

特点

Adapter中的下载

自动检测adapter中的重用功能,且一旦发现重用,将自动取消之前的下载。

@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);

}

图片转换

变换图片以便更好的适应布局,同时也减少了内存的使用。

Picasso.with(context)

.load(url)

.resize(50, 50)

.centerCrop()

.into(imageView)

你也可以自定义图片的转换方式以达到更复杂的变换要求。

public class CropSquareTransformation implements Transformation {

@Override public Bitmap transform(Bitmapsource) {

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()"; }

}

将CropSquareTransformation这个类的一个实例传递给transform()函数即可。

占位图片

Picasso同时支持“正在下载”时和“图片下载出错”后这两种状态展示的默认图片。

Picasso.with(context)

.load(url)

.placeholder(R.drawable.user_placeholder)

.error(R.drawable.user_placeholder_error)

.into(imageView);

Picasso至多会尝试三次下载,如果三次下载都失败了,就会在原图位置上展示“图片下载出错”时的图片。

资源加载

Picasso支持将resources、assets、files和contentprovider作为图片的加载源。

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(newFile(...)).into(imageView3);

调试指示器

开发者可以在图片的左上角展示一个彩色的小三角,不同的颜色指明了图片资源的不同来源。调用Picasso对象的setIndicatorsEnabled(true)方法就可以了。

011728311.png

下载

http://repo1.maven.org/maven2/com/squareup/picasso/picasso/2.5.2/picasso-2.5.2.jar

Picasso的源代码,例子和这个网址(http://square.github.io/picasso/)都放在了GitHub上

MAVEN

com.squareup.picasso

picasso

2.5.2

GRADLE

compile 'com.squareup.picasso:picasso:2.5.2'

为Picasso做贡献

如果你想为Picasso开发贡献你的代码,你可以上github,然后fork该代码库,再把你的修改发给我们(pull request)。

在提交代码的时候,请保持代码的习惯和风格与原来的一致以便于尽可能保持代码的可阅读性。同时,为了保证你的代码正确编译,请运行mvn clean verify。

你需要同意Individual ContributorLicense Agreement (CLA)才能将你的代码合并到Picasso工程中。

重要资料

1]Javadoc :

http://square.github.io/picasso/javadoc/index.html

[2]StackOverflow:http://stackoverflow.com/questions/tagged/picasso?sort=active

许可证

Copyright 2013 Square, Inc.

Licensed under the Apache License, Version2.0 (the "License");

you may not use this file except incompliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreedto in writing, software

distributed under the License isdistributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.

See the License for the specific languagegoverning permissions and

limitations under the License.

原文链接:http://square.github.io/picasso/

PicassoAndroid系统的图片下载缓存 Picasso 是Square开源的一个用于Android系统下载缓存图片的项目。该项目和其他一些下载图片项目的主要区别之一是:使用4.0+系统上的HTTP缓存来代替磁盘缓存Picasso 的使用是非常简单的,例如: 帮助 1 Picasso.with(context).load("http://i.imgur.com/DvpvklR.png.into(imageView")); Picasso有如下特性: 处理Adapter中的 ImageView 回收和取消已经回收ImageView的下载进程 使用最少的内存完成复杂的图片转换,比如把下载的图片转换为圆角等 自动添加磁盘和内存缓存 具体介绍 在Adapter中下载 自动检测Adapter中的ImageView重用和取消不必要的下载 帮助 01.@Override public void getView(int position, View convertView, ViewGroup parent) { 02.SquaredImageView view = (SquaredImageView) convertView; 03.if (view == null) { 04.view = new SquaredImageView(context); 05.} 06.String url = getItem(position);Picasso.with(context).load(url).into(view); 07.} 复制代码 图片转换 转换图片以适合所显示的ImageView,来减少内存消耗 帮助 01.Picasso.with(context) 02..load(url) 03..resize(50, 50) 04..centerCrop() 05..into(imageView) 复制代码 还可以设置自定义转换来实现高级效果,例如下面的矩形特效(把图片居中裁剪为矩形) 帮助 01.public class CropSquareTransformation implements Transformation { 02.@Override public Bitmap transform(Bitmap source) { 03.int size = Math.min(source.getWidth(), source.getHeight()); 04.int x = (source.getWidth() - size) / 2; 05.int y = (source.getHeight() - size) / 2; 06.Bitmap result = Bitmap.createBitmap(source, x, y, size, size); 07.if (result != source) { 08.source.recycle(); 09.} 10.return result; 11.}@Override public String key() { return "square()"; } 12.} 复制代码 用该类示例调用函数 RequestBuilder.transform(Transformation) 即可。 占位符图片 Picasso支持下载和加载错误占位符图片。 帮助 Picasso.with(context) .load(url) .placeholder(R.drawable.user_placeholder) .error(R.drawable.user_placeholder_error) .into(imageView); 如果重试3次(下载源代码可以根据需要修改)还是无法成功加载图片 则用错误占位符图片显示。 支持本地资源加载 从 Resources, assets, files, content providers 加载图片都支持 Picasso.with(context).load(R.drawable.landing_screen).into(imageView1); Picasso.with(context).load(new File("/images/oprah_bees.gif")).into(imageView2); 调试支持 调用函数 Picasso.setDebug(true) 可以在加载的图片左上角显示一个 三角形 ,不同的颜色代表加载的来源 红色:代表从网络下载的图片 黄色:代表从磁盘缓存加载的图片 绿色:代表从内存中加载的图片 如果项目中使用了OkHttp的话,默认会使用OkHttp来下载图片。否则使用HttpUrlConnection来下载图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值