博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【AndroidSupport】RoundedBitmapDrawable 创建一个圆角图片
阅读量:7024 次
发布时间:2019-06-28

本文共 1213 字,大约阅读时间需要 4 分钟。

  hot3.png

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

##简介 RoundedBitmapDrawable 是 android.support.v4.graphics.drawable 里面的一个类,用来创建简单的圆角图片。 如果只是简单的圆角展示,比如展示一个圆角头像,这个类完全可以胜任。

##简单使用

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

    RoundedBitmapDrawableFactory的静态签名:

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

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

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

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

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

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

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

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

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

##原理

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

#第三方开源控件

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

###Android分享 Q群:315658668

转载于:https://my.oschina.net/xesam/blog/529305

你可能感兴趣的文章
【ALearning】第二章 Androidproject知识介绍
查看>>
JAVA实现AES的加密和解密算法
查看>>
makefile 学习一
查看>>
yii 验证码 CCaptcha的总结(转)
查看>>
oracle汉字占用字节长度
查看>>
python--条件判断和循环--3
查看>>
开发环境、生产环境、测试环境的基本理解和区别
查看>>
CSS布局:水平居中
查看>>
【HTTP】WireShark中获取Content-Encoding: gzip时的响应内容
查看>>
一些组织和个人网站
查看>>
二叉树应用进阶之折纸(二叉树的右根左遍历)
查看>>
运维相关开源项目
查看>>
Lua MD5加密字符串
查看>>
Heap & Priority Queue
查看>>
RDA PQ工具使用 (Adi Analysis)
查看>>
LEETCODE
查看>>
织云Lite发布:详解包管理核心能力
查看>>
hadoop04---shell
查看>>
HDU 4419 Colourful Rectangle(线段树)
查看>>
webservice接口的开发和调用
查看>>