最近一直在赶项目,一晃都半个月没有来写文章了,今天忙里偷闲,把最近半个月用的功能和解决的bug来记录一下,方便自己日后查看
前段时间,在项目刚刚开始的时候根据UI切图用到了圆角图片,在网上找了资料,自定义了一个圆角view类,可以实现本地图片加载转换为圆角图片了,昨天后台把图片放到服务器上后,网络请求下来的图片我用Glide加载时程序一直崩溃,报转换异常错误!后面看了代码才知道,以前自定义的view只能把本地的图片转换为圆角图片,网络请求下来的图片无法转换。 fresco一款强大的图片加载框架,支持加载各种图形,只是用起来比较复杂,现在是项目急需,我需要更简单更方便的方法。后来我就想glide是否支持加载圆角图片呢,在网上搜了一圈,都说glide原生的不支持,但是有大神写好的方法,试了一下,还真的特别好用,关键是还很简单。贴代码如下:
下载的图片转圆形的方法
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override public String getId() {
return getClass().getName();
}
}
使用方法:
Glide
.with(this)
.load("图片url地址")
.centerCrop()
.placeholder(R.mipmap.ic_launcher)//默认图片
.crossFade()
.transform(new GlideCircleTransform(this))//**必须放在into上面使用,放在其他位置则无效果**
.into(iv_machine);
下载图片转换层圆角图片的方法
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundTransform(Context context) {
this(context, 4);
}
public GlideRoundTransform(Context context, int dp) {
super(context);
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return roundCrop(pool, toTransform);
}
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
@Override public String getId() {
return getClass().getName() + Math.round(radius);
}
}
在这个方法里面,你可以自定义圆角的大小,使用方式也非常简单
Glide
.with(this)
.load("图片url地址")
.centerCrop()
.placeholder(R.mipmap.ic_launcher)
.crossFade()
.transform(new GlideRoundTransform(this,8))**//也必须放在into上面才有效果,亲测!**
.into(iv_machine);
我爬的小坑如下:
写的时候就因为transform位置不对,一直没效果,放在into上面就可以了
写的不详细,如果不明白可以去看原文,原文地址链接如下:http://blog.csdn.net/weidongjian/article/details/47144549