drawable如何只让两个叫圆角_【AndroidSupport】RoundedBitmapDrawable 创建一个圆角图片...

#【AndroidSupport】RoundedBitmapDrawable 创建一个圆角图片

##简介

RoundedBitmapDrawable 是 android.support.v4.graphics.drawable 里面的一个类,用来创建简单的圆角图片。

如果只是简单的圆角展示,比如展示一个圆角头像,这个类完全可以胜任。

##简单使用

获取 RoundedBitmapDrawable。RoundedBitmapDrawable 是一个抽象类,无法直接获取。所以提供了 RoundedBitmapDrawableFactory 来操作:

RoundedBitmapDrawableFactory的静态签名:

static RoundedBitmapDrawablecreate(Resources res, InputStream is)

static RoundedBitmapDrawablecreate(Resources res, String filepath)

static RoundedBitmapDrawablecreate(Resources res, Bitmap bitmap)

没有提供直接从 resId 获取的方法,但是是一样的:

RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.xxx));

设置属性。几个重要的属性:

cornerRadius:圆角半径

alpha:透明度

如果想得到一个圆形,那么可以直接调用 RoundedBitmapDrawable#setCircular(true),

不过这样需要注意点是,如果原始的图形不是圆形,那么图形会变形。

当然,结果还与 scaleType 有关,这里有点复杂,暂无需关心。所以,如果你想要一个圆形,你就给一个正方形。

不要同时设置 cornerRadius 和 setCircular(true),因为两者是冲突的。

设置 Drawable。 ImageView#setImageDrawable(roundedBitmapDrawable);

##原理

RoundedBitmapDrawable 内部使用 BitmapShader 来处理图形渲染,无他。

#第三方开源控件

RoundedBitmapDrawable 的局限性还是比较大,如果想要实现一写些自由度更大的圆角,边框等等,可以考虑使用第三方空间,比如:

###Android分享 Q群:315658668

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值