2019-7-20 zhulinan Flash
import flash.display.Shape; import flash.display.GradientType; import flash.geom.Matrix; import flash.display.Sprite; var myShape:Shape = new Shape(); var gradientBoxMatrix:Matrix = new Matrix(); gradientBoxMatrix.createGradientBox(400, 400,0, 0, 0);//Math.PI*0.5是设置渐变转动90度为垂直(默认为水平,也可设任意角度) //参数填充类型,颜色数组,alpha数组,渐变比率 myShape.graphics.beginGradientFill(GradientType.LINEAR, [0,0,0,0], [0,1,1,0], [1,1,1,255], gradientBoxMatrix); myShape.graphics.drawRect(0, 0, 400, 400); myShape.graphics.endFill(); addChild(myShape); //然后就是把此对象设为遮罩层即可 var spt:Sprite=new Sprite(); spt.graphics.beginFill(0xFFCCCC); spt.graphics.drawRect(0, 0, 400, 400); spt.graphics.endFill(); addChild(spt); //设为遮罩层 spt.mask=myShape; //这时还没完成,如果这样是看不到效果的,必须得再设置这两个对象的cacheAsBitmap属性为true,才可以,即 myShape.cacheAsBitmap=true; spt.cacheAsBitmap=true; //设为遮罩层 spt.mask=myShape;
代码已验证,出处:https://www.cnblogs.com/frost-yen/p/4781672.html