在Glide V4版本之后,对于圆角处理和高斯模糊的方法都有了一些改变,下面主要简单介绍下处理方法 。在开始之前,我们先来看看效果,看看是否符合你的需要,图1:圆角处理(四个角可以随意组合处理);图2:高斯模糊处理;图3:圆角处理和高斯模糊处理
首先我们需要添加Glide依赖
compile('com.github.bumptech.glide:glide:4.7.1')
其次我们新建GlideRoundedCornersTransform类,代码如下
public class GlideRoundedCornersTransform extends CenterCrop {
private float mRadius;
private CornerType mCornerType;
private static final int VERSION = 1;
private static final String ID = BuildConfig.APPLICATION_ID + "GlideRoundedCornersTransform." + VERSION;
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
/** 待处理的圆角枚举*/
public enum CornerType {
ALL,
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
TOP, BOTTOM, LEFT, RIGHT,
TOP_LEFT_BOTTOM_RIGHT,
TOP_RIGHT_BOTTOM_LEFT,
TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT,
TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT,
TOP_LEFT_TOP_RIGHT
}
public GlideRoundedCornersTransform(float radius, CornerType cornerType) {
super();
mRadius = radius;
mCornerType = cornerType;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap transform = super.transform(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, transform);
}
private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) {
return null;
}
int width = source.getWidth();
int height = source.getHeight();
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);
Path path = new Path();
drawRoundRect(canvas, paint, path, width, height);
return result;
}
private void drawRoundRect(Canvas canvas, Paint paint, Path path, int width, int height) {
float[] rids ;
switch (mCornerType) {
case ALL:
rids = new float[]{mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius};
drawPath(rids,canvas, paint, path, width, height);
break;
case TOP_LEFT:
rids = new float[]{mRadius,mRadius,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
drawPath(rids,canvas, paint, path, width, height);
break;
case TOP_RIGHT:
rids = new float[]{0.0f,0.0f,mRadius,mRadius,0.0f,0.0f,0.0f,0.0f};
drawPath(rids,canvas, paint, path, width, height);
break;
case BOTTOM_RIGHT:
rids = new float[]{0.0f,0.0f,0.0f,0.0f,mRadius,mRadius,0.0f,0.0f};
drawPath(rids,canvas, paint, path, width, height);
break;
case BOTTOM_LEFT:
rids = new float[]{0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,mRadius,mRadius};
drawPath(rids,canvas, paint, path, width, height);
break;
case TOP:
rids = new float[]{mRadius,mRadius,mRadius,mRadius,0.0f,0.0f,0.0f,0.0f};
drawPath(rids,canvas, paint, path,width, height);
break;
case BOTTOM:
rids = new float[]{0.0f,0.0f,0.0f,0.0f,mRadius,mRadius,mRadius,mRadius};
drawPath(rids,canvas, paint, path, width, height);
break;
case LEFT:
rids = new float[]{mRadius,mRadius,0.0f,0.0f,0.0f,0.0f,mRadius,mRadius};
drawPath(rids,canvas, paint, path, width, height);
break;
case RIGHT:
rids = new float[]{0.0f,0.0f,mRadius,mRadius,mRadius,mRadius,0.0f,0.0f};
drawPath(rids,canvas, paint, path, width, height);
break;
case TOP_LEFT_BOTTOM_RIGHT:
rids = new float[]{mRadius,mRadius,0.0f,0.0f,mRadius,mRadius,0.0f,0.0f};
drawPath(rids,canvas, paint, path, width, height);
break;
case TOP_RIGHT_BOTTOM_LEFT:
rids = new float[]{0.0f,0.0f,mRadius,mRadius,0.0f,0.0f,mRadius,mRadius};
drawPath(rids,canvas, paint, path, width, height);
break;
case TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT:
rids = new float[]{mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,0.0f,0.0f};
drawPath(rids,canvas, paint, path, width, height);
break;
case TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT:
rids = new float[]{0.0f,0.0f,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius};
drawPath(rids,canvas, paint, path,width, height);
break;
case TOP_LEFT_TOP_RIGHT:
rids = new float[]{mRadius,mRadius,mRadius,mRadius,0.0f,0.0f,0.0f,0.0f};
drawPath(rids,canvas, paint, path, width, height);
break;
default:
throw new RuntimeException("RoundedCorners type not belong to CornerType");
}
}
/**@param rids 圆角的半径,依次为左上角xy半径,右上角,右下角,左下角*/
private void drawPath(float[] rids, Canvas canvas, Paint paint, Path path, int width, int height) {
path.addRoundRect(new RectF(0, 0, width, height), rids, Path.Direction.CW);
canvas.drawPath(path,paint);
}
@Override
public boolean equals(Object o) {
return o instanceof GlideRoundedCornersTransform;
}
@Override
public int hashCode() {
return ID.hashCode();
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
messageDigest.update(ID_BYTES);
}
}
最后我们使用Glide进行圆角处理,在这里直接使用本地图片进行演示了,网络图片直接使用url即可
private void toRoundCorners() {
// 圆角处理
Drawable drawable = ActivityCompat.getDrawable(this, R.drawable.mine_task_img_cover_icon_girl);
RequestOptions myOptions = new RequestOptions().optionalTransform
(new GlideRoundedCornersTransform(CommentUtils.dip2px(this, 5f)
, GlideRoundedCornersTransform.CornerType.ALL));
Glide.with(this)
.load(drawable)
.apply(myOptions)
.into(imgRounded);
}
首先我们添加相关依赖
compile 'jp.wasabeef:glide-transformations:2.0.1'
其次我们新建BlurTransformation类
public class BlurTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID = "BlurTransformation." + VERSION;
private static int MAX_RADIUS = 25;
private static int DEFAULT_DOWN_SAMPLING = 1;
private int radius;
private int sampling;
public BlurTransformation() {
this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
}
public BlurTransformation(int radius) {
this(radius, DEFAULT_DOWN_SAMPLING);
}
public BlurTransformation(int radius, int sampling) {
this.radius = radius;
this.sampling = sampling;
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
int scaledWidth = width / sampling;
int scaledHeight = height / sampling;
Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.scale(1 / (float) sampling, 1 / (float) sampling);
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(toTransform, 0, 0, paint);
bitmap = FastBlur.blur(bitmap, radius, true);
return bitmap;
}
@Override public String toString() {
return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")";
}
@Override public boolean equals(Object o) {
return o instanceof BlurTransformation &&
((BlurTransformation) o).radius == radius &&
((BlurTransformation) o).sampling == sampling;
}
@Override public int hashCode() {
return ID.hashCode() + radius * 1000 + sampling * 10;
}
@Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + radius + sampling).getBytes(CHARSET));
}
}
最后我们使用Glide进行处理即可
private void toBlurTransformation() {
// 高斯模糊
Drawable drawable = ActivityCompat.getDrawable(this, R.drawable.mine_task_img_cover_icon_girl);
Glide.with(this)
.load(drawable)
.apply(RequestOptions.bitmapTransform(new BlurTransformation(15, 1)))
.into(imgBlur);
}
通过上面可知圆角和高斯模糊分别处理的方法,可是当我们即需要圆角又需要高斯模糊呢?这时我们可以通过MultiTransformation类进行处理
private void toRoundAndBlurTransformation() {
// 圆角和高斯模糊
Drawable drawable = ActivityCompat.getDrawable(this, R.drawable.mine_task_img_cover_icon_girl);
MultiTransformation multi = new MultiTransformation(
new BlurTransformation(15, 1),
new GlideRoundedCornersTransform(CommentUtils.dip2px(this, 5f)
, GlideRoundedCornersTransform.CornerType.ALL)
);
Glide.with(this)
.load(drawable)
.apply(RequestOptions.bitmapTransform(multi))
.into(imgRoundAndBlur);
}