对于Actionscript 3“绘图应用程序”,我希望能够选择纹理并设置它的透明度。 因此,我尝试设置纹理的alpha透明度。 但它不起作用。
我做了什么:
结果:
当绘制线条(使用moveTo,lineTo等)时,线条使用纹理,但忽略使用lineStyle设置的“Alpha”。
我做错了什么?
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, setTexture);
setTexture(e:Event):void
{
e.currentTarget.removeEventListener(e.type, arguments.callee);
//Try 1: Trying to set the Alpha-Trasparency with "lineStyle"-Command:
myDrawContainer.graphics.lineBitmapStyle(5, 0xFF0000, 0,6);
//Try 2: Trying to set the Alpha-Transparency by changing the Alpha-Value of the loaded content:
myLoader.content.alpha = 0.6;
//Getting the BitmapData of the Image:
BitmapDataOfMyTexture = Bitmap(LoaderInfo(e.target).content).bitmapData
//"Using" the TBitmapData as "Color/Texture" for my Drawing:
myDrawContainer.graphics.lineBitmapStyle( BitmapDataOfMyTexture );
//Test-Drawing:
myDrawContainer.graphics.moveTo( 0, 0 );
myDrawContainer.graphics.moveTo( 500, 500 ); //-> RESULT: Textured Line WITHOUT Transparency!
}
结果:我得到一条使用纹理但没有透明度的线。
(更新)解决方案:(感谢DodgerThud)
要设置/更改已加载图像的Alpha通道,请不要使用“lineStyle”,而是使用......
创建新的colorTransform-object
然后将其设置为“alphaMultiplier” - 属于特定的alphaChannel
然后使用加载的BitmapData的“colorTransform”-Method将这个新创建的colorTransform-Object应用于加载的BitmapData。
但
这不适用于没有alpha通道或没有激活alpha通道的图像。降低alpha通道时,这些图像只会变暗。在这些情况下,您必须这样做:
所以这是工作代码:
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, setTexture);
setTexture(e:Event):void
{
e.currentTarget.removeEventListener(e.type, arguments.callee);
//Getting the BitmapData of the Image:
BitmapDataOfMyTexture = Bitmap(LoaderInfo(e.target).content).bitmapData
//Create an ADDITIONAL BitmapData-Object with 3rd
//argument set to TRUE and with same width and height
//as the LOADED image:
var BMDContainerWithAlphaActivated:BitmapData;
BMDContainerWithAlphaActivated = new BitmapData(BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height, true, 0xFFFFFF);
//Copy the pixels of the loaded image into the newly created
//"BitmapData-Container with activated AlphaChannel":
BMDContainerWithAlphaActivated.copyPixels(BitmapDataOfMyTexture, new Rectangle(0, 0, BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height), new Point(0,0))
//Modify the Alpha-Value (of the NEW BitmapData-Object):
var colorChanges:ColorTransform = new ColorTransform();
colorChanges.alphaMultiplier = 0.3;
BMDContainerWithAlphaActivated.colorTransform(new Rectangle(0, 0, BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height), colorChanges);
//"Using" the (NEW) BitmapData as "Color/Texture" for my Drawing:
myDrawContainer.graphics.lineBitmapStyle( BMDContainerWithAlphaActivated );
//Test-Drawing:
myDrawContainer.graphics.moveTo( 0, 0 );
myDrawContainer.graphics.moveTo( 500, 500 ); //-> RESULT: Textured Line WITH Transparency 0.3!
}
答案 0 :(得分:1)
啊,我知道,这比我最初想的要复杂一点。
好的,查看lineBitmapStyle
的{{3}}告诉我该函数需要以下参数:lineBitmapStyle(bitmap:BitmapData, matrix:Matrix = null, repeat:Boolean = true, smooth:Boolean = false)
现在,矩阵,重复和平滑对我们没有帮助(矩阵在这里用于变换,即定位,旋转等),但位图:BitmapData可能。我们需要做的是在将加载的PNG文件的BitmapData传递给lineBitmapStyle
之前对其进行操作。遗憾的是,我们无法直接在BMD上设置alpha值,因此我们可以尝试colorTransform
它。
这是未经测试的代码,但我认为这是正确的方法:
..
//store the bitmapdata in a seperate local variable
var bmd:BitmapData = LoaderInfo(e.target).content;
//create a ColorTransform Object to change the values of the BMD
var cTransform:ColorTransform = new ColorTransform();
//now here I am unsure, manipulating the alpha value of the BMD
cTransform.alphaMultiplier = 0.6;
//defining the rectangle dimensions of the bmd, we want it to be over the entire texture
var rect:Rectangle = new Rectangle(0,0,bmd.width,bmd.height);
//apply the colorTransformation on the BMD
bmd.colorTransform(rect,cTransform);
...
//the now manipulated BMD gets set as lineBitmapStyle
myDrawContainer.graphics.lineBitmapStyle(bmd);
现在我考虑一下,也许我们可以解决方法在BMD上设置alpha值,首先创建一个Bitmap,然后在那里设置alpha值并使用Bitmap的bitmapdata。像这样:
var bmd:BitmapData = LoaderInfo(e.target).content;
var bm:Bitmap = new Bitmap(bmd);
bm.alpha = 0.6;
myDrawContainer.graphics.lineBitmapStyle(bm.bitmapData);
好吧,上面的第一个片段似乎是这样做的,但BitmapData的transparent
值必须为true。鉴于您没有自己直接创建BitmapData并且值为false,我们在这里遇到了相当棘手的情况。
另一种方法是创建一个额外的bitmapdata,允许透明度和draw()
加载图像的位图数据:
var bmdSource:BitmapData = LoaderInfo(e.target).content;
var bmd:BitmapData = new BitmapData(bmdSource.width, bmdSource.height,true,0xffffffff);
var cTransform:ColorTransform = new ColorTransform();
cTransform.alphaMultiplier = 0.6;
var rect:Rectangle = new Rectangle(0,0,bmd.width,bmd.height);
bmd.colorTransform(rect,cTransform);
//now we have a completely white bitmapdata bmd, with an alpha value of 0.6
//we draw the contents of the bmdSource onto bmd, the alpha value effect should carry over
bmd.draw(bmdSource);