转载自:https://www.jianshu.com/p/ef0ed5b53eb1
1.CenterCrop与Transformer的共存问题
因为此bug无法在使用GridLayoutManager和StaggeredGridLayoutManager等其他情况中使用.centerCrop选项,所以要实现此功能需要在ImageView中去设置scaleType为centerCrop
But,如果你想同时让图片有圆角之类的Transformer,比如在Glide中.transform()配置了一个圆角矩形,那如果同时ImageView的scaleType设置了centerCrop,那圆角就没有了
要解决此问题需要设置两个Transformer,在我的项目中像这样:
.transform(new CenterCrop(getContext())
,new GlideRoundTransform(getContext(), 20))
这样完美解决问题。
附上圆角的代码:
GlideRoundTransform 代码
https://github.com/wasabeef/glide-transformations/issues/16
https://github.com/bumptech/glide/issues/613
* Created by rainfool on 16/8/4. */ public class GlideRoundTransform extends BitmapTransformation { private static float radius = 0f; public GlideRoundTransform(Context context) { this(context, 4); } public GlideRoundTransform(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 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; } @Override public String getId() { return getClass().getName() + Math.round(radius); } }
作者:RainFool
链接:https://www.jianshu.com/p/0e79080c8116
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。