ImageView设置rounded corner

版权声明:本文为xing_star原创文章,转载请注明出处!

本文同步自http://javaexception.com/archives/207

ImageView设置rounded corner

到2019年,现在的图片加载框架,几乎都是Glide了。
最近的需求又碰到了要给图片设置圆角,发现之前总结的很多都解决不了。这里在单独对图片设置圆角在做个汇总。
 
在Glide3.x的版本中可以采用如下实现方式:
Glide.with(picViewHolder.imageView.getContext()).load(url)
.asBitmap().centerCrop()
        .into(new BitmapImageViewTarget(picViewHolder.imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(picViewHolder.imageView.getResources(), resource);
                circularBitmapDrawable.setCornerRadius(SystemUtils.dip2px(3));
                picViewHolder.imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
在Glide4.x中可以采用如下实现方式:
Glide.with(this.context)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(14)))
                .into(ImageView);
下次再碰到了,一定及时找这篇文章,不用在到google上找半天啦。
 
相关资料参考:
 
Glide处理圆形ImageView http://javaexception.com/archives/182
 
Glide3升级到Glide4碰到的问题汇总以及部分代码修改 http://javaexception.com/archives/188
原文地址:https://www.cnblogs.com/xing-star/p/11478427.html