【AndroidSupport】RoundedBitmapDrawable 建立一个圆角图片

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

##简介 RoundedBitmapDrawable 是 android.support.v4.graphics.drawable 里面的一个类,用来建立简单的圆角图片。 若是只是简单的圆角展现,好比展现一个圆角头像,这个类彻底能够胜任。git

##简单使用github

  1. 获取 RoundedBitmapDrawable。RoundedBitmapDrawable 是一个抽象类,没法直接获取。因此提供了 RoundedBitmapDrawableFactory 来操做:code

    RoundedBitmapDrawableFactory的静态签名:图片

    static RoundedBitmapDrawable	create(Resources res, InputStream is)
     static RoundedBitmapDrawable	create(Resources res, String filepath)
     static RoundedBitmapDrawable	create(Resources res, Bitmap bitmap)

    没有提供直接从 resId 获取的方法,可是是同样的:get

    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.xxx));
  2. 设置属性。几个重要的属性:it

    1. cornerRadius:圆角半径
    2. alpha:透明度

    若是想获得一个圆形,那么能够直接调用 RoundedBitmapDrawable#setCircular(true),原理

    不过这样须要注意点是,若是原始的图形不是圆形,那么图形会变形。渲染

    固然,结果还与 scaleType 有关,这里有点复杂,暂无需关心。因此,若是你想要一个圆形,你就给一个正方形。file

    不要同时设置 cornerRadius 和 setCircular(true),由于二者是冲突的。

  3. 设置 Drawable。 ImageView#setImageDrawable(roundedBitmapDrawable);

##原理

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

#第三方开源控件

RoundedBitmapDrawable 的局限性仍是比较大,若是想要实现一写些自由度更大的圆角,边框等等,能够考虑使用第三方空间,好比:

CircularImageView RoundedImageView

###Android分享 Q群:315658668

相关文章
相关标签/搜索