android - 如何使用圆角 BitmapDrawable

标签 android android-drawable android-bitmap

有人用过RoundedBitmapDrawable吗?如果我错了,请纠正我,但据我了解,它是从常规矩形图像生成圆形图像。

到目前为止我尝试过的是这个

RoundedBitmapDrawable.createRoundedBitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), iconResource))

我试图实现的目标:将任何图像转换为圆形图像并使用 ImageView 显示它。

万一我把事情搞混了,我说的都是胡说八道。是否有可能(或更简单)使用任何新框架来做到这一点? (Android L 或新的支持库)

最佳答案

你需要设置圆角半径。

Resources res = getResources();
Bitmap src = BitmapFactory.decodeResource(res, iconResource);
RoundedBitmapDrawable dr =
    RoundedBitmapDrawableFactory.create(res, src);
dr.setCornerRadius(Math.max(src.getWidth(), src.getHeight()) / 2.0f);
imageView.setImageDrawable(dr);

关于android - 如何使用圆角 BitmapDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24878740/

相关文章:

android - 位图到uri转换android?

android - Android 中 TextPaint 标签的不同文本质量和输出

android - RecyclerView 内容高度

android - Android 上的 Xuggler

android - 以编程方式将颜色 "#e3bb87"添加到 StateListDrawable

android - 如何从 res/drawables 文件夹存储图片路径?

android - 从 SD 卡 Rajawali 解析 .obj 模型

android - 更新 SDK 版本,获取 ClassNotFoundException : android. support.v4.view.ViewPager

android - lineDraw() 方法未在 Canvas 上显示线条 (Android)

android - TextView 到 Bitmap,对齐问题