本篇文章其实并不应该出现!【捂脸哭】
看过这篇文章的建议测试一下,本文中的示例,顺便对比其他百度到的示例。
写这篇文章的原因如下:
1.目前能百度到的glide圆角,圆形方案都是错误的
2.实际上glide在后续的新版本里提供了圆角圆形方案。
刚开始我在做项目的时候,采用了百度到的方案,结果发现百度到的代码效率极差,尤其是在做recycleView的时候,展示图片慢成狗。
于是我重新研究了glide代码,以及官方文档。
注:以下示例
圆形:
public class GlideCircleTransform extends BitmapTransformation {
private static final String ID = "com.xiaohe.www.lib.tools.glide.GlideCircleTransform";
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
int size = Math.min(toTransform.getWidth(), toTransform.getHeight());
int x = (toTransform.getWidth() - size) / 2;
int y = (toTransform.getHeight() - size) / 2;
Bitmap square = Bitmap.createBitmap(toTransform, x, y, size, size);
Bitmap circle = pool.get(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(circle);
Paint paint = new Paint();
paint.setShader(new BitmapShader(square, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return circle;
}
@Override
public boolean equals(Object obj) {
return obj instanceof GlideCircleTransform;
}
@Override
public int hashCode() {
return ID.hashCode();
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update(ID_BYTES);
}
}
圆角:
public class GlideRoundTransform extends BitmapTransformation {
private static final String ID = "com.xiaohe.www.lib.tools.glide.GlideRoundTransform";
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
private int radius;
public GlideRoundTransform(Context context, int radius) {
this.radius = UUi.dp2px(radius);
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(toTransform, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
return bitmap;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof GlideRoundTransform) {
GlideRoundTransform other = (GlideRoundTransform) obj;
return radius == other.radius;
}
return false;
}
@Override
public int hashCode() {
return Util.hashCode(ID.hashCode(), Util.hashCode(radius));
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update(ID_BYTES);
byte[] radiusData = ByteBuffer.allocate(4).putInt(radius).array();
messageDigest.update(radiusData);
}
}
————————————————我是分割线————————————————
解释说明:
问:为嘛这个示例效率高?????
答:这个示例明显重写了三个方法,百度到的代码都是重写了两个方法,并不能让glide运行正常!!!!
———————————————–再分割————————————————————
其实新版本的glide提供了这两个方法
圆角:RequestOptions.bitmapTransform(new RoundedCorners( 5))
圆形:RequestOptions.bitmapTransform(new CircleCrop())
又跳过了一个坑。。。。。。。。。。。。。。。。。。。。开心