再此之前一段时间我曾分享过一个Flex版本的倒影类,接受uicompnent组件作为倒影目标,参考《Flex)一个Flex倒影组件类 Reflactor.as 的使用》!因为用到了一些mx类包下的类及事件,对纯flash组件无法直接支持。
这篇将分享一个ActionScript3 所写的倒影类,支持DisplayObject做为倒影目标,未用到flex相关类包
直接上代码后再解释:
/**
* 倒影类 Reflexion
*/
package taotao.bitmap {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Reflexion extends Sprite {
private var _disTarget:DisplayObject;
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
/**
* 构造函数
* @param set_disTarget 目标
* @param set_numStartFade
* @param set_numMidLoc
* @param set_numEndFade
* @param set_numSkewX
* @param set_numScale
*
*/
public function Reflexion(set_disTarget:DisplayObject, set_numStartFade:Number=0.4, set_numMidLoc:Number=0.2, set_numEndFade:Number=0, set_numSkewX:Number=0, set_numScale:Number=1) {
super()
_disTarget = set_disTarget;
_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, _disTarget.height, true, 0);
var matSkew:Matrix = new Matrix(1, 0, _numSkewX, -1 * _numScale, 0, _disTarget.height);
var recDraw:Rectangle = new Rectangle(0, 0, _disTarget.width, _disTarget.height * (2 - _numScale));
var potSkew:Point = matSkew.transformPoint(new Point(0, _disTarget.height));
matSkew.tx = potSkew.x * -1;
matSkew.ty = (potSkew.y - _disTarget.height) * -1;
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, _disTarget.height, 0.5 * Math.PI);
shpDraw.graphics.beginGradientFill(GradientType.LINEAR, new Array(0,0,0), arrAlpha, arrMatrix, matGrad)
shpDraw.graphics.drawRect(0, 0, _disTarget.width, _disTarget.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;
}
public function reDraw():void
{
this.createReflection();
}
}
}
说明:
1、以上类参考了Dustin Andrew所写的倒影类,同时增加了一个reDraw方法,用于根据需要重绘倒影
2、使用时用类似如下方法:
var myRef:Reflexion = new Reflexion(disImg, .4, .2, 0, 0, 1);
sprCon.addChild(myRef);
其中disImg是目标显示对象,sprCon是disImg的容器,同时也将是倒影的容器。这是一个范例效果截图:
![](https://img-my.csdn.net/uploads/201303/07/1362642388_5098.jpg)
更改你的Reflexion参数数值以达到不同效果!
(本文完)