[ActionScript 3.0] 创建倒影

 1 package 
 2 {
 3     import flash.display.Bitmap;
 4     import flash.display.BitmapData;
 5     import flash.display.DisplayObject;
 6     import flash.display.GradientType;
 7     import flash.display.Loader;
 8     import flash.display.Shape;
 9     import flash.display.Sprite;
10     import flash.events.Event;
11     import flash.geom.Matrix;
12     import flash.geom.Point;
13     import flash.net.URLRequest;
14 
15     /**
16       * ...
17       * @author Frost.Yen
18       */
19     [SWF(width=800, height=1000, backgroundColor=0x000000, frameRate=30)]
20     public class Reflection extends Sprite
21     {
22         private var ldr:Loader=new Loader();
23         private var pic:Sprite=new Sprite();
24         public function Reflection()
25         {
26             ldr.load(new URLRequest("http://hiphotos.baidu.com/frostyen/pic/item/7e49d8b53f6b48ffd9335aa2.jpg"));
27             ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
28         }
29 
30         private function onImageLoaded(e:Event):void
31         {
32             this.addChild(pic);
33             pic.addChild(ldr);
34             pic.x = 200;
35             pic.y = 10;
36             ldr.scaleX = ldr.scaleY = .5;
37             createRef(pic,5,0);
38         }
39         /**
40            *
41            * @param obj  需创建倒影的对象
42            * @param distance  对象与倒影的距离
43            * @param offset  倒影的偏移量
44            */
45         private function createRef(obj:DisplayObject,distance:Number=5,offset:Number=0):void
46         {
47             // 倒置
48             var bd:BitmapData = new BitmapData(obj.width,obj.height,true);
49             var mtx:Matrix = new Matrix();
50             mtx.d = -1;
51             mtx.ty = bd.height;
52             bd.draw(obj, mtx);
53 
54             // 添加倒影渐变
55             var _width:int = bd.width;
56             var _height:int = bd.height;
57 
58             mtx.createGradientBox(_width, _height, 0.5 * Math.PI,0,offset );
59             var shape:Shape = new Shape();
60             shape.graphics.beginGradientFill(GradientType.LINEAR, [0,0,0], [0.6,0.1, 0], [0,200, 255], mtx);
61             shape.graphics.drawRect(0, 0, _width, _height);
62             shape.graphics.endFill();
63             var mask_bd:BitmapData = new BitmapData(_width,_height,true,0);
64             mask_bd.draw(shape);
65             // 生成最终效果
66             bd.copyPixels(bd, bd.rect, new Point(0, 0), mask_bd, new Point(0, 0), false);
67             // 将倒影放置于对象下方
68             var ref:Bitmap = new Bitmap(bd);
69             ref.y = obj.height + obj.y + distance;
70             ref.x = obj.x;
71             obj.parent.addChild(ref);
72         }
73     }
74 
75 }

 

posted on 2015-06-19 17:14  晏过留痕  阅读(243)  评论(0编辑  收藏  举报