【问题标题】:Casting from RoundedBitmapDrawable to BitmapDrawable从 RoundedBitmapDrawable 转换为 BitmapDrawable
【发布时间】:2015-06-17 02:39:25
【问题描述】:

在开始使用 RoundedBitmapDrawable 为Bitmap 圆角之前,一切都运行良好。

开始使用RoundedBitmapDrawable后,我得到:

java.lang.ClassCastException: android.support.v4.graphics.drawable.RoundedBitmapDrawable21 无法转换为 android.graphics.drawable.BitmapDrawable

代码:

BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());

【问题讨论】:

  • 发现这个可行,但仍然宁愿使用上面这个问题中建议的简单转换。 ^^stackoverflow.com/a/9390776/1525654
  • 为什么需要 BitnapDrawable?
  • 从 Drawable 转换为 Bitmap
  • 如果您需要位图,那么您找到的解决方案就是正确的解决方案。如果你需要你的 riunded drawable 来绘制一些东西,只需使用它的 draw() 方法

标签: android casting imageview bitmapdrawable roundedbitmapdrawable


【解决方案1】:

这两种类型之间没有层次联系,只是它们共享同一个父级。相反,您可以在运行时检查可绘制对象的类型并进行相应的转换,

if (imageView.getDrawable() instanceof android.support.v4.graphics.drawable.RoundedBitmapDrawable) {
    RoundedBitmapDrawable drawable = (RoundedBitmapDrawable) imageView.getDrawable();
    Bitmap roundedBitmap = drawable.getBitmap();
    // ...
}

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 2015-02-02
    • 2020-10-29
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多