範例 滾動的球
建立一個來回滾動的球。
1
)啓動
Microsoft Expression Blend
,建
Silverlight
項目,並將其打開以供編輯。您如今便可在項目的主頁
(MainPage.xaml)
中建立內容。
2
)在
"
工具
"
面板中,選擇其中橢圓形工具,如圖圖
11.3-1
。
圖
11.3-1
3
)美工板上,經過拖動鼠標在
MainPage.xaml
的設計界面中繪製一個橢圓,並在「屬性面板」的佈局中設置其下列屬性,如圖
11.3-2
Height=90
,
Width=300
,
StrokeThickness=0
如圖
11.3-2
在按住
Shift
的同時進行拖動,可以使高度和寬度保持相同。這樣作可在繪製矩形時生成正方形,在繪製橢圓時生成圓形。
在按住
Alt
的同時進行拖動,可將點擊的第一個點做爲中心點,而不是以該點做爲所繪形狀的左上角。
4
)在「屬性面板」的畫筆下單擊
Fill
,再單擊「漸變畫筆」標籤,接着單擊「徑向漸變」畫筆,見如圖
11.3-3
如圖
11.3-3
5
)單擊「工具箱」中的「漸變工具」
,將會看到一個漸變箭頭,如圖
11.3-6
。
11.3-6
6
)鼠標調整漸變畫筆的中心點,完成後效果以下圖
11.3-7
:
11.3-7
7
)重複
8
)、
9
),再繪製一個橢圓,並設置其下列屬性,完成後效果如圖
11.3-8
:
Height=70
,
Width=240
,
StrokeThickness=0
圖
11.3-8
8
)按住
Shift
,單擊「工具箱」中的「選擇」工具,選中兩個橢圓。
9
)在「對象」菜單上,單擊合併、排斥。
若要將全部形狀或路徑均合併到單一對象中,請單擊
"
相併
"
。
若要根據相交部分剪切形狀或路徑,但保留全部未相交部分,請單擊
"
相割
"
若要保留對象的重疊區域並刪除不重疊區域,請單擊
"
相交
"
。
若要保留非重疊區域並刪除重疊區域,請單擊
"
相斥
"
。
若要刪除最後選定的形狀之外的其餘全部選定形狀,請單擊
"
相減
"
,如圖圖
11.3-9
。
圖
11.3-9
10
)在
"
工具
"
面板中,選擇其中橢圓形工具,在美工板上,經過拖動鼠標繪製一個圓,並設置其下列屬性。
Height=30
,
Width=30
,
StrokeThickness=0
11
)「屬性面板」的畫筆下單擊
Fill
,再單擊「漸變畫筆」標籤,接着單擊「線性漸變」畫筆,在下面的調色板中選擇顏色,設置漸變顏色爲黑到淡紅。效果如圖
11.3-10
:
圖
11.3-10
12
)
F6
切換到
"
動畫
"
工做區,準備建立腳本(
Storyboard
)實現動畫。
按
F6
可在可用的工做區之間切換。在
"
動畫
"
工做區中,
"
對象和時間線
"
面板位於美工板的下方。
13
)
"
對象和時間線
"
面板中,單擊
"
新建
"
,此時,將顯示
"
建立
Storyboard
資源
"
對話框,如圖圖
11.3-11
。
圖
11.3-11
14
)在
"
名稱
(
關鍵字
)"
字段中,鍵入資源的名稱,而後單擊
"
肯定
"
。
15
)
"
對象和時間線
"
面板中,選擇圓。
16
)擊
"
記錄關鍵幀
"
,以便記錄在
0
秒播放點標記上的對象的當前位置處外觀,如圖圖
11.3-12
。
圖
11.3-12
17
)
"
對象和時間線
"
面板中,將播放點拖到
1s
的時間點上,移動圓到新的位置,
18
)播放點拖到下一時間點上,不斷重複,讓圓繞一圈。
19
)擊
"
對象和時間線
"
頂部的
"
播放
"
按鈕,可查看剛纔建立的動畫效果
20
)擊「對象和時間線」中打開的名稱,此時屬性面板會顯示該腳本可供設置的屬性,選中
AutoReverse
,
RepeatBehavior
次數設爲
2x
。
AutoReverse
屬性設置爲
True
時,動畫播放結束時,會循着原來的軌跡逆向播放。
RepeatBehavior
屬性用來決定動畫播放的次數,如設置爲
Forever
則爲不斷地重複播放
21
)現已完成了動畫的設計操做,會保存爲
UserControl
的資源,可切換至
XAML
視圖來查看,以下爲自動產生的動畫資源
XAML
代碼。
<UserControl.Resources>
<Storyboard x:Name="Storyboard1" AutoReverse="True" RepeatBehavior="2x">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<EasingDoubleKeyFrame KeyTime="00:00:01" Value="-37"/>
<EasingDoubleKeyFrame KeyTime="00:00:02" Value="-70"/>
<EasingDoubleKeyFrame KeyTime="00:00:03" Value="-97"/>
<EasingDoubleKeyFrame KeyTime="00:00:04" Value="-43"/>
<EasingDoubleKeyFrame KeyTime="00:00:05" Value="-4"/>
<EasingDoubleKeyFrame KeyTime="00:00:06" Value="36"/>
<EasingDoubleKeyFrame KeyTime="00:00:07" Value="67"/>
<EasingDoubleKeyFrame KeyTime="00:00:08" Value="105"/>
<EasingDoubleKeyFrame KeyTime="00:00:09" Value="73"/>
<EasingDoubleKeyFrame KeyTime="00:00:10" Value="45"/>
<EasingDoubleKeyFrame KeyTime="00:00:11" Value="13"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<EasingDoubleKeyFrame KeyTime="00:00:01" Value="-3"/>
<EasingDoubleKeyFrame KeyTime="00:00:02" Value="-9"/>
<EasingDoubleKeyFrame KeyTime="00:00:03" Value="-17"/>
<EasingDoubleKeyFrame KeyTime="00:00:04" Value="-29"/>
<EasingDoubleKeyFrame KeyTime="00:00:05" Value="-31"/>
<EasingDoubleKeyFrame KeyTime="00:00:06" Value="-31"/>
<EasingDoubleKeyFrame KeyTime="00:00:07" Value="-27"/>
<EasingDoubleKeyFrame KeyTime="00:00:08" Value="-16"/>
<EasingDoubleKeyFrame KeyTime="00:00:09" Value="-6"/>
<EasingDoubleKeyFrame KeyTime="00:00:10" Value="-3"/>
<EasingDoubleKeyFrame KeyTime="00:00:11" Value="-1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
22
)啓動動畫:
Storyboard1.Begin ();
更詳細內容及源代碼下載:
http://www.amazon.cn/mn/detailApp/ref=sr_1_1?_encoding=UTF8&s=books&qid=1287058088&asin=B0043RT7I2&sr=8-1