public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
private static final String ID = "com.bumptech.glide.load.resource.bitmap.FillSpace";
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
public GlideRoundTransform(Context context) {
this(4);
}
public GlideRoundTransform(int dp) {
radius = (Resources.getSystem().getDisplayMetrics().density * dp);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, bitmap);
}
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;
}
public String getId() {
return getClass().getName() + Math.round(radius);
}
@Override
public boolean equals(Object o) {
if (o instanceof GlideRoundTransform) {
GlideRoundTransform other = (GlideRoundTransform) o;
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).putFloat(radius).array();
messageDigest.update(radiusData);
}
}
Glide 加载圆角,避免闪烁
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 网上很多方法说是recycleView的动画或是Glide的设置取消加载变换动画.dontAnimate(),试了...
- 直接上代码: 看注解 @SuppressLint("AppCompatCustomView") public cl...
- 睁开眼又是美好的一天,对于即将开始写代码的我,心情是这样滴 戴上耳机听着自己喜欢的五月天的歌,写着让我感觉到安全的...
- 可能这个问题很常见,大家也有不少人解决了,大部分人用的方法一和二如果你还没解决的话,那么看这篇文章就对了使用Rec...