根据此处的文档,应该将 png 资源转换为BitmapDrawable. 但是,我观察到一个奇怪的行为,其中如果我尝试在自定义视图的构造函数中执行以下操作,其中只有黑色像素ClassCastException的 png 文件会因为(包裹在)而导致崩溃:InvocationTargetException
...
tempDrawable = typedArr.getDrawable(R.styleable.CustomView_src); // 源指向一个 png 文件
Log.i("TestPNGToResource", "规范类名" + tempDrawable.getClass().getCanonicalName());
tempBitmap = ((BitmapDrawable) tempDrawable).getBitmap();
...
我看到以下登录 android 2.2 和 2.3
09-24 13:21:37.575: I/TestPNGToResource(532): 规范类名 android.graphics.drawable.ColorDrawable
为什么资源没有被转换为 BitmapDrawable?