根据这里的文档,png资源应该被转换为
BitmapDrawable
。然而,我观察到一个奇怪的行为,即一个只有黑色像素的png文件会导致崩溃,因为如果我在自定义视图的构造函数中尝试以下操作:
... tempDrawable = typedArr.getDrawable(R.styleable.CustomView_src); // Source points to a png file Log.i("TestPNGToResource", "Canonical Class Name " + tempDrawable.getClass().getCanonicalName()); tempBitmap = ((BitmapDrawable) tempDrawable).getBitmap(); ...
在android 2.2和2.3上记录如下
09-24 13:21:37.575: I/TestPNGToResource(532): Canonical Class Name android.graphics.drawable.ColorDrawable
为什么资源没有被转换为BitmapDrawable?
aapt
的PNG优化是否能够将纯色图像转换为ColorDrawable
或者这是否是在解析自定义属性时出现的错误。 - zapl