android图片圆角,Android图片圆角的实现方案

一、 参考glide的实现方法

RequestOptions options = new RequestOptions().bitmapTransform(new RoundedCorners(30));//图片圆角为30

Glide.with(this).load(bitmap) //图片地址

.apply(options)

.into(iv_normal);

通过RoundedCorners的Transfromation进行圆角实现,在TransformationUtil中通过BitmapShader来设置Paint,然后canvas.drawRect来实现。

public static Bitmap roundedCorners(

@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int roundingRadius) {

Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");

// Alpha is required for this transformation.

Bitmap.Config safeConfig = getAlphaSafeConfig(inBitmap);

Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);

Bitmap result = pool.get(toTransform.getWidth(), toTransform.getHeight(), safeConfig);

result.setHasAlpha(true);

BitmapShader shader = new BitmapShader(toTransform, Shader.TileMode.CLAMP,

Shader.TileMode.CLAMP);

Paint paint = new Paint();

paint.setAntiAlias(true);

paint.setShader(shader);

RectF rect = new RectF(0, 0, result.getWidth(), result.getHeight());

BITMAP_DRAWABLE_LOCK.lock();

try {

Canvas canvas = new Canvas(result);

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);

clear(canvas);

} finally {

BITMAP_DRAWABLE_LOCK.unlock();

}

if (!toTransform.equals(inBitmap)) {

pool.put(toTransform);

}

return result;

}

二、项目中的实现方案

计算四个角的path来canvas.drawPath,这样实现起来不公麻烦,而且会影响黑暗模式的适配

public void initRightDownPath() {

rightDownPath = new Path();

rightDownPath.moveTo(width, height);

rightDownPath.lineTo(width, height - radius);

RectF rectF = new RectF(width - 2 * radius, height - 2 * radius, width, height);

rightDownPath.arcTo(rectF, 0, 90, true);

rightDownPath.lineTo(width, height);

rightDownPath.close();

}

canvas.drawPath(leftUpPath, paint);

canvas.drawPath(rightUpPath, paint);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值