Refer to Java class object type conversion knowledge, ColorDrawable and BitmapDrawable are in a horizontal relationship, and there is no possibility of forced conversion to each other. The class object conversion mechanism exists between class objects with a parent-child relationship. When forced conversion, only the parent class object can be forced to be converted into a child class object.
The inheritance relationship is as above
can be generated using the constructor of
Canvas
将ColorDrawable
转成Bitmap
,用BitmapDrawable
Can’t.
This is not an Android problem, Java does not allow you to cast an object to an incompatible class
Refer to Java class object type conversion knowledge, ColorDrawable and BitmapDrawable are in a horizontal relationship, and there is no possibility of forced conversion to each other. The class object conversion mechanism exists between class objects with a parent-child relationship. When forced conversion, only the parent class object can be forced to be converted into a child class object.