• AS3 CookBook学习整理(六)


    1. 在位图上应用滤镜

    使用BitmapData类的applyFilter()

    destBmp.applyFilter(sourceBitmapData, sourceRect, destPoint, filter);

    sourceBitmapData -- 要使用的输入位图图像。源图像可以是另一个BitmapData对象,也可以引用当前BitmapData实例

    sourceRect -- 定义要用作输入的源图像区域的矩形

    destPoint -- 目标图像(当前BitmapData实例)中与源矩形的左上角对应的点

    filter -- 用于执行过滤操作的滤镜对象

    如果 BitmapData 对象和指定为 sourceBitmapData 参数的对象是同一对象,应用程序将使用该对象的临时副本来执行滤镜。为了获得最佳性能,请避免这种情况

    可以直接定义Bitmap.filters属性,它不会直接接触BitmapData的像素数据,因为滤镜只应用在包装BitmapData的Bitmap上

    package {   
        import flash.display.Bitmap;   
        import flash.display.BitmapData;   
        import flash.display.Sprite;   
        import flash.events.Event;   
        import flash.filters.BlurFilter;   
        import flash.geom.Point;   
      
        public class Sample0319 extends Sprite   
        {   
            private var image:Bitmap;   
            private var blurFilter:BlurFilter;   
            private var point:Point;   
               
            public function Sample0319()   
            {   
                image = new Bitmap();   
                image.bitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false,0x000000);   
                this.addChild(image);   
                   
                blurFilter = new BlurFilter();   
                point = new Point();   
                   
                this.addEventListener(Event.ENTER_FRAME,onEnterFrame);   
            }   
               
            private function onEnterFrame(target:Event):void   
            {   
                for(var i:uint=0;i<100;i++)   
                {   
                    image.bitmapData.setPixel(mouseX + Math.random()*20 - 10,   
                                  mouseY + Math.random()*20 - 10,   
                                  0xFFFFFF);       
                }   
                image.bitmapData.applyFilter(image.bitmapData,image.bitmapData.rect,point,blurFilter);   
            }   
        }   
    }

    2. 位图的淡出

    使用BitmapData类的pixelDissolve()方法

    seed = pixelDissolve(

    sourceBitmapData:BitmapData, //要使用的源位图图像。源图像可以是另一个BitmapData对象,也可以是目标位图自身

    sourceRect:Rectangle, //源位图区域的矩形(一般等于目标位图的大小)

    destPoint:Point, //目标位图(当前BitmapData实例)中与源矩形的左上角对应的点

    randomSeed:int = 0, //用于开始像素溶解的随机种子(第一次可随意定义)

    numPixels:int = 0, //每次拷贝多少像素,默认值是源区域(宽度×高度)的1/30

    fillColor:uint = 0 //一个ARGB颜色值,用于填充其原值等于目标值的像素(即目标位图淡出为该种颜色) ):int

    * 该函数每次调用,都返回用于后续调用的新随机种子值

    * 可以通过 已拷贝的像素总数 是否大于 位图宽度×位图高度,来判断是否拷贝完成

    package {   
        import flash.display.Bitmap;   
        import flash.display.Loader;   
        import flash.display.LoaderInfo;   
        import flash.display.Sprite;   
        import flash.events.Event;   
        import flash.geom.Point;   
        import flash.net.URLRequest;   
      
        public class Sample0319 extends Sprite   
        {      
            private var imageSmall:Bitmap;   
            private var imageBig:Bitmap;   
            private var seed:uint;   
            private var pixelComplete:uint = 0;   
               
            public function Sample0319()   
            {   
                var loader:Loader = new Loader();   
                loader.load(new URLRequest("1.jpg"));   
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadSmallComplete);   
                   
                loader = new Loader();   
                loader.load(new URLRequest("2.jpg"));   
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadBigComplete);   
               
                this.addEventListener(Event.ENTER_FRAME,onEnterFrame);   
            }   
               
            private function onLoadSmallComplete(event:Event):void   
            {   
                var tmp:LoaderInfo = event.target  as  LoaderInfo;   
                imageSmall = tmp.loader.content  as  Bitmap;   
                this.addChild(imageSmall);   
            }   
               
            private function onLoadBigComplete(event:Event):void   
            {   
                var tmp:LoaderInfo = event.target  as  LoaderInfo;   
                imageBig = tmp.loader.content  as  Bitmap;   
               //this.addChild(imageBig);  
               //imageBig.x = 200;    
            }   
               
            private function onEnterFrame(event:Event):void   
            {   
                if(imageSmall!=null && imageBig!=null)   
                {   
                   //每次拷贝总像素的1%,如果是30帧播放的话,即1秒完成30%,全部完成要3秒左右    
                    var numPixels:uint = imageSmall.bitmapData.width * imageSmall.bitmapData.height / 100;   
                       
                    seed = imageSmall.bitmapData.pixelDissolve(   
                                                                imageBig.bitmapData,   
                                                                imageSmall.bitmapData.rect,   
                                                                new Point(),   
                                                                seed,   
                                                                numPixels);                                            
                    pixelComplete += numPixels;   
                       
                    if(pixelComplete > imageSmall.bitmapData.width * imageSmall.bitmapData.height)   
                    {   
                        this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);   
                    }   
                }   
            }   
        }   
    }

    3. 滚动位图

    使用BitmapData类的scroll(x,y)方法,将图像按一定量的(x,y)像素进行滚动。滚动区域之外的边缘区域保持不变

    x -- 水平滚动量

    y -- 垂直滚动量

    package {   
        import flash.display.Bitmap;   
        import flash.display.BitmapData;   
        import flash.display.LoaderInfo;   
        import flash.display.Shape;   
        import flash.display.Sprite;   
        import flash.events.Event;   
      
        public class Sample0320 extends Sprite   
        {          
            private var image:Bitmap;   
               
            public function Sample0320()   
            {   
                var circle:Shape = new Shape();   
                circle.graphics.beginFill(0xFFFF00);   
                for(var i:int=20;i<200;i=i+20)   
                {   
                    circle.graphics.drawCircle(i,i,10);   
                }   
                circle.graphics.endFill();   
               //this.addChild(circle);    
                   
                image = new Bitmap();   
                image.bitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false,0x000000);   
                image.bitmapData.draw(circle);   
                   
                this.addChild(image);   
                   
                this.addEventListener(Event.ENTER_FRAME,onEnterFrame);   
            }   
               
            private function onEnterFrame(event:Event):void   
            {   
                if(image!=null)   
                {   
                    image.bitmapData.scroll(-1,-1);   
                }      
            }   
        }   
    }

    4. 设置文本框的边框、背景与前景色

    默认状态下,文本框的边框和背景都是禁用的。所以在设置之前需要先启用;前景色可直接设置。

    边框 -- TextField.border = true; TextField.borderColor = 0xFF0000;

    背景 -- TextField.background = true; TextField.backgroundColor = 0xFF0000;

    前景色 -- TextField.textColor = 0xFF0000;

    5. 设置文本框为可输入,不能选择及粘贴

    设置TextField.type属性为TextFieldType.INPUT,可以将文本框定义为可输入。默认是TextFieldType.DYNAMIC(动态文本框,可以由ActionScript控制,但用户无法输入)

    设置TextField.selectable = false,则文本字段中的文本不响应来自鼠标或键盘的选择命令,并且不能使用“复制”命令复制文本。默认为true

    6. 设置文本框为密码输入框

    设置TextField.displayAsPassword = true;

    7. 限制文本输入

    设置TextField.restrict,例如设置TextField.restrict = "abc",则只允许输入字符abc,以大写形式输入也会变成小写。

    package {   
        import flash.display.Sprite;   
        import flash.text.TextField;   
        import flash.text.TextFieldType;   
      
        public class Sample0323 extends Sprite   
        {   
            public function Sample0323()   
            {   
                var text:TextField = new TextField();   
                text.type = TextFieldType.INPUT;   
                text.border = true;   
                text.borderColor = 0xFFFF00;   
                text.height = 20;   
                   
                text.restrict = "a-z0-9";//允许a-z和0-9之间的字符    
               //text.restrict = "0-9^5"; //允许除了5以外的所有数字    
               //text.restrict = "^\u001A"; //不允许(Control-Z) 产生的字符(Unicode编码)    
               //text.restrict = "^\\-"; //特殊符号可通过(\\)进行转义    
                   
                this.addChild(text);   
            }   
        }   
    }

    8. 限制文本框输入字符数

    设置TextField.maxChars = 5,则为最大允许输入5个字符。如果设为null则表示不限制

    9. 显示HTML格式文本

    设置htmlText属性值为HTML内容

    package {   
        import flash.display.Sprite;   
        import flash.text.TextField;   
        import flash.text.TextFieldType;   
      
        public class Sample0323 extends Sprite   
        {   
            public function Sample0323()   
            {   
                var text:TextField = new TextField();   
                text.type = TextFieldType.INPUT;   
                text.border = true;   
                text.borderColor = 0xFFFF00;   
                text.height = 20;   
                   
                text.htmlText = "<a href='http://www.baidu.com/' target='_blank'><i><u>跳转到百度</u></i></a>";   
                text.appendText("增加的内容");   
                       
                this.addChild(text);   
            }   
        }   
    }

    10. 显示HTML文本时压缩空格

    设置condenseWhite属性为true

    package {   
        import flash.display.Sprite;   
        import flash.text.TextField;   
        import flash.text.TextFieldType;   
      
        public class Sample0323 extends Sprite   
        {   
            public function Sample0323()   
            {   
                var text:TextField = new TextField();   
                text.type = TextFieldType.INPUT;   
                text.border = true;   
                text.borderColor = 0xFFFF00;   
                text.height = 20;   
                   
                text.condenseWhite = true;//必须在赋值前设置,否则无效    
                text.htmlText = "hello           world!";   
                   
                this.addChild(text);   
            }   
        }   
    }
  • 相关阅读:
    丁丁杂想
    Tomcat5.5Ubuntu手记之编程
    插入递归引用Identity列的记录
    evolution错误Ubuntu手记之系统配置
    硬盘安装Ubuntu手记
    WAS安装及概念
    丁丁病了『续』
    web.config中配置字符串中特殊字符的处理
    新宝宝睡眠护理全方位【转】
    丁丁病了
  • 原文地址:https://www.cnblogs.com/CoderWayne/p/1778052.html
Copyright © 2020-2023  润新知