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())
又跳过了一个坑。。。。。。。。。。。。。。。。。。。。开心
作者:王二蛋和他的狗
链接:https://www.jianshu.com/p/5c54046d2b1c
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦