Flex动画效果与变换(三)[转]

这篇文章是Flex动画效果变换的最后一编了,这篇将会讲述Flex中的“变面”技术,即是Transitions!
如果看过Flex SDK里面的自带的例子程序,有一个叫“Flex Store”的应用,在里面的手机列表中看某一个手机的详细时,就是这种效果,不多说,这篇会比较简单,先看看效果:在线播放
看到了效果了吧,这种的变换不难实现,再来看看代码再解析:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” width=”695″ height=”555″>
<mx:states>
<mx:State name=”A”>
<mx:SetProperty target=”{windowA}” name=”width” value=”500″/>
<mx:SetProperty target=”{windowA}” name=”height” value=”300″/>
<mx:SetProperty target=”{windowC}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowC}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowC}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowD}” name=”x” value=”373″/>
<mx:SetProperty target=”{windowD}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowD}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowD}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowB}” name=”x” value=”23″/>
<mx:SetProperty target=”{windowB}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowB}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowB}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowC}” name=”x” value=”200″/>
</mx:State>
<mx:State name=”B”>
<mx:SetProperty target=”{windowD}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowD}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowC}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowC}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowA}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowA}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowB}” name=”width” value=”500″/>
<mx:SetProperty target=”{windowB}” name=”height” value=”300″/>
<mx:SetProperty target=”{windowA}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowC}” name=”x” value=”200″/>
<mx:SetProperty target=”{windowC}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowB}” name=”x” value=”23″/>
<mx:SetProperty target=”{windowD}” name=”x” value=”373″/>
<mx:SetProperty target=”{windowD}” name=”y” value=”333″/>
</mx:State>
<mx:State name=”C”>
<mx:SetProperty target=”{windowD}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowD}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowB}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowB}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowA}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowA}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowC}” name=”width” value=”500″/>
<mx:SetProperty target=”{windowC}” name=”height” value=”300″/>
<mx:SetProperty target=”{windowA}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowB}” name=”x” value=”200″/>
<mx:SetProperty target=”{windowB}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowC}” name=”x” value=”23″/>
<mx:SetProperty target=”{windowC}” name=”y” value=”19″/>
<mx:SetProperty target=”{windowD}” name=”x” value=”373″/>
<mx:SetProperty target=”{windowD}” name=”y” value=”333″/>
</mx:State>
<mx:State name=”D”>
<mx:SetProperty target=”{windowC}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowC}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowB}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowB}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowA}” name=”width” value=”150″/>
<mx:SetProperty target=”{windowA}” name=”height” value=”150″/>
<mx:SetProperty target=”{windowD}” name=”width” value=”500″/>
<mx:SetProperty target=”{windowD}” name=”height” value=”300″/>
<mx:SetProperty target=”{windowA}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowB}” name=”x” value=”200″/>
<mx:SetProperty target=”{windowB}” name=”y” value=”333″/>
<mx:SetProperty target=”{windowD}” name=”x” value=”23″/>
<mx:SetProperty target=”{windowD}” name=”y” value=”19″/>
<mx:SetProperty target=”{windowC}” name=”x” value=”373″/>
<mx:SetProperty target=”{windowC}” name=”y” value=”333″/>
</mx:State>
</mx:states>
<mx:transitions>
<mx:Transition fromState=”*” toState=”*”>
<mx:Parallel targets=”{[windowA, windowB, windowC, windowD]}”>
<mx:Move />
<mx:Resize />
</mx:Parallel>
</mx:Transition>
</mx:transitions>
<mx:TitleWindow x=”23″ y=”19″ width=”250″ height=”200″ layout=”absolute” title=”A” id=”windowA” click=”currentState=’A'” />
<mx:TitleWindow x=”309″ y=”19″ width=”250″ height=”200″ layout=”absolute” title=”B” id=”windowB” click=”currentState=’B'” />
<mx:TitleWindow x=”23″ y=”260″ width=”250″ height=”200″ layout=”absolute” title=”C” id=”windowC” click=”currentState=’C'” />
<mx:TitleWindow x=”309″ y=”260″ width=”250″ height=”200″ layout=”absolute” title=”D” id=”windowD” click=”currentState=’D'” />
</mx:Application>
代 码会比较多,我们先看看<mx:states>标签,它是一个集合,就是你的程序有多少个状态,什么是状态呢?我自已理解就即是有多少个“面 谱”,即是现在程序里面有四个显示界面状态,所以里面有四个<mx:State>标签,每个<mx:State>状态都需要有一 个名字name属性,以区分是哪个界面状态,在每个状态里面都有很多<mx:SetProperty>的标签,看英文都知道了,该标签用于设 置这个状态下的所有界面元素的属性(组件的属性),因为每个界面状态中各个组件的大小布局都不同,所以在状态标签里就要预先设置好该状态下的组件的位置与 大小,那个target=”{windowC}”属性就是设置你要设置的哪个组件的名字拉,name=”height”就是你要设置的属性value= “333″就是你要设置该属性的值,我们细心看看的话,可能会发现,每个状态里面设置的属性,都是width,height,x,y这四个属性,我们看看 上面的最终效果就知道无论切换哪个状态,组件间的变换来来去去都是移动位置与大小改变,就是说当你变换状态时,需要改动哪些属性的,就将它的目标值设置在 <mx:SetProperty>标签里。再看看下面的<mx:transitions>标签,这个也是个集合,里面放着不同的 变换方法<mx:Transition>,我们来看看变换标签的代码:
<mx:Transition fromState=”*” toState=”*”>
<mx:Parallel targets=”{[windowA, windowB, windowC, windowD]}”>
<mx:Move />
<mx:Resize />
</mx:Parallel>
</mx:Transition>
formState 与toState属性是要设置该状态变换是怎样触发的,里面要填上状态的名字,<mx:State name=”C”>   C就是状态的名字,即是如果你formState=”A”,toState=”C”的话,只有从A状态切换到C状态时,才会产生以上的变换动画效果,如果 不附合该规则如A切换到B状态的话,则只会按状态的属性设置值来直接生成视图,而没有动画渐变效果了。如果填上“*”的话,就是无论是哪个状态切换到哪 个,都会运行动画效果,至于变换期间用到哪种动画效果来进行渐变,就在它的下级标签里定义了,这里它用到了<mx:Parallel>并列播 放移动与重整大小的动画效果,之前文章讲过,这里不多说了。基本上,一个变换就做好了,但我们还需要触发它,也就是去改变程序当前的显示状态:click =”currentState=’A'” 在每个组件的click事件里,改变程序的currentState值,就是改变程序的当前显示状态!之后动画效果就会触发了!
原文地址:https://www.cnblogs.com/frogbag/p/1570864.html