常见的框架
在几乎所有的apk中,都会涉及到图片异步读取、显示,这里总结下面四种框架,并准备做具体分析。
- Picasso
- Glide
- Fresco
体积大小
Fresco>Glide>Picasso
特色
- Picasso:和Square的网络库一起能发挥最大作用,因为Picasso可以选择将网络请求的缓存部分交给了okhttp实现,默认的ARGB_8888格式;如果项目中使用okhttp,建议直接使用这种框架加载;
- Glide:在Picasso的基础上加了很多的扩展(比如gif等支持),默认的RGB_565格式,大型的图片流,比如gif、Video可以用这种框架;
- Fresco:在5.0以下系统,Fresco将图片放到一个特别的内存区域(Ashmem区),在图片不显示的时候,占用的内存会自动被释放。这会使得APP更加流畅,减少因图片内存占用而引发的OOM,在5.0以后系统默认就是存储在Ashmem区。
Picasso介绍使用
官网地址:http://square.github.io/picasso/
Github地址:https://github.com/square/picasso
不同资源的加载方式
- 网络图片:Picasso.with(context).load(“http://i.imgur.com/DvpvklR.png“).into(imageView);
- Drawable:Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
- Assets:Picasso.with(context).load(“file:///android_asset/DvpvklR.png”).into(imageView2);
- SD:Picasso.with(context).load(new File(…)).into(imageView3);
压缩图片
直接指定图片的大小
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()”; }
}
Picasso.with(context)
.load(“http://…”)
.transform(transformation)
.into(imageview);
指定不同状态的图片
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)//默认图片
.error(R.drawable.user_placeholder_error)//失败后的图片
.into(imageView);
圆形图片&圆角图片
圆角矩形
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import com.squareup.picasso.Transformation;
public class PicassoRoundTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int widthLight = source.getWidth();
int heightLight = source.getHeight();
Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paintColor = new Paint();
paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);
RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));
canvas.drawRoundRect(rectF, widthLight / 5, heightLight / 5, paintColor);
Paint paintImage = new Paint();
paintImage.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(source, 0, 0, paintImage);
source.recycle();
return output;
}
@Override
public String key() {
return “roundcorner”;
}
}圆形图片
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(this).load(url).config(Bitmap.Config.ARGB_8888).placeholder(R.mipmap.head1).error(R.mipmap.ic_launcher).into(img,
new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
Toast.makeText(getApplicationContext(), “onSuccess”, Toast.LENGTH_SHORT).show();
}
@Override
public void onError() {
Toast.makeText(getApplicationContext(), “onError”, Toast.LENGTH_SHORT).show();
}
});