Android 使用 RoundedBitmapDrawable 实现圆角

RoundedBitmapDrawable是系统提供的类,用来实现圆角

package androidx.core.graphics.drawable;
/**
 * A Drawable that wraps a bitmap and can be drawn with rounded corners. You can create a
 * RoundedBitmapDrawable from a file path, an input stream, or from a
 * {@link android.graphics.Bitmap} object.
 * <p>
 * Also see the {@link android.graphics.Bitmap} class, which handles the management and
 * transformation of raw bitmap graphics, and should be used when drawing to a
 * {@link android.graphics.Canvas}.
 * </p>
 */

public abstract class RoundedBitmapDrawable extends Drawable {
.....
}

效果:
在这里插入图片描述

用法:

        ImageView imageView = findViewById(R.id.rounded_bitmap_drawable_circle);
        RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.magazine));
        circleDrawable.setCircular(true);
        imageView.setImageDrawable(circleDrawable);



        imageView = findViewById(R.id.rounded_bitmap_drawable_corner);
        circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.magazine));
        circleDrawable.setCornerRadius(50f);
        imageView.setImageDrawable(circleDrawable);

原理:

使用BitmapShader实现圆角Bitmap

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值