Android Glide框架将图片设置为圆形的

我们经常遇到图片展示的 不需要展示四四方方的 而是圆形的图片 比如头像之类的
其实Glide已经帮我们做好了这些,废话不说代码如下:
第一步 添加依赖包

implementation 'com.github.bumptech.glide:glide:3.7.0'

第二步,写一个公共方法,传入路径和ImageView

/**
	 * 圆 图片
	 */
	public static void imaview(final Context context, String str, ImageView view) {
		if (str == null || str.equals("")) {
			return;
		}
		Glide.with(context).load(str).asBitmap().centerCrop()
				.into(new BitmapImageViewTarget(view) {
					@Override
					protected void setResource(Bitmap resource) {
						RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory
								.create(context.getResources(), resource);
//						circularBitmapDrawable.setCornerRadius(100); // 设置圆角半径(根据实际需求)
//						circularBitmapDrawable.setAntiAlias(true); // 设置反走样
						 circularBitmapDrawable.setCircular(true);
						view.setImageDrawable(circularBitmapDrawable);
					}
				});
	}

猜你喜欢

转载自blog.csdn.net/qq_28643195/article/details/107428775
今日推荐