【www.bbyears.com--安卓教程】
之前一直使用imageloader这个框架加载图片,觉得配置很麻烦,觉得使用Picasso这个框架。
什么是Picasso我就不多说了,大家可以去官网看,http://square.github.io/picasso/。
Picasso使用的方法汇总:
代码如下
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso.with(context).load(url).into(view);
Picasso.with(context).load(url) .resize(50, 50).centerCrop().into(imageView)
//这里的placeholder将resource传入通过getResource.getDrawable取资源,所以可以是张图片也可以是color id
Picasso.with(context).load(url).placeholder(R.drawable.user_placeholder).error(R.drawable.user_placeholder_error).into
(imageView);
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);
//这里显示notification的图片
Picasso.with(activity).load(Data.URLS[new Random().nextInt(Data.URLS.length)]).resizeDimen
(R.dimen.notification_icon_width_height,R.dimen.notification_icon_width_height).into(remoteViews, R.id.photo,
NOTIFICATION_ID, notification);
//这里是通过设置tag标签,就是当前传过来的context,这样就可以根据这个context tag来pause和resume显示了
Picasso.with(context).load(url).placeholder(R.drawable.placeholder).error(R.drawable.error).fit().tag(context).into
(view);
//监听onScrollStateChanged的时候调用执行
picasso.resumeTag(context);
picasso.pauseTag(context);
Picasso.with(context).load(contactUri).placeholder(R.drawable.contact_picture_placeholder).tag(context).into
(holder.icon);
//这个onpause方法里的这段代码还是很有意思的
@Override protected void onPause() {
super.onPause();
if (isFinishing()) {
// Always cancel the request here, this is safe to call even if the image has been loaded.
// This ensures that the anonymous callback we have does not prevent the activity from
// being garbage collected. It also prevents our callback from getting invoked even after the
// activity has finished.
Picasso.with(this).cancelRequest(imageView);
}
}
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
.centerInside()
.tag(context)
.into(holder.image);
//Picasso.with使用的是单例模式
Picasso.with(this).cancelTag(this);
然后呢,Picasso还提供了debug的标示,调用picasso的setIndicatorsEnabled方法,true是debug模式,跟踪代码其实就是在最后生成的
PicassoDrawable类的ondraw里绘制了个左上角小三角,根据
public enum LoadedFrom {
MEMORY(Color.GREEN),
DISK(Color.BLUE),
NETWORK(Color.RED);
枚举里的不同值标示不同加载来源,这对分析图片加载有好处。
在Picasso.with()的时候会将执行所需的所有必备元素创建出来,如缓存cache、执行executorService、调度dispatch等,在load()时创建Request,在into()中创建action、bitmapHunter,并最终交给dispatcher执行。
Picasso创建圆形图像
上篇介绍了Picasso这个框架,git地址是https://github.com/square/picasso。怎么实现各种各样的图片样式呢,在Picasso里提供了Transformation这个接口,我们可以自己实现。下面是一个实现圆形图片的demo;
代码如下public class CircleTransform 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 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";
}
}
使用它:
Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);