解决Glide4.0和圆角裁剪CenterCrop冲突
16lz
2021-12-04
两种方案:
1、重写BitmapTransformation
重写方法解决冲突,来自CSDN的一个解决方案
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) { 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 void updateDiskCacheKey(MessageDigest messageDigest) { }}
在加载的时候使用
RequestOptions myOptions = new RequestOptions() .transform(new GlideRoundTransform(this,30)); Glide.with(this) .load(R.drawable.item1) .apply(myOptions) .into(icon1);
地址http://blog.csdn.net/flyinbed_/article/details/75506062
2、使用第三方框架
库地址:
https://github.com/wasabeef/glide-transformations
该库是专门针对glide的一个辅助类,包括裁剪
效果图
引入方法
compile 'jp.wasabeef:glide-transformations:3.0.1'
使用方法
Glide.with(mContext) .load(aClass.img) .apply(RequestOptions.bitmapTransform(new MultiTransformation( new CenterCrop(), new RoundedCornersTransformation(SizeUtils.dp2px(5), 0, RoundedCornersTransformation.CornerType.TOP)))) .into(imageView);
这里RoundedCornersTransformation的第一个参数需要转换成像素
更多相关文章
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- eclipse中使用RecyclerView报错:Error inflating class android.s
- Android使用DialogFragment,自定义对话框
- Best Practice For Android
- android中使用SQLite进行CRUD操作的实例
- Android中Intent传值