从 RoundedBitmapDrawable 转换为 BitmapDrawable



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

开始使用RoundedBitmapDrawable后,我得到:

java.lang.ClassCastException: android.support.v4.graphics.drawable.RoundedBitmapDrawable21 不能转换为 android.graphics.drawable.BitmapDrawable

法典:

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

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

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

最新更新