因注释很细,就不一一解释说明了!
1.Glide加载圆形图片:
自定义GlideCircleTransUtils继承BitmapTransformation,重写transform()方法;
public class GlideCircleTransUtils extends BitmapTransformation {
public GlideCircleTransUtils(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());
// 子位图第一个像素在源位图的X坐标
int x = (source.getWidth() - size) / 2;
//子位图第一个像素在源位图的y坐标
int y = (source.getHeight() - size) / 2;
//创建新位图 source 源位图
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
//返回一个正好匹配给定宽、高和配置的只包含透明像素的Bitmap
// 如果BitmapPool中找不到这样的Bitmap,就返回null
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
//当返回null 时,创建给定宽、高和配置的新位图
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
//画图
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
// 设置shader
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(imgurl).transform(new GlideCircleTransUtils(this)).into(iv_circle);
2.Glide加载圆角图片:
自定义GlideRoundTransUtils继承BitmapTransformation,重写transform()方法;可自定义圆角度数;
public class GlideRoundTransUtils extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundTransUtils(Context context) {
this(context, 4);
}
public GlideRoundTransUtils(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
// 如果BitmapPool中找不到这样的Bitmap,就返回null
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
//当返回null 时,创建给定宽、高和配置的新位图
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
// 设置shader
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
//抗锯齿
paint.setAntiAlias(true);
//画矩形
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
//绘制圆角矩形 (RectF对象,x方向上的圆角半径,y方向上的圆角半径,绘制时所使用的画笔)
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
@Override
public String getId() {
return getClass().getName() + Math.round(radius);
}
}
使用:
//加载圆角图片
Glide.with(this).load(imgurl).transform(new GlideRoundTransUtils(this, 10)).into(iv_Round);
最后附下整体demo下载地址:
http://download.csdn.net/download/shanshan_1117/10256998
补充:
glide对指定角加载圆角
效果图:
不做介绍了.下面附上整体代码下载地址(次代码包含上边demo的整体代码):
http://download.csdn.net/download/shanshan_1117/10259431