[T1 Silverlight Training] Day 2: Mouse & Keyboard (Routed Event), Animation(Linear Animation, Key Frame, StoryBoard)
"一個(gè)月一更么?",呵呵,最近太忙了,沒機(jī)會(huì)寫博客,現(xiàn)在補(bǔ)上。
首先,我還是想Show一下我和Andy Wigley的合影,相信大家都知道Jump Start Video的Presenter吧,這次去西雅圖帶回來很多有用的WP7資源,在T2的時(shí)候再介紹。
【插播廣告】我們?cè)诒本┱衅竁indows Phone 7 Architect,有意向的人請(qǐng)聯(lián)系我 linuszhu@gmail.com

Mouse & Keyboard (Routed Event)
鼠標(biāo)和鍵盤事件和普通的Windows Form應(yīng)用程序或者Asp.net應(yīng)用程序的事件沒有差別,在WP中也保留了Silverlight的Mouse事件,不過同時(shí)也映射到了Tap等Touch相關(guān)的事件。
對(duì)于Silverlight應(yīng)用程序的鼠標(biāo)或者鍵盤事件都是Routed Event,和普通的Event差別在于,能夠進(jìn)行在UI的Visual Tree上進(jìn)行Bubble,同時(shí)EventArgs上帶有OriginalSource來標(biāo)識(shí)該事件最先發(fā)起的對(duì)象。

Animation(Linear Animation, Key Frame, StoryBoard)
Animation我覺得是在Silverlight里面非常強(qiáng)大的東西,關(guān)鍵的是在于它有很好的工具Blend支持。關(guān)于工具,這是我在給Symbian的人培訓(xùn)的時(shí)候大家得出的一個(gè)主要結(jié)論,微軟在語言、框架、理念上并領(lǐng)先,甚至有些Qt的更好,但在工具上絕對(duì)是勝出一大截,這個(gè)也可以理解,因?yàn)槲④浀哪康木褪亲尦绦騿T"傻瓜化"(不知道是好事還是壞事)。
在Silverlight里面總結(jié)一句話,什么是Animation?
Animation隨著時(shí)間的變化來改變Dependency Property的值。
Animation有以下三個(gè)主要的概念
- Storyboard, 動(dòng)畫控制單元,通過名稱來找到相應(yīng)的動(dòng)畫,包含Play,Stop等方法
- Timeline, 動(dòng)畫制作主線,主要有DoubleAnimation,PointAnimation,ColorAnimation等,通過StoryBoard的Target和TargetProperty Attach Property來設(shè)置該動(dòng)畫效果針對(duì)哪個(gè)具體的Logical Tree UIElement
- Key frame, 動(dòng)畫幀,即某時(shí)刻某個(gè)DP的屬性值應(yīng)該為多少,主要有LinearDoubleKeyFrame, DiscreteDoubleKeyFrame, SplineDoubleKeyFrame, EasingDoubleKeyFrame
以下給出了一些Animation的實(shí)例
<Storyboard x:Name="drive" Storyboard.TargetName="car">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Left)">
<LinearDoubleKeyFrame KeyTime="0:0:2" Value="100"/>
<LinearDoubleKeyFrame KeyTime="0:0:4" Value="100"/>
<LinearDoubleKeyFrame KeyTime="0:0:6" Value="200"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Top)">
<LinearDoubleKeyFrame KeyTime="0:0:2" Value="0"/>
<LinearDoubleKeyFrame KeyTime="0:0:4" Value="50"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
DoubleAnimation animation = new DoubleAnimation();
animation.From = 0;
animation.To = 100;
animation.Duration = new Duration(new TimeSpan(0,0,2));
Storyboard storyboard = new Storyboard();
Storyboard.SetTarget (animation, ball);
Storyboard.SetTargetProperty(animation, new PropertyPath( "(Canvas.Left)" ));
對(duì)于非線性的動(dòng)畫可以通過Spline的兩個(gè)Control Point來控制AP值變化速率,可以通過Blend來進(jìn)行設(shè)置。

同時(shí)Silverlight也自帶了一系列的預(yù)先配置好的Timeline效果,稱作EasingFunction,可以直接使用。

最后還有一點(diǎn)要提一下,由于WP7的StoryBoard是運(yùn)行在Composite Thread上面,而該線程可以直接通過GPU進(jìn)行繪畫,因此動(dòng)畫效果會(huì)很好,如果大家涉及到將某個(gè)UIElement的屬性(所有屬性均為DP)隨著時(shí)間變化時(shí),首先考慮用的應(yīng)該就是Animation。
下篇
Day 3 : Data Binding(Code, XAML, Value Conversion, Collection), Networking (Http, Socket)
posted on 2011-11-02 18:04 溫故 閱讀(1772) 評(píng)論(6) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)