Following on from an answer I gave here: AS 3 | Cloning Png image data
I knew that to get a transparent BitmapData Object it was necessary to specify a background color of black, but until I was called out on it I never bothered to check why. Having checked, I wonder if there is a reason I don't see for what seems like conflicting default arguments to the BitmapData constructor?
First, here is the constructor as defined in the documentation:
public function BitmapData(width:int, height:int, transparent:Boolean = true, fillColor:uint = 0xFFFFFFFF)
Please notice that the default value for transparency is true. Now consider what the docs say about the transparent parameter:
transparent:Boolean (default = true) — Specifies whether the bitmap image supports per-pixel transparency. The default value is true (transparent). To create a fully transparent bitmap, set the value of the transparent parameter to true and the value of the fillColor开发者_Python百科 parameter to 0x00000000 (or to 0). Setting the transparent property to false can result in minor improvements in rendering performance.
It seems to say that this parameter is useless unless you set the fill color to black. So why then is the default fillColor value 0xFFFFFFFF?
This seems wrong to me. If a developer wants the performance gain of no alpha channel, it must be explicitly specified:
var bmd:BitmapData = new BitmapData(width,height,false);
And if a transparent BitmapData is required, that too must be explicitly specified:
var bmd:BitmapData = new BitmapData(width,height,true,0x000000);
So at no point can we rely on the shortest constructor form, without accepting a performance hit, and I can't understand why. Surely either the fillColor should default to black:
var bmd:BitmapData = new BitmapData(width,height); // transparent BitmapData
or transparency should default to false:
var bmd:BitmapData = new BitmapData(width,height); // faster, no-alpha BitmapData
Can anyone explain to me what the benefit of the current constructor is?
There is no benefit of the current constructor -- it's just Adobe mode the default -- I'm assuming they didn't want people complaining that when drawing displayobjects, movieclips, etc, the bitmap contained a background. Honestly -- I think it would've made more sense to make no defaults, but that's just what they chose, and we have to abide by it.
If you really want a minor speed increase and don't want to specify that constructor, create a final subclass with that ctor defined:
public final class Surface extends BitmapData {
public function Surface(width:int, height:int, fillColor:uint = 0x000000):void {
super(width, height, false, fillColor);
}
}
精彩评论