Glide 加载圆角图片(解决设置圆角后和centerCrop冲突的问题)

平常设置圆角

 public static void setRoundedCornersImage(Context context, String url, ImageView imageView, int roundingRadius, int errorImage, int placeholderImage) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholderImage) //预加载图片
                .error(errorImage) //加载失败图片
                .bitmapTransform(new RoundedCorners(DataTools.dip2px(context, roundingRadius))); //圆角
        Glide.with(context).load(url).apply(options).into(imageView);
    }

这样设置会导致 .centerCrop() 方法无效(圆角和它是同一类型设置),如果你在xml也设置了这个则会出现圆角无效的问题(大抵是因为设置圆角后又被剪切了的原因吧)

如果想要既占满图片大小,又显示圆角,则需要自定义一些这个圆角控件(RoundedCorners)

进入源码,找到 transform()方法,修改(可以全复制出来修改)

   private boolean isCenterCrop;

    /**
     * @param roundingRadius the corner radius (in device-specific pixels).
     * @throws IllegalArgumentException if rounding radius is 0 or less.
     */
    public RoundedCorners2(int roundingRadius, boolean isCenterCrop) {
        Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");
        this.roundingRadius = roundingRadius;
        this.isCenterCrop = isCenterCrop;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        if (isCenterCrop) {
            Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
            return TransformationUtils.roundedCorners(pool, bitmap, roundingRadius);
        } else {
            return TransformationUtils.roundedCorners(pool, toTransform, roundingRadius);
        }
    }

增加了一个字段:isCenterCrop 用来设置是否需要另设置 centerCrop效果,然后在 transform 里设置

调用

 public static void setRoundedCornersImage(Context context, String url, ImageView imageView, int roundedCorners,boolean isCenterCrop) {
        RequestOptions options = new RequestOptions()
                .bitmapTransform(new RoundedCorners2(DataTools.dip2px(context, roundedCorners), isCenterCrop)); //圆角
        Glide.with(context).load(url).apply(options).into(imageView);
    }

如果想要设置占位图或者失败图的话就会发现,这俩不是圆角,很不合群,这里需要再给它俩也设置下,使用 .thumbnail()方法

 public static void setRoundedCornersImage(Context context, String url, ImageView imageView, int roundedCorners,boolean isCenterCrop, int errorImage, int placeholderImage) {
        RequestOptions options = new RequestOptions()
                .placeholder(placeholderImage) //预加载图片
                .error(errorImage) //加载失败图片
                .bitmapTransform(new RoundedCorners2(DataTools.dip2px(context, roundedCorners), isCenterCrop)); //圆角
        Glide.with(context).load(url).apply(options)
                .thumbnail(loadTransform(imageView.getContext(), placeholderImage, roundedCorners, isCenterCrop))
                .thumbnail(loadTransform(imageView.getContext(), errorImage, roundedCorners, isCenterCrop))
                .into(imageView);
    }

    private static RequestBuilder<Drawable> loadTransform(Context context, @DrawableRes int placeholderId, int roundedCorners, boolean isCenterCrop) {
        RequestOptions options = new RequestOptions()
                .bitmapTransform(new RoundedCorners2(DataTools.dip2px(context, roundedCorners), isCenterCrop));
        return Glide.with(context)
                .load(placeholderId)
                .apply(options);
    }
/**
	 * dipתΪ px
	 */
	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值