/*
用法:
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Bitmap;
var bitmapData:BitmapData= new Shuttle(160,160);//Shuttle为照片
var bitmap:Bitmap= new Bitmap(bitmapData);
var reflect:Reflection=new Reflection(bitmap);
addChild(reflect.container);
*/
package utils
{
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.Shape;
import flash.geom.Matrix;
import flash.display.GradientType;
//import flash.display.IBitmapDrawable;
//import flash.display.MovieClip;
public class Reflection
{
//public var reflectObject:DisplayObject;
private var srcWidth:Number;
private var srcHeight:Number;
private var gap:Number ;//两个相片间的距离
//private var bitmapData:BitmapData;
private var topImg:Bitmap;
private var reflImg:Bitmap;
private var ratio:Number;
private var maskingShape:Shape;
private var _container:Sprite;// 默认大小变为原来的1.6倍 影子和原图的比例为 3:5
public function Reflection(src:DisplayObject,_gap:Number=1,_ratio:Number=3/5)//影子和原图的比例为 3:5 即影子的高度变短了
{
gap=_gap;
srcWidth = src.width;
srcHeight = src.height;
this.ratio=_ratio;
var bitmapData:BitmapData = new BitmapData(srcWidth,srcHeight);
//var bmpData:BitmapData = new BitmapData(srcWidth,srcHeight*ratio);
bitmapData.draw(src);
// bmpData.draw(src);
topImg = new Bitmap(bitmapData);
reflImg = new Bitmap(bitmapData);
trace("现为:"+reflImg.height)
//reflImg.width=reflImg.width*ratio
/*trace("原为:"+reflImg.height)
reflImg.scaleY=reflImg.height*ratio
trace("现为:"+reflImg.height)*/
_container= new Sprite();
_container.addChild(topImg);
_container.addChild(reflImg);
reflImg.scaleY=ratio
//bitmapData.dispose();
//bitmapData=null;
init();
}
private function init():void
{
reflImg.scaleY = -1;
topImg.x = 0;
topImg.y = 0;
reflImg.x = topImg.x;
reflImg.y = topImg.y + (reflImg.height+topImg.height) + gap;
maskingShape=new Shape();
_container.addChild(maskingShape);
//trace(_container.height);
maskingShape.scaleY = -1;
maskingShape.x = reflImg.x;
maskingShape.y = reflImg.y;
drawInMask();
reflImg.cacheAsBitmap=true;
maskingShape.cacheAsBitmap=true;
reflImg.mask=maskingShape;
}
private function drawInMask():void
{
var mat:Matrix= new Matrix();
var colors:Array = [0xFF0000,0xFF0000];
var alphas:Array = [0,0.7];
var ratios:Array = [10,255];
mat.createGradientBox(srcWidth,srcHeight,90*(Math.PI/180));
maskingShape.graphics.lineStyle();
maskingShape.graphics.beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,mat);
maskingShape.graphics.drawRect(0,0,srcWidth,srcHeight);
maskingShape.graphics.endFill();
}
public function get container():Sprite{
return _container;
}
}
}