倒影类Reflection及其用法举例

package {
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;

	import com.Reflection;

	public class PhotoFrame extends Sprite {
		private var url:String;
		public var _name:String;
		private var link:String;

		private var loader:Loader;

		private var bg_reflection:Reflection;

		public function PhotoFrame(url_:String,name_:String,link_:String):void {
			url=url_;
			_name=name_;
			link=link_;

			init();
		}
		private function init():void {
			tf_label.text=_name;

			loader=new Loader();
			loader.load(new URLRequest(url));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded,false,0,true);
		}

		private function loaded(evt:Event):void {
			this.bg.addChild(loader);
			loader.x=5;
			loader.y=5;

			bg_reflection=new Reflection(bg,bg.height*0.6,1,1,1,0,1);
			addChild(bg_reflection);
			bg_reflection.y+=8;
			//注意:因为图片是添加到bg容器里,所以只需要对bg添加倒影效果即可,不需再为loader添加倒影
		}
	}
}

 

package com{

	/*
	
	Written by:
	Dustin Andrew
	dustin@flash-dev.com
	www.flash-dev.com
	
	LAST UPDATED:
	01/24/06
	
	Reflection.as
	
	Create a bitmap reflection of a displayobject
	
	*/

	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;

	public class Reflection extends Sprite {

		private var _disTarget:DisplayObject;
		private var _height:Number;
		private var _numStartFade:Number=.3;
		private var _numMidLoc:Number=.5;
		private var _numEndFade:Number=0;
		private var _numSkewX:Number=0;
		private var _numScale:Number=1;
		private var _bmpReflect:Bitmap;

		// Constructor
		public function Reflection(set_disTarget:DisplayObject, height_:Number, set_numStartFade:Number, set_numMidLoc:Number, set_numEndFade:Number, set_numSkewX:Number, set_numScale:Number) {
			super();
			_disTarget=set_disTarget;
			_height=height_;
			_numStartFade=set_numStartFade;
			_numMidLoc=set_numMidLoc;
			_numEndFade=set_numEndFade;
			_numSkewX=set_numSkewX;
			_numScale=set_numScale;

			_bmpReflect=new Bitmap(new BitmapData(1,1,true,0));
			this.addChild(_bmpReflect);
			createReflection();
		}

		// Create reflection
		private function createReflection(event:Event = null):void {

			// Reflection
			var bmpDraw:BitmapData=new BitmapData(_disTarget.width,_height,true,0);
			var matSkew:Matrix=new Matrix(1,0,_numSkewX,-1*_numScale,0,_height);//此处必须要用_height
			var recDraw:Rectangle = new Rectangle(0, 0, _disTarget.width, _height * (2 - _numScale));
			var potSkew:Point=matSkew.transformPoint(new Point(0,_disTarget.height));//此处不能替换成_height
			matSkew.tx=potSkew.x*-1;
			matSkew.ty = (potSkew.y - _height) * -1;//此处必须要用_height
			bmpDraw.draw(_disTarget, matSkew, null, null, recDraw, true);

			// Fade
			var shpDraw:Shape = new Shape();
			var matGrad:Matrix = new Matrix();
			var arrAlpha:Array = new Array(_numStartFade, (_numStartFade - _numEndFade) / 2, _numEndFade);
			var arrMatrix:Array=new Array(0,0xFF*_numMidLoc,0xFF);
			matGrad.createGradientBox(_disTarget.width, _height, 0.5 * Math.PI);
			//这里设置投影高度=_height;
			shpDraw.graphics.beginGradientFill(GradientType.LINEAR, new Array(0,0,0), arrAlpha, arrMatrix, matGrad);
			shpDraw.graphics.drawRect(0, 0, _disTarget.width, _height);
			shpDraw.graphics.endFill();
			bmpDraw.draw(shpDraw, null, null, BlendMode.ALPHA);

			_bmpReflect.bitmapData.dispose();
			_bmpReflect.bitmapData=bmpDraw;

			_bmpReflect.filters=_disTarget.filters;

			this.x=_disTarget.x;
			this.y = (_disTarget.y + _disTarget.height) - 1;
		}
	}
}

 

 

posted @ 2010-08-24 17:48  LT世纪  阅读(481)  评论(0编辑  收藏  举报