如果看過Flex SDK里面的自帶的例子程序,有一個叫“Flex Store”的應(yīng)用,在里面的手機列表中看某一個手機的詳細時,就是這種效果,不多說,這篇會比較簡單,先看看效果:
看到了效果了吧,這種的變換不難實現(xiàn),再來看看代碼再解析:
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="695" height="555">
3 <mx:states>
4 <mx:State name="A">
5 <mx:SetProperty target="{windowA}" name="width" value="500"/>
6 <mx:SetProperty target="{windowA}" name="height" value="300"/>
7 <mx:SetProperty target="{windowC}" name="width" value="150"/>
8 <mx:SetProperty target="{windowC}" name="height" value="150"/>
9 <mx:SetProperty target="{windowC}" name="y" value="333"/>
10 <mx:SetProperty target="{windowD}" name="x" value="373"/>
11 <mx:SetProperty target="{windowD}" name="width" value="150"/>
12 <mx:SetProperty target="{windowD}" name="height" value="150"/>
13 <mx:SetProperty target="{windowD}" name="y" value="333"/>
14 <mx:SetProperty target="{windowB}" name="x" value="23"/>
15 <mx:SetProperty target="{windowB}" name="y" value="333"/>
16 <mx:SetProperty target="{windowB}" name="width" value="150"/>
17 <mx:SetProperty target="{windowB}" name="height" value="150"/>
18 <mx:SetProperty target="{windowC}" name="x" value="200"/>
19 </mx:State>
20 <mx:State name="B">
21 <mx:SetProperty target="{windowD}" name="width" value="150"/>
22 <mx:SetProperty target="{windowD}" name="height" value="150"/>
23 <mx:SetProperty target="{windowC}" name="width" value="150"/>
24 <mx:SetProperty target="{windowC}" name="height" value="150"/>
25 <mx:SetProperty target="{windowA}" name="width" value="150"/>
26 <mx:SetProperty target="{windowA}" name="height" value="150"/>
27 <mx:SetProperty target="{windowB}" name="width" value="500"/>
28 <mx:SetProperty target="{windowB}" name="height" value="300"/>
29 <mx:SetProperty target="{windowA}" name="y" value="333"/>
30 <mx:SetProperty target="{windowC}" name="x" value="200"/>
31 <mx:SetProperty target="{windowC}" name="y" value="333"/>
32 <mx:SetProperty target="{windowB}" name="x" value="23"/>
33 <mx:SetProperty target="{windowD}" name="x" value="373"/>
34 <mx:SetProperty target="{windowD}" name="y" value="333"/>
35 </mx:State>
36 <mx:State name="C">
37 <mx:SetProperty target="{windowD}" name="width" value="150"/>
38 <mx:SetProperty target="{windowD}" name="height" value="150"/>
39 <mx:SetProperty target="{windowB}" name="width" value="150"/>
40 <mx:SetProperty target="{windowB}" name="height" value="150"/>
41 <mx:SetProperty target="{windowA}" name="width" value="150"/>
42 <mx:SetProperty target="{windowA}" name="height" value="150"/>
43 <mx:SetProperty target="{windowC}" name="width" value="500"/>
44 <mx:SetProperty target="{windowC}" name="height" value="300"/>
45 <mx:SetProperty target="{windowA}" name="y" value="333"/>
46 <mx:SetProperty target="{windowB}" name="x" value="200"/>
47 <mx:SetProperty target="{windowB}" name="y" value="333"/>
48 <mx:SetProperty target="{windowC}" name="x" value="23"/>
49 <mx:SetProperty target="{windowC}" name="y" value="19"/>
50 <mx:SetProperty target="{windowD}" name="x" value="373"/>
51 <mx:SetProperty target="{windowD}" name="y" value="333"/>
52 </mx:State>
53 <mx:State name="D">
54 <mx:SetProperty target="{windowC}" name="width" value="150"/>
55 <mx:SetProperty target="{windowC}" name="height" value="150"/>
56 <mx:SetProperty target="{windowB}" name="width" value="150"/>
57 <mx:SetProperty target="{windowB}" name="height" value="150"/>
58 <mx:SetProperty target="{windowA}" name="width" value="150"/>
59 <mx:SetProperty target="{windowA}" name="height" value="150"/>
60 <mx:SetProperty target="{windowD}" name="width" value="500"/>
61 <mx:SetProperty target="{windowD}" name="height" value="300"/>
62 <mx:SetProperty target="{windowA}" name="y" value="333"/>
63 <mx:SetProperty target="{windowB}" name="x" value="200"/>
64 <mx:SetProperty target="{windowB}" name="y" value="333"/>
65 <mx:SetProperty target="{windowD}" name="x" value="23"/>
66 <mx:SetProperty target="{windowD}" name="y" value="19"/>
67 <mx:SetProperty target="{windowC}" name="x" value="373"/>
68 <mx:SetProperty target="{windowC}" name="y" value="333"/>
69 </mx:State>
70 </mx:states>
71 <mx:transitions>
72 <mx:Transition fromState="*" toState="*">
73 <mx:Parallel targets="{[windowA, windowB, windowC, windowD]}">
74 <mx:Move />
75 <mx:Resize />
76 </mx:Parallel>
77 </mx:Transition>
78 </mx:transitions>
79 <mx:TitleWindow x="23" y="19" width="250" height="200" layout="absolute" title="A" id="windowA" click="currentState='A'" />
80 <mx:TitleWindow x="309" y="19" width="250" height="200" layout="absolute" title="B" id="windowB" click="currentState='B'" />
81 <mx:TitleWindow x="23" y="260" width="250" height="200" layout="absolute" title="C" id="windowC" click="currentState='C'" />
82 <mx:TitleWindow x="309" y="260" width="250" height="200" layout="absolute" title="D" id="windowD" click="currentState='D'" />
83 </mx:Application>
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="695" height="555">
3 <mx:states>
4 <mx:State name="A">
5 <mx:SetProperty target="{windowA}" name="width" value="500"/>
6 <mx:SetProperty target="{windowA}" name="height" value="300"/>
7 <mx:SetProperty target="{windowC}" name="width" value="150"/>
8 <mx:SetProperty target="{windowC}" name="height" value="150"/>
9 <mx:SetProperty target="{windowC}" name="y" value="333"/>
10 <mx:SetProperty target="{windowD}" name="x" value="373"/>
11 <mx:SetProperty target="{windowD}" name="width" value="150"/>
12 <mx:SetProperty target="{windowD}" name="height" value="150"/>
13 <mx:SetProperty target="{windowD}" name="y" value="333"/>
14 <mx:SetProperty target="{windowB}" name="x" value="23"/>
15 <mx:SetProperty target="{windowB}" name="y" value="333"/>
16 <mx:SetProperty target="{windowB}" name="width" value="150"/>
17 <mx:SetProperty target="{windowB}" name="height" value="150"/>
18 <mx:SetProperty target="{windowC}" name="x" value="200"/>
19 </mx:State>
20 <mx:State name="B">
21 <mx:SetProperty target="{windowD}" name="width" value="150"/>
22 <mx:SetProperty target="{windowD}" name="height" value="150"/>
23 <mx:SetProperty target="{windowC}" name="width" value="150"/>
24 <mx:SetProperty target="{windowC}" name="height" value="150"/>
25 <mx:SetProperty target="{windowA}" name="width" value="150"/>
26 <mx:SetProperty target="{windowA}" name="height" value="150"/>
27 <mx:SetProperty target="{windowB}" name="width" value="500"/>
28 <mx:SetProperty target="{windowB}" name="height" value="300"/>
29 <mx:SetProperty target="{windowA}" name="y" value="333"/>
30 <mx:SetProperty target="{windowC}" name="x" value="200"/>
31 <mx:SetProperty target="{windowC}" name="y" value="333"/>
32 <mx:SetProperty target="{windowB}" name="x" value="23"/>
33 <mx:SetProperty target="{windowD}" name="x" value="373"/>
34 <mx:SetProperty target="{windowD}" name="y" value="333"/>
35 </mx:State>
36 <mx:State name="C">
37 <mx:SetProperty target="{windowD}" name="width" value="150"/>
38 <mx:SetProperty target="{windowD}" name="height" value="150"/>
39 <mx:SetProperty target="{windowB}" name="width" value="150"/>
40 <mx:SetProperty target="{windowB}" name="height" value="150"/>
41 <mx:SetProperty target="{windowA}" name="width" value="150"/>
42 <mx:SetProperty target="{windowA}" name="height" value="150"/>
43 <mx:SetProperty target="{windowC}" name="width" value="500"/>
44 <mx:SetProperty target="{windowC}" name="height" value="300"/>
45 <mx:SetProperty target="{windowA}" name="y" value="333"/>
46 <mx:SetProperty target="{windowB}" name="x" value="200"/>
47 <mx:SetProperty target="{windowB}" name="y" value="333"/>
48 <mx:SetProperty target="{windowC}" name="x" value="23"/>
49 <mx:SetProperty target="{windowC}" name="y" value="19"/>
50 <mx:SetProperty target="{windowD}" name="x" value="373"/>
51 <mx:SetProperty target="{windowD}" name="y" value="333"/>
52 </mx:State>
53 <mx:State name="D">
54 <mx:SetProperty target="{windowC}" name="width" value="150"/>
55 <mx:SetProperty target="{windowC}" name="height" value="150"/>
56 <mx:SetProperty target="{windowB}" name="width" value="150"/>
57 <mx:SetProperty target="{windowB}" name="height" value="150"/>
58 <mx:SetProperty target="{windowA}" name="width" value="150"/>
59 <mx:SetProperty target="{windowA}" name="height" value="150"/>
60 <mx:SetProperty target="{windowD}" name="width" value="500"/>
61 <mx:SetProperty target="{windowD}" name="height" value="300"/>
62 <mx:SetProperty target="{windowA}" name="y" value="333"/>
63 <mx:SetProperty target="{windowB}" name="x" value="200"/>
64 <mx:SetProperty target="{windowB}" name="y" value="333"/>
65 <mx:SetProperty target="{windowD}" name="x" value="23"/>
66 <mx:SetProperty target="{windowD}" name="y" value="19"/>
67 <mx:SetProperty target="{windowC}" name="x" value="373"/>
68 <mx:SetProperty target="{windowC}" name="y" value="333"/>
69 </mx:State>
70 </mx:states>
71 <mx:transitions>
72 <mx:Transition fromState="*" toState="*">
73 <mx:Parallel targets="{[windowA, windowB, windowC, windowD]}">
74 <mx:Move />
75 <mx:Resize />
76 </mx:Parallel>
77 </mx:Transition>
78 </mx:transitions>
79 <mx:TitleWindow x="23" y="19" width="250" height="200" layout="absolute" title="A" id="windowA" click="currentState='A'" />
80 <mx:TitleWindow x="309" y="19" width="250" height="200" layout="absolute" title="B" id="windowB" click="currentState='B'" />
81 <mx:TitleWindow x="23" y="260" width="250" height="200" layout="absolute" title="C" id="windowC" click="currentState='C'" />
82 <mx:TitleWindow x="309" y="260" width="250" height="200" layout="absolute" title="D" id="windowD" click="currentState='D'" />
83 </mx:Application>
代碼會比較多,我們先看看<mx:states>標簽,它是一個集合,就是你的程序有多少個狀態(tài),什么是狀態(tài)呢?我自已理解就即是有多少個“面譜”,即是現(xiàn)在程序里面有四個顯示界面狀態(tài),所以里面有四個<mx:State>標簽,每個<mx:State>狀態(tài)都需要有一個名字name屬性,以區(qū)分是哪個界面狀態(tài),在每個狀態(tài)里面都有很多<mx:SetProperty>的標簽,看英文都知道了,該標簽用于設(shè)置這個狀態(tài)下的所有界面元素的屬性(組件的屬性),因為每個界面狀態(tài)中各個組件的大小布局都不同,所以在狀態(tài)標簽里就要預(yù)先設(shè)置好該狀態(tài)下的組件的位置與大小,那個target="{windowC}"屬性就是設(shè)置你要設(shè)置的哪個組件的名字拉,name="height"就是你要設(shè)置的屬性value="333"就是你要設(shè)置該屬性的值,我們細心看看的話,可能會發(fā)現(xiàn),每個狀態(tài)里面設(shè)置的屬性,都是width,height,x,y這四個屬性,我們看看上面的最終效果就知道無論切換哪個狀態(tài),組件間的變換來來去去都是移動位置與大小改變,就是說當你變換狀態(tài)時,需要改動哪些屬性的,就將它的目標值設(shè)置在<mx:SetProperty>標簽里。再看看下面的<mx:transitions>標簽,這個也是個集合,里面放著不同的變換方法<mx:Transition>,我們來看看變換標簽的代碼:
1 <mx:Transition fromState="*" toState="*">
2 <mx:Parallel targets="{[windowA, windowB, windowC, windowD]}">
3 <mx:Move />
4 <mx:Resize />
5 </mx:Parallel>
6 </mx:Transition>
2 <mx:Parallel targets="{[windowA, windowB, windowC, windowD]}">
3 <mx:Move />
4 <mx:Resize />
5 </mx:Parallel>
6 </mx:Transition>
formState與toState屬性是要設(shè)置該狀態(tài)變換是怎樣觸發(fā)的,里面要填上狀態(tài)的名字,<mx:State name="C"> C就是狀態(tài)的名字,即是如果你formState="A",toState="C"的話,只有從A狀態(tài)切換到C狀態(tài)時,才會產(chǎn)生以上的變換動畫效果,如果不附合該規(guī)則如A切換到B狀態(tài)的話,則只會按狀態(tài)的屬性設(shè)置值來直接生成視圖,而沒有動畫漸變效果了。如果填上“*”的話,就是無論是哪個狀態(tài)切換到哪個,都會運行動畫效果,至于變換期間用到哪種動畫效果來進行漸變,就在它的下級標簽里定義了,這里它用到了<mx:Parallel>并列播放移動與重整大小的動畫效果,之前文章講過,這里不多說了。基本上,一個變換就做好了,但我們還需要觸發(fā)它,也就是去改變程序當前的顯示狀態(tài):click="currentState='A'" 在每個組件的click事件里,改變程序的currentState值,就是改變程序的當前顯示狀態(tài)!之后動畫效果就會觸發(fā)了!
遲點有時間,再做一個3D的動畫效果,將會用到PV3D的框架,再整理一個代碼與教程也貼出來跟大家分享吧,不過不知道大家對PV3D這個東東熟悉不,不熟悉的話,可能看得痛苦點,至于PV3D方面的教程,我也看看抽點時間寫寫吧!先謝過大家的支持!