|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
这几天在自己做游戏的时候,发现有时候总是需要给元件加滤镜,按照API的方法又要定义滤镜类型,又要数组,又要push...挺累人的,所以我就抽空写了这么一个类,来简化这些操作,下面附上源码~
注意:使用前请先导入Filters类所在的Class
共有9个静态方法,使用Filters.+相应方法即可操作;
其中前六个是设置常用滤镜的方法,后三个是关于滤镜的操作
- package kujiukaren.createUI{
-
- import flash.filters.DropShadowFilter;
- import flash.filters.BlurFilter;
- import flash.filters.GlowFilter;
- import flash.filters.BevelFilter;
- import flash.filters.GradientBevelFilter;
- import flash.filters.GradientGlowFilter;
- //import flash.filters.ColorMatrixFilter;
-
- public class Filters{
-
- private static var _filters:Array=new Array();
-
- //**************************************************
-
- public function Filters():void{
- //设置不能被实例化
- throw new Error("Filter class is static container only");
- }
-
- //**************************************************
-
- //设置投影滤镜
- public static function setDropShadow(mc:*,distance:Number=4.0,angle:Number=45,color:uint=0,alpha:Number=1.0,blurX:Number=4.0,blurY:Number=4.0,strength:Number=1.0,quality:int=1,inner:Boolean=false,knockout:Boolean=false,hideObject:Boolean=false):void{
- //阴影的偏移距离,以像素为单位|阴影的角度|阴影的颜色|阴影颜色的 Alpha 透明度值|水平模糊量|垂直模糊量|印记或跨页的强度|应用滤镜的次数|表示阴影是否为内侧阴影|应用挖空效果 (true),这将有效地使对象的填色变为透明,并显示文档的背景颜色|表示是否隐藏对象
- var drop:DropShadowFilter=new DropShadowFilter(distance,angle,color,alpha,blurX,blurY,strength,quality,inner,knockout,hideObject);
- _filters.push(drop);
-
- mc.filters=_filters;
- }
-
- //设置模糊滤镜
- public static function setBlur(mc:*,blurX:Number=4.0,blurY:Number=4.0,quality:int=1):void{
- //水平模糊量|垂直模糊量|执行模糊的次数
- var blur:BlurFilter=new BlurFilter(blurX,blurY,quality);
- _filters.push(blur);
-
- mc.filters=_filters;
- }
-
- //设置发光滤镜
- public static function setGlow(mc:*,color:uint=0xFF0000,alpha:Number=1.0,blurX:Number=6.0,blurY:Number=6.0,strength:Number=2,quality:int=1,inner:Boolean=false,knockout:Boolean=false):void{
- //光晕颜色|颜色的 Alpha 透明度值|水平模糊量|垂直模糊量|印记或跨页的强度|应用滤镜的次数|指定发光是否为内侧发光|指定对象是否具有挖空效果
- var glow:GlowFilter=new GlowFilter(color,alpha,blurX,blurY,strength,quality,inner,knockout);
- _filters.push(glow);
-
- mc.filters=_filters;
- }
-
- //设置斜角滤镜
- public static function setBevel(mc:*,distance:Number=4.0,angle:Number=45,highlightColor:uint=0xFFFFFF,highlightAlpha:Number=1.0,shadowColor:uint=0x000000,shadowAlpha:Number=1.0,blurX:Number=4.0,blurY:Number=4.0,strength:Number=1,quality:int=1,type:String="inner",knockout:Boolean=false):void{
- //斜角的偏移距离|斜角的角度|斜角的加亮颜色|加亮颜色的 Alpha 透明度值|斜角的阴影颜色|阴影颜色的 Alpha 透明度值|水平模糊量,以像素为单位|垂直模糊量,以像素为单位|印记或跨页的强度|应用滤镜的次数|斜角在对象上的位置|应用挖空效果 (true),这将有效地使对象的填色变为透明,并显示文档的背景颜色
- var bevel:BevelFilter=new BevelFilter(distance,angle,highlightColor,highlightAlpha,shadowColor,shadowAlpha,blurX,blurY,strength,quality,type,knockout);
- _filters.push(bevel);
-
- mc.filters=_filters;
- }
-
- //设置渐变斜角滤镜
- public static function setGradientBevel(mc:*,distance:Number=4.0,angle:Number=45,colors:Array=null,alphas:Array=null,ratios:Array=null,blurX:Number=4.0,blurY:Number=4.0,strength:Number=1,quality:int=1,type:String="inner",knockout:Boolean=false):void{
- //偏移距离|角度,以度为单位|渐变中使用的 RGB 十六进制颜色值数组|colors 数组中对应颜色的 Alpha 透明度值的数组|对应于 colors 数组中颜色的一组颜色分布比率|水平模糊量,以像素为单位|垂直模糊量,以像素为单位|印记或跨页的强度|应用滤镜的次数|斜角效果的放置|指定对象是否具有挖空效果
- var gradientBevel:GradientBevelFilter=new GradientBevelFilter(distance,angle,colors,alphas,ratios,blurX,blurY,strength,quality,type,knockout);
- _filters.push(gradientBevel);
-
- mc.filters=_filters;
- }
-
- //设置渐变发光滤镜
- public static function setGradientGlow(mc:*,distance:Number=4.0,angle:Number=45,colors:Array=null,alphas:Array=null,ratios:Array=null,blurX:Number=4.0,blurY:Number=4.0,strength:Number=1,quality:int=1,type:String="inner",knockout:Boolean=false):void{
- //光晕的偏移距离|角度,以度为单位|定义渐变的颜色数组|colors 数组中对应颜色的 Alpha 透明度值的数组|对应于 colors 数组中颜色的一组颜色分布比率|水平模糊量,以像素为单位|垂直模糊量,以像素为单位|印记或跨页的强度|应用滤镜的次数|斜角在对象上的位置|应用挖空效果 (true),这将有效地使对象的填色变为透明,并显示文档的背景颜色
- var gradientGlow:GradientGlowFilter=new GradientGlowFilter(distance,angle,colors,alphas,ratios,blurX,blurY,strength,quality,type,knockout);
- _filters.push(gradientGlow);
-
- mc.filters=_filters;
- }
-
- /*
- //设置调整颜色滤镜
- public static function setColorMatrix(mc:*,):void{
- var colormatrix:ColorMatrixFilter=new ColorMatrixFilter();
- }*/
-
- //清理缓存的滤镜信息
- public static function cleanAllFilters():void{
- _filters=[];
- }
-
- //删除指定mc的所有滤镜
- public static function deleteAllFilters(mc:*):void{
- mc.filters=null;
- }
-
- //获取指定mc的全部滤镜属性
- public static function getFilters(mc:*):Array{
- //trace("filters:"+mc.filters);
- return mc.filters;
- }
- }
- }
复制代码 使用示例:- package{
-
- import flash.display.MovieClip;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
-
- import kujiukaren.createUI.Filters;
-
- public class SampleMain extends MovieClip {
-
- private var _titleText:TextField;
- private var _titleText_2:TextField;
- private var _titleText_3:TextField;
-
- public function SampleMain() {
- _titleText=new TextField();
- _titleText.text="什么都不加";
- _titleText.autoSize=TextFieldAutoSize.LEFT;
- _titleText.x=150;
- _titleText.y=50;
-
- _titleText_2=new TextField();
- _titleText_2.text="设置了阴影滤镜";
- _titleText_2.autoSize=TextFieldAutoSize.LEFT;
- _titleText_2.x=150;
- _titleText_2.y=100;
-
- _titleText_3=new TextField();
- _titleText_3.text="设置了阴影+发光滤镜";
- _titleText_3.autoSize=TextFieldAutoSize.LEFT;
- _titleText_3.x=150;
- _titleText_3.y=150;
-
- setTextStyle(_titleText,24,true,0xffffff);
- setTextStyle(_titleText_2,24,true,0xffffff);
- setTextStyle(_titleText_3,24,true,0xffffff);
-
- Filters.cleanAllFilters();
- Filters.setDropShadow(_titleText_2,5.0,45,0x000000,1.0,5.0,5.0,1.0,3,false,false,false);
-
- Filters.cleanAllFilters();
- Filters.setDropShadow(_titleText_3,5.0,45,0x000000,1.0,5.0,5.0,1.0,3,false,false,false);
- Filters.setGlow(_titleText_3,0xff3300,1.0,5.0,5.0,1,3,false,false);
-
- addChild(_titleText);
- addChild(_titleText_2);
- addChild(_titleText_3);
- }
-
- private function setTextStyle(txtField:TextField,size:int=12,bold:Boolean=false,color:uint=0xffffff):void{
- var fontStyle:TextFormat=new TextFormat();
- fontStyle.font="Microsoft YaHei";
- fontStyle.size=size;
- fontStyle.bold=bold;
- fontStyle.color=color;
-
- txtField.setTextFormat(fontStyle);
- }
- }
-
- }
复制代码 效果:
|
|