WPF中的動漫

2021-09-23 18:44:04 字數 2898 閱讀 5021

<

!-- this example shows how to animate with a storyboard.

-->

"" xmlns:x=

""

windowtitle=

"storyboards example"

>

"20"

>

"myrectangle"

width=

"100"

height=

"100"

>

"mysolidcolorbrush" color=

"blue"

/>

<

/rectangle.fill>

"page.loaded"

>

"forever" autoreverse=

"true"

>

storyboard.targetname=

"myrectangle"

storyboard.targetproperty=

"width"

from=

"100" to=

"200" duration=

"0:0:1"

/>

<

/storyboard>

<

/beginstoryboard>

<

/eventtrigger>

<

/rectangle.triggers>

<

/rectangle>

<

/stackpanel>

<

/page>

在介紹storyboard之前應該先了解animation

animation提供一種簡單的「漸變」動畫,我們為乙個animation指定開始值和乙個結束值,並指定由開始值到達結束值所需的時間,便可形成乙個簡單的動畫。比如我們指定長方形的寬度由100變化到200,所需時間為1秒,很容易想像這樣的動畫是什麼樣的,而它對應的xaml**如下:

storyboard.targetname=

"myrectangle"

storyboard.targetproperty=

"width"

from=

"100" to=

"200" duration=

"0:0:1"

/>

**裡我們定義了乙個doubleanimation,並指定了它的開始值和結束值以及它由開始值到達結束值所需的時間。至於後面兩句,它們是用來將aniamtion與指定的物件和指定的屬性相關聯,等會我們將介紹。

注意到,這裡我們使用的是doubleanimation,因為我們所要變化的是數值。那麼如果我們要變化顏色是不是就用coloranimation了呢,對,其實出了這些之外還有pointanimation等等,並且你可以實現ianimatable介面來實現自定義版本的animation。關於這些你可以參見system.windows.mediaaniamtion名字空間.

但值得注意的是並非每個屬性都能夠使用animation,它必須滿足以下條件:

1,它必須是dependency property

2,它所在類必須繼承於dependencyobject,必須實現了ianimatable介面.

3,必須有型別一致的animation type(即color型別使用coloraniamtion,point型別使用pointanimation等)

乙個簡單的animation定義了乙個簡單的動畫,很容易想到的是,如果若干個animation同時作用於乙個物件,那麼這個物件不就可以表現複雜的動畫了嗎,對,這就是storyboard

storyboard可以看做是animation的容器,它包含了若干的簡單動畫來完成乙個複雜動畫。

參考以下**:

"" 

xmlns:x=

""

windowtitle=

"storyboards example"

>

"20"

>

"myrectangle"

width=

"100" height=

"100"

>

"mysolidcolorbrush" color=

"blue"

/>

<

/rectangle.fill>

"page.loaded"

>

"forever" autoreverse=

"true"

>

storyboard.targetname=

"myrectangle"

storyboard.targetproperty=

"width"

from=

"100" to=

"200" duration=

"0:0:1"

/>

storyboard.targetname=

"mysolidcolorbrush"

storyboard.targetproperty=

"color"

from=

"blue" to=

"red" duration=

"0:0:1"

/>

<

/storyboard>

<

/beginstoryboard>

<

/eventtrigger>

<

/rectangle.triggers>

<

/rectangle>

<

/stackpanel>

<

/page>

學習WPF 了解WPF中的XAML

xaml的簡單說明 xaml是用於例項化.net物件的標記語言,主要用於構建wpf的使用者介面 xaml中的每乙個元素都對映為.net類的乙個例項,例如對映為wpf的button物件 xaml可以在乙個元素中巢狀另乙個元素,例如grid元素可以巢狀button元素 了解xaml visualstud...

學習WPF 了解WPF中的XAML

xaml的簡單說明 xaml是用於例項化.net物件的標記語言,主要用於構建wpf的使用者介面 xaml中的每乙個元素都對映為.net類的乙個例項,例如對映為wpf的button物件 xaml可以在乙個元素中巢狀另乙個元素,例如grid元素可以巢狀button元素 了解xaml visualstud...

WPF中MatrixTransform的理解與應用

matrixtransform 主要通過點的矩陣變換來實現圖形的改變,我們常看到的一些效果,如對稱效果,就可以通過矩陣變換來實現。首先,我們先來了解一下 matrixtransform 的所有引數的意義,matrixtransform 的引數如下 其中 構成乙個矩陣 a,用於座標的變換 構成平移向量...