- 用于处理图片下载、缓存、裁剪、缩放等功能的框架
- 英文介绍链接地址 : http://square.github.io/picasso/
- Picasso.with(context) 创建Picasso对象
- Picasso.with(context).load(imageUrl).into(imageView);
- 自动将图像缓存在本地
- 通过图片压缩转换以减少内存消耗
- Picasso.with(context).load(imageUrl).resize(50,50).centerCrop().into(imageView);
剪裁大小
- Picasso.wint(context).load(imageUrl).placeholder(R.drawable.image_placeholder).error(R.drawable.ic);
占位符图片:所谓的占位符图像即当图片未正常显示时默认的图片,通过placeholder()设置,Picasso也支持设置图片显示错误时显示的默认图片,通过error()设置
- Picasso.with(context).load(R.drawable.icon).into(imageView);
- Picasso.with(context).load("file:///android_asset/Adnroid.png").into(imageView);
- Picasso.with(context).load(new File(...)).into(imageView);
载入本地图片资源(以上3个)
- Picasso.with(mContext).load(url).transform(transform)
自定义图片裁剪方式
使用方式:
导入的位置:Project Structure –Dependencies 框架名称:com.squareup.picasso:picasso:2.5.2
Picasso.with(context) 创建Picasso对象
Picasso.with(context).load(imageUrl).into(imageView); 自动将图像缓存在本地 通过图片压缩转换以减少内存消耗
Picasso.with(context).load(imageUrl).resize(50,50).centerCrop().into(imageView); 剪裁大小 Picasso.wint(context).load(imageUrl).placeholder(R.drawable.image_placeholder).error(R.drawable.ic); 占位符图片:所谓的占位符图像即当图片未正常显示时默认的图片,通过placeholder()设置,Picasso也支持设置图片显示错误时显示的默认图片,通过error()设置
加载本地图片: Picasso.with(context).load(R.drawable.icon).into(imageView); Picasso.with(context).load("file:///android_asset/Adnroid.png").into(imageView); Picasso.wiht(context).load(new File(...)).into(imageView);
Picasso.with(mContext).load(url).transform(transform) 自定义图片裁剪方式 |
自定义裁剪的具体实现:
public class MyRoundTran implements Transformation { |
生成圆形图片:
public
CircleTransformation(Picasso picasso) {
this
.picasso = picasso;
}
@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 squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if
(squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas =
new
Canvas(bitmap);
Paint paint =
new
Paint();
BitmapShader shader =
new
BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(
true
);
float
r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return
bitmap;
}
@Override
public
String key() {
return
"circle"
;
}
}
生成灰色图片:
public
class
GrayscaleTransformation
implements
Transformation {
private
final
Picasso picasso;
public
GrayscaleTransformation(Picasso picasso) {
this
.picasso = picasso;
}
@Override
public
Bitmap transform(Bitmap source) {
Bitmap result = createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
Bitmap noise;
try
{
noise = picasso.load(R.drawable.noise).get();
}
catch
(IOException e) {
throw
new
RuntimeException(
"Failed to apply transformation! Missing resource."
);
}
BitmapShader shader =
new
BitmapShader(noise, REPEAT, REPEAT);
ColorMatrix colorMatrix =
new
ColorMatrix();
colorMatrix.setSaturation(
0
);
ColorMatrixColorFilter filter =
new
ColorMatrixColorFilter(colorMatrix);
Paint paint =
new
Paint(ANTI_ALIAS_FLAG);
paint.setColorFilter(filter);
Canvas canvas =
new
Canvas(result);
canvas.drawBitmap(source,
0
,
0
, paint);
paint.setColorFilter(
null
);
paint.setShader(shader);
paint.setXfermode(
new
PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
canvas.drawRect(
0
,
0
, canvas.getWidth(), canvas.getHeight(), paint);
source.recycle();
noise.recycle();
return
result;
}
@Override
public
String key() {
return
"grayscaleTransformation()"
;
}
}
|