第一个在这里下载源码 : http://www.flashandmath.com/flashcs4/leo/
用途,对相片进行折叠 很不错的特技!而其CPU占率很低 。很实用
下面是自己整理的一些解释
/*
ActionScript 3 Tutorials by Dan Gries and Barbara Kaskosz.
See the tutorial at
http://www.flashandmath.com/flashcs4/leo/
for explanations how to use the class.
Last modified: March 28, 2010.
官方解释:
What you see in the applet linked from the screen shot above, are two instances of ImageAccordion class, one of each image.
The ImageAccordion class extends Sprtite. Its constructor, evoked with the keyword 'new', takes four parameters, three optional:
new ImageAccordion(bd:BitmapData,df:Number=0.5, lf:Number=0, sl:int=10)
The constructor takes four parameters (three are optional). The first parameter is the BitmapData object
for your image. You can get it by importing an image to the Library in Flash and linking to AS3 or by embedding
in Flex or by loading at runtime. 'df' and 'lf' will be assigned as values for two variables within the class
'darkEdgeFactor' and 'lightEdgeFactor'. Those values will determine the amount of darkening for the bottom edges
of the accordion and the amount of lightening for the top edges. 'df' works best if it is between 0 and 1, the larger 'df' the
more darkening. Similarly 'lf' works best if it is between 0 and 1. Again the larger 'lf' the more lightening. The default
settings work well with light images. The last parameter in the number of wedges in your accordion. sl指的是照片被分割的块数(即进行折叠的片数),默认值为10块
In the fla file corresponding to the example above, we imported the two images to the Library and linked them to AS3 under the
names of Leonardo and MonaLisa. Then we created two instances of ImageAccordion, one for each image. Here is the Timeline code
that creates the first instance. (The folder flashandmath contained in the zip file has to be in the same folder as your fla
file so the class ImageAccordion can be found.)
import flashandmath.as3.ImageAccordion;
var accordion:ImageAccordion=new ImageAccordion(new Leonardo(224,350),0.5,0,14);/// 创建对象
this.addChild(accordion);
accordion.x=160;
accordion.y=235;
We rotate the first accordion by 30 degrees and push it back along the z-axis so it becomes smaller and sharper.
accordion.rotationY=-30;
accordion.z=100;
accordion.filters=[new DropShadowFilter(4.0,45,0x999999)];
We do this similarly for MonaLisa, just positon it more to the right and rotate by 30 degrees.
The text that you see on the Stage was created at authoring time, but, of course, you could use dynamic
text fields instead and load their content at runtime from an external file.
The ImageAccordion class has two public properties: tweenDownDuration and tweenUpDuration (in number of frames).
They are set by default to 50 and 40. You can change it easily. For example, by setting:
accordion.tweenUpDuration=20;
*/
package utils.flashandmath {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.ColorTransform;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
public class ImageAccordion extends Sprite {
private var numSlices:int;
private var imgData:BitmapData;
private var sliceWidth:Number;
private var sliceHeight:Number;
private var picWidth:Number;
private var picHeight:Number;
private var sliceBmps:Array;
private var container:Sprite;
private var isMoving:Boolean;
private var maxAng:Number;
private var paramTween:Tween;
public var tweenDownDuration:Number;
public var tweenUpDuration:Number;
private var paramObj:Object;
private var isClosed:Boolean;
private var isOpen:Boolean;
private var lightEdgeFactor:Number;
private var darkEdgeFactor:Number;
/*
Constructor begins. The constructor takes four parameters (three are optional).
The first parameter is the BitmapData object for your image. You can get it by importing
an image to the Library in Flash and linking to AS3 or by embedding in Flex or
by loading at runtime. 'df' and 'lf' will be assigned as values for 'darkEdgeFactor'
and 'lightEdgeFactor'. Those values will determine the amount of darkening for the bottom
edges of the accordion and the amount of lightening for the top edges. 'df' works best if it is between
0 and 1, the larger 'df' the more darkening. Similarly 'lf' works best if it is between 0 and 2.
Again the larger 'lf' the more lightening. The default settings work well with light images.
The last parameter in the number of wedges in your accordion.
*/
public function ImageAccordion(bd:BitmapData,df:Number=0.5, lf:Number=0, sl:int=10) {
numSlices=sl;
imgData=bd.clone();
picWidth=imgData.width;
picHeight=imgData.height;
sliceHeight=picHeight/numSlices;
sliceWidth=picWidth;
tweenDownDuration = 50;
tweenUpDuration = 40;
maxAng=80;
paramObj = {t:maxAng};
container=new Sprite();
this.addChild(container);
container.x=-picWidth/2;
container.y=-picHeight/2;
createSlices();
setUpListeners();
isClosed=true;
isOpen=false;
isMoving=false;
lightEdgeFactor=lf;
darkEdgeFactor=df;
foldAccordion(maxAng);
}
//End of constructor.
private function createSlices():void {
var k:int;
var slices:Array=[];
sliceBmps=[];
for(k=0;k<numSlices;k++){
slices[k]=new BitmapData(sliceWidth,sliceHeight);
slices[k].copyPixels(imgData,new Rectangle(0,k*sliceHeight,sliceWidth,sliceHeight),new Point(0,0));
sliceBmps[k]=new Bitmap(slices[k]);
container.addChild(sliceBmps[k]);
sliceBmps[k].x=0;
sliceBmps[k].y=sliceHeight*k;
sliceBmps[k].z=0;
}
}
private function foldAccordion(deg:Number):void {
var k:int;
var evenFactor:Number;
var oddFactor:Number;
evenFactor=(-darkEdgeFactor/maxAng)*deg+1;
oddFactor=(lightEdgeFactor/maxAng)*deg+1;
var rad:Number=deg*Math.PI/180;
for(k=0;k<numSlices;k++){
sliceBmps[k].rotationX=deg*Math.pow(-1,k);
if(k%2==1){
sliceBmps[k].z=sliceHeight*Math.sin(rad);
sliceBmps[k].y=sliceHeight*Math.cos(rad)*k;
sliceBmps[k].transform.colorTransform=new ColorTransform(oddFactor,oddFactor,oddFactor,1,0,0,0,0);
} else {
sliceBmps[k].z=0;
sliceBmps[k].y=sliceHeight*Math.cos(rad)*k;
sliceBmps[k].transform.colorTransform=new ColorTransform(evenFactor,evenFactor,evenFactor,1,0,0,0,0);
}
}
}
private function setUpListeners():void {
container.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(e:MouseEvent):void {
if(isMoving){
return;
}
if (isClosed) {
isMoving=true;
paramTween = new Tween(paramObj, "t", Bounce.easeOut, maxAng, 0, tweenDownDuration);
paramTween.addEventListener(TweenEvent.MOTION_CHANGE, goDown);
paramTween.addEventListener(TweenEvent.MOTION_FINISH, goDownComplete);
}
if(isOpen){
isMoving=true;
paramTween = new Tween(paramObj, "t", Bounce.easeOut, 0, maxAng, tweenUpDuration);
paramTween.addEventListener(TweenEvent.MOTION_CHANGE, goUp);
paramTween.addEventListener(TweenEvent.MOTION_FINISH, goUpComplete);
}
}
private function goDown(tevt:TweenEvent):void {
foldAccordion(paramObj.t);
}
private function goDownComplete(tevt:TweenEvent):void {
isMoving=false;
isClosed=false;
isOpen=true;
paramTween.removeEventListener(TweenEvent.MOTION_CHANGE, goDown);
paramTween.removeEventListener(TweenEvent.MOTION_FINISH, goDownComplete);
}
private function goUp(tevt:TweenEvent):void {
foldAccordion(paramObj.t);
}
private function goUpComplete(tevt:TweenEvent):void {
isMoving=false;
isClosed=true;
isOpen=false;
paramTween.removeEventListener(TweenEvent.MOTION_CHANGE, goUp);
paramTween.removeEventListener(TweenEvent.MOTION_FINISH, goUpComplete);
}
}
}
===================================================================
对文本进行折叠 该源码在 http://www.flashandmath.com/flashcs5/textaccordion/index.html
用途: 对文字进行折叠,非常不错,而且CPU占率也很低
自己的整理:
/*
var textMarkup2:String = "<html>" +
"<p>" +
"The text used in this example is typed in " +
"standard html format, but the Flash TLF Text converter can also import plain text " +
"or text written in the Adobe Text Layout format. " +
"Since this is html text, we can " +
"easily insert <a href='http://www.flashandmath.com'>links</a>." +
"</p>" +
"</html>";
用法:
accordion1 = new TextAccordion();//创建该类实例
this.addChild(accordion1); //添加进显示列表 一定要再其添加进显示列表后再传入文本
accordion1.x = 125;//坐标
accordion1.y = 45;//坐标
accordion1.z = 45;//坐标
accordion1.yRotationStart = -30;//该实例的初始旋转角度(tween之前的角度)
accordion1.yRotationEnd = -30;//click事件后,tween滚动后的该实例的角度
accordion1.fontSize = 15;
accordion1.lineHeight = 30;
accordion1.fontSize = 15;//文本字体的大小
accordion1.lineHeight = 30;//行高,即每一行之间间隔的距离
accordion1.paperColor = 0x97BC71;//该实例的背景颜色
accordion1.textPixelWidth = 240;//该实例的宽度
accordion1.tweenDurationDown = 20;//向下发生teewn滚动事件所花的时间
accordion1.tweenDurationUp = 15;//向上发生teewn滚动事件所花的时间
accordion1.markupType = "html";//所使用的文本类型 ,还有TextLayout和其他的类型
accordion1.importText(textMarkup1);//将文字textMarkup1装载到该实例中 textMarkup1 文字见上面
*/
package utils.flashandmath {
//Import statements
import flashx.textLayout.compose.StandardFlowComposer;
import flashx.textLayout.container.ContainerController;
import flashx.textLayout.conversion.TextConverter;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.formats.TextAlign;
import flashx.textLayout.edit.EditManager;
import flashx.textLayout.edit.EditingMode;
import flash.events.Event;
import flash.display.Sprite;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.geom.Matrix;
import flashx.textLayout.compose.TextFlowLine;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.display.BlendMode;
import flashx.textLayout.edit.SelectionManager;
public class TextAccordion extends Sprite {
//Variables
public var markupType:String;
public var flow:TextFlow;
public var numContainers:int;
public var textPixelWidth:Number;
public var yRotationStart:Number;
public var yRotationEnd:Number;
public var cosineStart:Number;
public var cosineEnd:Number;
public var paperColor:uint;
public var tweenDurationDown:Number;
public var tweenDurationUp:Number;
public var fontSize:Number;
public var lineHeight:Number;
public var paddingLeft:Number;
public var paddingRight:Number;
public var shadeAmountStart:Number;
public var shadeAmountEnd:Number;
public var shadeColor:uint;
private var containers:Vector.<Sprite>;
private var controllers:Vector.<ContainerController>;
private var initYPos:Vector.<Number>;
private var finalYPos:Vector.<Number>;
private var paramObj:Object;
private var containerHolders:Vector.<Sprite>;
private var moveDown:Boolean;
private var cosine:Number;
private var paramTween:Tween;
private var darknessOverlays:Vector.<Shape>;
private var _numLinesPerSlat:int;
function TextAccordion():void {
fontSize = 14;
lineHeight = 28;
paddingLeft = 15;
paddingRight = 15;
tweenDurationDown = 25;
tweenDurationUp = 20;
textPixelWidth = 240;
yRotationStart = -30;
yRotationEnd = -30;
cosineStart = 0.07;
cosineEnd = 1;
shadeAmountStart = 1;
shadeAmountEnd = 0.15;
shadeColor = 0x000000;
paperColor = 0xF8E9BF;
_numLinesPerSlat = 1;
markupType = "html";
containers = new Vector.<Sprite>;
containerHolders = new Vector.<Sprite>;
controllers = new Vector.<ContainerController>;
darknessOverlays = new Vector.<Shape>;
flow = new TextFlow();
flow.flowComposer = new StandardFlowComposer();
paramObj = {t:0};
initYPos = new Vector.<Number>;
finalYPos = new Vector.<Number>;
//We will disallow mouse clicks until the accordion is open. this will prevent clicks on hypertext
//while the paper is folded up.
this.mouseChildren = false;
}
public function set numLinesPerSlat(n:Number):void {
_numLinesPerSlat = Math.max(1,Math.floor(n));
}
public function get numLinesPerslat():Number {
return _numLinesPerSlat;
}
public function importText(markup:String):void {
if (markupType == "html") {
flow = TextConverter.importToFlow(markup, TextConverter.TEXT_FIELD_HTML_FORMAT);
}
else if (markupType == "TextLayout") {
flow = TextConverter.importToFlow(markup, TextConverter.TEXT_LAYOUT_FORMAT);
}
else {
flow = TextConverter.importToFlow(markup, TextConverter.PLAIN_TEXT_FORMAT);
}
flow.fontSize = fontSize;
flow.lineHeight = lineHeight;
flow.paddingTop = (flow.lineHeight - flow.fontSize)/2;
flow.paddingLeft = paddingLeft;
flow.paddingRight = paddingRight;
var slatHeight:Number = _numLinesPerSlat*flow.lineHeight;
var slatWidth:Number = textPixelWidth;
//We will keep adding containers for the text until we have enough to contain all lines of text.
//After each container is added, we compose the text again, and see if the number of lines
//is less than or equal to the number of containers.
var i:Number = 0;
do {
//Display objects
var thisContainer:Sprite = new Sprite();
var thisHolder:Sprite = new Sprite();
var thisDarknessOverlay:Shape = new Shape();
thisHolder.addChild(thisContainer);
thisHolder.addChild(thisDarknessOverlay);
this.addChild(thisHolder);
//Positioning of objects
thisHolder.x = 0;
thisHolder.y = 0;
thisHolder.rotationY = yRotationStart;//旋转角度
initYPos.push(cosineStart*slatHeight/2+cosineStart*slatHeight*i);
finalYPos.push(slatHeight/2+cosineEnd*slatHeight*i);
thisHolder.y = initYPos[i];
if (i % 2 == 0) {
thisHolder.rotationX = 180/Math.PI*Math.acos(cosineStart);
}
else {
thisHolder.rotationX = -180/Math.PI*Math.acos(cosineStart);
}
thisContainer.y = -slatHeight/2;
//Building Vectors for later reference of objects
containers.push(thisContainer);
containerHolders.push(thisHolder);
darknessOverlays.push(thisDarknessOverlay);
thisDarknessOverlay.alpha = shadeAmountStart;
//Drawing
thisHolder.graphics.beginFill(paperColor);
thisHolder.graphics.drawRect(0,-slatHeight/2,slatWidth,slatHeight);
thisHolder.graphics.endFill();
var gradMat:Matrix = new Matrix();
gradMat.createGradientBox(slatWidth,slatHeight,Math.PI/2,0,-slatHeight/2);
if (i % 2 == 0) {
thisDarknessOverlay.graphics.beginGradientFill("linear", [shadeColor, shadeColor], [0,1], [0,255], gradMat);
thisDarknessOverlay.graphics.drawRect(0,-slatHeight/2,slatWidth,slatHeight);
thisDarknessOverlay.graphics.endFill();
}
else {
thisDarknessOverlay.graphics.beginGradientFill("linear", [shadeColor, shadeColor], [1,0], [0,255], gradMat);
thisDarknessOverlay.graphics.drawRect(0,-slatHeight/2,slatWidth,slatHeight);
thisDarknessOverlay.graphics.endFill();
}
var lineColor:uint;
var lineAlpha:Number;
if (i > 0) {
if ((i % 2) == 0) {
lineColor = 0xFFFFFF;
lineAlpha = 0.2;
}
else {
lineColor = 0x000000;
lineAlpha = 0.1;
}
thisDarknessOverlay.graphics.lineStyle(1,lineColor,lineAlpha);
thisDarknessOverlay.graphics.moveTo(0,-slatHeight/2);
thisDarknessOverlay.graphics.lineTo(slatWidth, -slatHeight/2);
}
//Each individual segment of the folded paper is a separate Sprite with its own
//ContainerController
var thisController:ContainerController = new ContainerController(thisContainer, textPixelWidth, slatHeight);
flow.flowComposer.addController(thisController);
controllers.push(thisController);
//After updating all controllers, the text will be distributed to each of the container sprites.
//If there are not enough containers to contain all of the text lines, then this do-while loop
//will continue.
flow.flowComposer.updateAllControllers();
i++;
} while(flow.flowComposer.numLines > flow.flowComposer.numControllers*_numLinesPerSlat);
numContainers = flow.flowComposer.numControllers;
moveDown = true;
this.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(evt:MouseEvent):void {
if (paramTween != null) {
paramTween.stop();
}
var currentValue = paramObj.t;
if (moveDown) {
paramTween = new Tween(paramObj, "t", Bounce.easeOut, currentValue, 1, tweenDurationDown);
}
else {
this.mouseChildren = false;
paramTween = new Tween(paramObj, "t", Bounce.easeOut, currentValue, 0, tweenDurationUp);
}
moveDown = !moveDown;
//this.stage.removeEventListener(MouseEvent.CLICK, onClick); //prevent clicks until after the tween is complete
paramTween.addEventListener(TweenEvent.MOTION_FINISH, tweenComplete);
paramTween.addEventListener(TweenEvent.MOTION_CHANGE, onEnter);
}
function onEnter(evt:Event):void {
for (var i:int = 0; i< numContainers; i++) {
cosine = cosineStart + paramObj.t*(cosineEnd - cosineStart);
containerHolders[i].y = initYPos[i] + paramObj.t*(finalYPos[i] - initYPos[i]);
containerHolders[i].rotationY = yRotationStart + paramObj.t*(yRotationEnd - yRotationStart);
if (i % 2 == 0) {
containerHolders[i].rotationX = 180/Math.PI*Math.acos(cosine);
}
else {
containerHolders[i].rotationX = -180/Math.PI*Math.acos(cosine);
}
darknessOverlays[i].alpha = shadeAmountStart + (shadeAmountStart-shadeAmountEnd)/(cosineStart-cosineEnd)*(cosine-cosineStart);
}
}
function tweenComplete(evt:TweenEvent):void {
if (!moveDown) {
this.mouseChildren = true;
}
paramTween.removeEventListener(TweenEvent.MOTION_CHANGE, onEnter);
}
}
}