Flex 4里的特效1- 基本效果
这篇文章是 Chet Haase 写的,原文在:http://www.adobe.com/devnet/flex/articles/flex4_effects_pt1.html。Chet Haase这哥们原来是sun的工程师,filthy rich clients 是他在swing 特效方面的一本书的网站 (这本书绝对是Swing effect开发的宝典了)。后来跑到Adobe去了,还是作特效方面的工作。最近也出了本Flex特效方面的书《Flex 4 Fun》(我只看了个样章,写得非常详实)。总之这哥们的文章是绝对有含金量的。对Flex graphic感兴趣的人强烈推荐去参观下他的blog。
对图形爱好者来说 ,Flex 特效是Flex平台里最酷也最有趣的部分。有了特效就可以容易地开发出酷的应用,同时增强用户的体验。
在Flex 4里,特效可以应用于任意的对象(不仅是UIComponent)和属性(不仅是数值类型的属性),既可以使用Flash Player的最新技术,从编程的角度也变得更强大和丰富。
Animate是所有特效的基础
Flex4里的所有特效都是Animate类的子类,而Animate是Effect的一个子类。Flex4里的特效类的层次是全新的,与Flex3里的
层次不能兼容,在Flex3里所有的特效类都是TweenEffect的一个子类。这2套特效库可以同时使用,老代码里的特效代码无需修改即可在Flex 4里运行。而Flex 4的开发人员就可以充分利用新的特效功能,这些新特效可用于旧的和新的组件,也可用于新的graphic元素,甚至是任意地对象。
新的Animate类提供了新的特效功能的通用功能,比如使特效能够应用于任意对象和类型的功能。Animate允许你用Animation子类来创建,操作和播放动画效果。Animation类包含了实际运行动画的功能,比如计算和修改动画属性的值。
使用Animate来来创建和使用特效是很简答的:首先要一个目标对象,以及这个对象的某些属性的名字,这些属性会被Animate类
修改来达到动画的效果。还有些可选的参数,比如效果持续的时间。一切都设置好后,调用play()来播放就是了。
下面的例子中,我们给一个按钮应用了动画效果,动画的内容是把按钮向右移动100个像素。
<s:Animate id="mover" target="{button}"> <s:SimpleMotionPath property="x" valueFrom="0" valueTo="100"/> </s:Animate> <s:Button id="button" click="mover.play()"/><s:Animate id="mover" target="{button}" duration="1000"> <s:SimpleMotionPath property="x" valueFrom="0" valueTo="100"/> <s:SimpleMotionPath property="y" valueTo="100"/> <s:SimpleMotionPath property="width" valueBy="20"/> </s:Animate><s:Move id="mover" target="{button}" xTo="100" yTo="200"/><s:Resize id="resizer" widthTo="100" heightTo="50" target="{button}"/><s:Parallel id="transformer" target="{button}"> <s:Move xFrom="50" xTo="150" autoCenterTransform="true"/> <s:Rotate angleFrom="0" angleTo="90" autoCenterTransform="true"/> <s:Scale scaleXFrom="1" scaleXTo="2" autoCenterTransform="true"/> </s:Parallel> <s:Button id="button" x="50" y="100" label="Transform Me" click="transformer.play()"/><s:Fade target="{button}" alphaTo="0"/><s:states> <s:State name="state1"/> <s:State name="state2"/> </s:states> <s:transitions> <s:Transition> <s:Fade targets="{[button0, button1, button2]}"/> </s:Transition> </s:transitions><s:Button id="button0" label="Visible" x="100" y="0" visible="true" visible.state2="false"/> <s:Button id="button1" label="Alpha" x="100" y="50" alpha="0" alpha.state2="1"/> <s:Button id="button2" label="Existence" x="100" y="100" includeIn="state2"/>
<s:Button label="Toggle State" click="currentState=(currentState=='state1')?'state2':'state1'"/>
<s:Group mouseDown="currentState='state2'" mouseUp="currentState='state1'"> <s:Ellipse x="50" y="50" width="100" height="100"> <s:fill> <s:RadialGradient> <s:GradientEntry id="center" color="0xf0f0f0" color.state2="0x808080" ratio="0"/> <s:GradientEntry id="edge" color="0x404040" ratio="1"/> </s:RadialGradient> </s:fill> </s:Ellipse> </s:Group><s:transitions> <s:Transition> <s:AnimateColor target="{center}" duration="150"/> </s:Transition></s:transitions>