欢迎来到个人简历网!永久域名:gerenjianli.cn (个人简历全拼+cn)
当前位置:首页 > 范文大全 > 实用文>Silverlight:动画概述

Silverlight:动画概述

2022-11-19 08:29:15 收藏本文 下载本文

“爱喝白开水”通过精心收集,向本站投稿了8篇Silverlight:动画概述,下面是小编帮大家整理后的Silverlight:动画概述,希望对大家有所帮助。

Silverlight:动画概述

篇1:Silverlight:动画概述

在 Silverlight 中,动画可以通过添加移动和交互性来增强图形的创建效果 ,通过对背景色进行动画处理或应用动画 Transform,您可以创造出生动的屏幕 过渡效果或提供有帮助的视觉提示。

动画简介

动画是快速播放一系列图像(其中每个图像与下一个图像略微不同)给人造成 的一种幻觉。大脑感觉这组图像是一个变化的场景。在电影中,摄像机每秒钟拍 摄许多照片(帧),便可使人形成这种幻觉。用投影仪播放这些帧时,观众便可 以看电影了。在 Silverlight 中,通过对对象的个别属性应用动画,可以对对象 进行动画处理。例如,若要使 UIElement 增大,需对其 Width 和 Height 属性 进行动画处理。若要使 UIElement 逐渐从视野中消失,可以对其 Opacity 属性 进行动画处理。可以对 Silverlight 中许多对象的属性进行动画处理。

说明:

在 Silverlight 中,您只能对值类型为 Double、Color或 Point 的属性执行简单的动画处理。此外,还可以使用 ObjectAnimationUsingKeyFrames 对其他类型的属性进行动画处理,但是这需要 使用离散内插(从一个值跳到另一个值),而多数人认为这不是真正的动画。

下一节演示如何创建一个简单的动画,使 Rectangle(一种 UIElement)逐渐 进入视野并从视野中逐渐消失。

使 UIElement 逐渐进入视野并从视野中逐渐消失

此示例演示如何使用 Silverlight 动画通过对属性值进行动画处理,使 Rectangle 逐渐进入视野并从视野中逐渐消失。本示例使用 DoubleAnimation( 一种生成 Double 值的动画类型)对 Rectangle 的 Opacity 属性进行动画处理 。因此,Rectangle 将逐渐进入视野并逐渐从视野中消失。若要查看您将演练的 动画的预览,请单击下面的链接来运行示例,然后单击矩形开始运行动画。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=animation_ovw_intro

示例的第一部分创建一个 Rectangle 元素,并将其显示在 StackPanel 中。

XAML

x:Name=“MyAnimatedRectangle”

Width=“100” Height=“100” Fill=“Blue” />

若要创建动画并将其应用于矩形的 Opacity 属性,请执行以下操作:

创建 DoubleAnimation

创建 Storyboard

开始 Storyboard 以响应事件

以下各节将详细讨论这些步骤。

创建 DoubleAnimation

由于 Opacity 属性的类型是 Double,因此需要一个生成 Double 值的动画。 DoubleAnimation 就是这样一种动画;它可以创建两个 Double 值之间的过渡。 若要指定 DoubleAnimation 的起始值,可设置其 From 属性。若要指定其终止值 ,可设置其 To 属性。

不透明度值 1.0 使对象完全不透明,不透明度值 0.0 使对象完全不可见。若 要使动画的不透明度值从 1.0 过渡为 0.0,可以将其 From 属性设置为 1.0,将 其 To 属性设置为 0.0。

XAML

为该动画指定 Duration。动画的 Duration 属性指定了从其起始值过渡为目 标值所需的时间。下面的示例将动画的持续时间设置为一秒钟。

XAML

上面的示例创建了不透明度值从 1.0 向 0.0 过渡的动画,此过渡使目标元素 从完全不透明逐渐转变为完全不可见。若要使元素在消失后再逐渐回到视野中, 请将 AutoReverse 属性设置为 true。若要使动画无限期地重复,请将其 RepeatBehavior. 属性设置为 Forever。

XAML

AutoReverse=“True” RepeatBehavior=“Forever”/>

创建演示图板

若要向对象应用动画,请创建 Storyboard 对象并使用 TargetName 和 TargetProperty 附加属性指定要进行动画处理的对象和属性。

创建 Storyboard 并将动画添加为其子项。

XAML

AutoReverse=“True” RepeatBehavior=“Forever” />

本示例中的 Storyboard 仅包含一个动画,不过您可以添加多个动画。

使用 TargetName 附加属性指定要进行动画处理的对象。在下面的代码中,为 DoubleAnimation 指定了一个目标名称 myAnimatedRectangle,这是要进行动画 处理的对象的名称。

XAML

Storyboard.TargetName=“MyAnimatedRectangle”

From=“1.0” To=“0.0” Duration=“0:0:1”

AutoReverse=“True” RepeatBehavior=“Forever” />

使用 TargetProperty 附加属性指定要进行动画处理的属性。在下面的代码中 ,动画被配置为面向 Rectangle 的 Opacity 属性。

XAML

Storyboard.TargetName=“MyAnimatedRectangle”

Storyboard.TargetProperty=“Opacity”

From=“1.0” To=“0.0” Duration=“0:0:1”

AutoReverse=“True” RepeatBehavior=“Forever” />

将演示图板与事件相关联

此时您已经指定了动画的目标对象以及动画的行为方式;接下来您需要指定何 时开始播放动画。可以使用事件执行此操作。

1.将演示图板作为一种资源。将 Storyboard 放入一个资源块内,以便您能够 轻松地自代码引用该 Storyboard,以执行开始、停止、暂停和继续等操作。下面 的标记显示 StackPanel 对象资源块中声明的 Storyboard。请注意,您可以在任 意资源块中声明 Storyboard,只要该资源块与您希望进行动画处理的对象位于同 一个作用域中。

XAML

Storyboard.TargetName=“MyAnimatedRectangle”

Storyboard.TargetProperty=“Opacity”

From=“1.0” To=“0.0” Duration=“0:0:1”

AutoReverse=“True” RepeatBehavior=“Forever” />

x:Name=“MyAnimatedRectangle”

Width=“100” Height=“100” Fill=“Blue” />

2.将事件附加到某一元素。您可以使用多种事件来启动动画,包括鼠标相关事 件,如在用户单击某一对象时引发的 MouseLeftButtonDown,或是在首次加载对 象时引发的 Loaded 事件。有关事件的更多信息,请参见 Silverlight 的事件概 述。在本示例中,MouseLeftButtonDown 事件附加到 Rectangle,这样用户单击 矩形时将引发该事件。

XAML

x:Name=“MyAnimatedRectangle”

Width=“100” Height=“100” Fill=“Blue” />

3.从事件处理程序控制动画。Storyboard 提供多种方法,这些方法允许您控 制 Storyboard 动画的播放,包括 Begin、Stop、Pause 和 Resume。本示例使用 Begin 方法,该方法在用户单击矩形并引发 MouseLeftButtonDown 事件时启动动 画。

VB

' When the user clicks the Rectangle, the animation

' begins.

Private Sub Mouse_Clicked(ByVal sender As Object, ByVal e As MouseEventArgs)

myStoryboard.Begin

End Sub

完整的示例

下面的示例演示了完整的 XAML 标记,这些标记用于创建在加载时逐渐进入视 野并从视野中逐渐消失的矩形。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=animation_ovw_intro

XAML

xmlns=“schemas.microsoft.com/winfx//xaml/presentation”

xmlns:x=“schemas.microsoft.com/winfx/2006/xaml”

Width=“400” Height=“300”>

Storyboard.TargetName=“MyAnimatedRectangle”

Storyboard.TargetProperty=“Opacity”

From=“1.0” To=“0.0” Duration=“0:0:1”

AutoReverse=“True”

RepeatBehavior=“Forever” />

Click on the rectangle to start the animation.

x:Name=“MyAnimatedRectangle”

Width=“100” Height=“100” Fill=“Blue” />

VB

' When the user clicks the Rectangle, the animation

' begins.

Private Sub Mouse_Clicked(ByVal sender As Object, ByVal e As MouseEventArgs)

myStoryboard.Begin()

End Sub

开始、停止、暂停和继续动画播放

上面的示例演示如何使用 Begin 方法启动动画;不过,Storyboard 还提供 Stop、Pause 和 Resume 方法,这些方法可用于控制动画。下面的示例提供一系 列按钮,这些按钮允许用户控制 Ellipse 在整个屏幕中的动画播放。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=interactive_animation

XAML

xmlns=“schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=“schemas.microsoft.com/winfx/2006/xaml”

Width=“400” Height=“300”>

This sample uses the Begin, Pause, Resume, and Stop methods to control an animation.

Storyboard.TargetName=“MyAnimatedEllipseGeometry”

Duration=“0:0:5”

From=“20,200”

To=“400,100”

RepeatBehavior=“Forever” />

Center=“20,20” RadiusX=“15” RadiusY=“15” />

VB

Private Sub Animation_Begin(ByVal sender As Object, ByVal e As RoutedEventArgs)

myStoryboard.Begin()

End Sub

Private Sub Animation_Pause(ByVal sender As Object, ByVal e As RoutedEventArgs)

myStoryboard.Pause()

End Sub

Private Sub Animation_Resume(ByVal sender As Object, ByVal e As RoutedEventArgs)

myStoryboard.Resume()

End Sub

Private Sub Animation_Stop(ByVal sender As Object, ByVal e As RoutedEventArgs)

myStoryboard.Stop()

End Sub

控制动画的开始时间

Storyboard 以及所有其他动画对象(DoubleAnimation、DoubleAnimationUsingKeyFrames、ColorAnimation 等)从 Timeline 类继承( 请参见本主题后面的动画是时间线)。Timeline 类授予很多有用的属性给这些动 画对象,其中包括 BeginTime 属性。顾名思义,使用 BeginTime 属性可以指定 动画对象活动开始点的时间。例如,可以对 Storyboard 的 BeginTime 指定两秒 时间。当使用 Begin 方法开始 Storyboard 时,Storyboard 将等待两秒钟,然 后再开始。此外,可以指定 Storyboard 内的动画对象的 BeginTime。例如,如 果您有一个 BeginTime 为两秒钟的 Storyboard,此 Storyboard 包含两个 DoubleAnimation 对象(一个未指定 BeginTime,另一个的 BeginTime 为三秒钟 ),则第一个 DoubleAnimation 将在对 Storyboard 调用 Begin 方法之后的两 秒钟启动,第二个 DoubleAnimation 将在调用该方法之后的五秒钟启动 (Storyboard 的两秒钟延迟加上 DoubleAnimation 的三秒钟延迟)。下面的示 例显示如何执行此项操作。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=BeginTime

XAML

Storyboard.TargetName=“MyAnimatedRectangle”

Storyboard.TargetProperty=“Width”

To=“300” Duration=“0:0:1” />

Storyboard.TargetName=“MyAnimatedRectangle”

Storyboard.TargetProperty=“Opacity”

To=“0” Duration=“0:0:1” />

Loaded=“Start_Animation”

Width=“100” Height=“100” Fill=“Blue” />

VB

' Start the animation when the object loads.

Private Sub Start_Animation(ByVal sender As Object, ByVal e As EventArgs)

myStoryboard.Begin()

End Sub

可以考虑将 Storyboard 作为其他动画对象(例如 DoubleAnimation)以及其 他 Storyboard 对象的容器。即可以在 Storyboard 对象中嵌套该对象并分别为 每个 Storyboard 指定 BeginTime 值。使用嵌套的演示图板可帮助您安排精心设 计的动画序列。每个子 Storyboard 都会在其父 Storyboard 开始前等待,然后 在再次开始前开始倒计时。

在程序代码中创建动画

除了使用 XAML,也可以完全在程序代码(如 C# 或 Visual Basic)中创建动 画。下面的示例演示如何创建一个动画,在其中用动画播放 Rectangle 的 Canvas.Top 和 Canvas.Left 附加属性。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=programmatic_animation

VB

Private Sub Create_And_Run_Animation(ByVal sender As Object, ByVal e As EventArgs)

' Create a red rectangle that will be the target

' of the animation.

Dim myRectangle As Rectangle = New Rectangle

myRectangle.Width = 200

myRectangle.Height = 200

Dim myColor As Color = Color.FromArgb(255, 255, 0, 0)

Dim myBrush As SolidColorBrush = New SolidColorBrush

myBrush.Color = myColor

myRectangle.Fill = myBrush

' Add the rectangle to the tree.

LayoutRoot.Children.Add(myRectangle)

' Create a duration of 2 seconds.

Dim duration As Duration = New Duration (TimeSpan.FromSeconds(2))

' Create two DoubleAnimations and set their properties.

Dim myDoubleAnimation1 As DoubleAnimation = New DoubleAnimation

Dim myDoubleAnimation2 As DoubleAnimation = New DoubleAnimation

myDoubleAnimation1.Duration = duration

myDoubleAnimation2.Duration = duration

Dim sb As Storyboard = New Storyboard

sb.Duration = duration

sb.Children.Add(myDoubleAnimation1)

sb.Children.Add(myDoubleAnimation2)

Storyboard.SetTarget(myDoubleAnimation1, myRectangle)

Storyboard.SetTarget(myDoubleAnimation2, myRectangle)

' Set the attached properties of Canvas.Left and Canvas.Top

' to be the target properties of the two respective DoubleAnimations

Storyboard.SetTargetProperty(myDoubleAnimation1, New PropertyPath(“(Canvas.Left)”))

Storyboard.SetTargetProperty(myDoubleAnimation2, New PropertyPath(“(Canvas.Top)”))

myDoubleAnimation1.To = 200

myDoubleAnimation2.To = 200

' Make the Storyboard a resource.

LayoutRoot.Resources.Add(“unique_id”, sb)

' Begin the animation.

sb.Begin()

End Sub

说明:

不要试图在页面的构造函数中调用 Storyboard 方法,如 Begin,

这将导致动画失败,且无任何提示。

动画类型

上面的示例使用了 DoubleAnimation 对属性进行动画处理。除了 DoubleAnimation 类型,Silverlight 还提供其他几种动画对象。由于动画生成 属性值,因此对于不同的属性类型,会有不同的动画类型。若要对采用 Double 值的属性(例如元素的 Width 属性)进行动画处理,请使用生成 Double 值 (DoubleAnimation)的动画。若要对采用 Point 值的属性进行动画处理,请使 用生成 Point 值(如 PointAnimation 等)的动画。

Silverlight 提供两大类动画类型,From/To/By 动画和关键帧动画。下表说 明这些动画类别及其命名约定。

类别说明命名约定 From/To/By 动画在起始值和结束值之间进行动画处理:

若要指定起始值 ,请设置动画的 From 属性。

若要指定结束值,请设置动画的 To 属性。

若要指定相对于起始值的结束值,请设置动画的 By 属性( 而不是 To 属性)。

此概述中的示例使用这些动画,因为这些动画 最容易实现。typeAnimation 关键帧动画在使用关键帧对象指定的一系列值之间播放动画。关键帧动画的功能 比 From/To/By 动画的功能更强大,因为您可以指定任意多个目标值,甚至可以 控制它们的插值方法。关键帧动画中详细描述了关键帧动画。typeAnimationUsingKeyFrames

下表显示了一些常用动画类型以及与这些类型一起使用的一些属性。

属性类型对应的基本 (From/To/By) 动画对应的关键帧动画用法示例 ColorColorAnimationColorAnimationUsingKeyFrames对 SolidColorBrush 或 GradientStop 的 Color 进行动画处理。 DoubleDoubleAnimationDoubleAnimationUsingKeyFrames对 Rectangle 的 Width 或 Ellipse 的 Height(或任意 FrameworkElement)进行动画处理。 PointPointAnimationPointAnimationUsingKeyFrames对 EllipseGeometry 的 Center 位置进行动画处理。 Object无ObjectAnimationUsingKeyFrames对 Fill 属性进行动画处理,使其在不同的 GradientBrush 之间进行 转换。

动画是时间线

所有动画均继承自 Timeline 对象,因此所有动画都是专用类型的时间线。 Timeline 定义时间段。您可以指定时间线的以下“计时行为”:其 Duration 和重 复次数,甚至可以为时间线指定时间走得多快。

因为动画是 Timeline,所以它还表示一个时间段。在动画的指定时间段(即 Duration)内运行动画时,动画会计算输出值。在运行或“播放”动画时,动画将 更新与其关联的属性。

Duration、AutoReverse 和 RepeatBehavior. 是三个常用的计时属性。

Duration 属性

时间线(以及继承自时间线的动画)表示一个时间段。该时间段的长度由时间 线的 Duration 属性(通常用 TimeSpan 值指定)来决定。当时间线达到其持续 时间的终点时,表示时间线完成了一次重复。

动画使用其 Duration 属性来确定其当前值。如果没有为动画指定 Duration 值,它将使用默认值(1 秒)。

下面的语法显示了 Duration 属性 (Property) 的 XAML 属性 (Attribute) 语法的简化版本。

小时 : 分钟 : 秒

下表显示了一些 Duration 设置及其结果值。

设置所得值 0:0:5.55.5 秒。 0:30:5.530 分 5.5 秒。 1:30:5.51 小时 30 分 5.5 秒。AutoReverse 属性

AutoReverse 属性指定时间线在到达其 Duration 的终点后是否倒退。如果将 此动画属性设置为 true,则动画在到达其 Duration 的终点后将倒退,即从其终 止值向其起始值反向播放。默认情况下,该属性为 false。

RepeatBehavior. 属性

RepeatBehavior. 属性指定时间线的播放次数。默认情况下,时间线的重复次 数为 1.0,即播放一次时间线,不进行重复。

对属性应用动画

前面几节描述动画的不同类型及其计时属性。本节演示如何对要进行动画处理 的属性应用动画。Storyboard 对象提供了对属性应用动画的一种方法。 Storyboard 是一个为其所包含的动画提供目标信息的容器时间线。

以对象和属性为目标

Storyboard 类提供 TargetName 和 TargetProperty 附加属性。通过在动画 上设置这些属性,您将告诉动画对哪些内容进行动画处理。不过,在动画以对象 作为处理目标之前,必须使用 x:Name 属性为该对象提供一个名称(如上例所示 ),否则必须间接以属性作为目标。下面的示例演示如何间接以属性作为目标。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=coloranimation

XAML

Loaded=“Start_Animation”>

Storyboard.TargetProperty=“(Panel.Background). (SolidColorBrush.Color)”

From=“Red” To=“Green” Duration=“0:0:4” />

VB

' Start the animation when the object loads

Private Sub Start_Animation(ByVal sender As Object, ByVal e As EventArgs)

colorStoryboard.Begin()

End Sub

请注意,正在进行动画处理的属性值 (Color) 属于未命名甚至未显式声明的 SolidColorBrush 对象。此间接目标是使用下面的特殊语法完成的。

XAML

Storyboard.TargetProperty=“(Panel.Background). (SolidColorBrush.Color)”

或者,可以显式创建 SolidColorBrush,对其进行命名,然后直接以其 Color 属性为目标。下面的示例演示如何创建与前面的示例相同的动画,但是使用直接 属性目标。

XAML

Storyboard.TargetProperty=“Color” From=“Red” To=“Green” Duration=“0:0:4” />

VB

' Start the animation when the object loads

Private Sub Start_Animation(ByVal sender As Object, ByVal e As EventArgs)

colorStoryboard.Begin()

End Sub

对变换进行动画处理

一些更有趣的动画(如旋转、扭曲和重新缩放对象)是通过对 Transform. 对 象的属性进行动画处理实现的。

下面的示例将 Storyboard 和 DoubleAnimation 与 RotateTransform. 一起使 用,以便使 Rectangle 旋转到位。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=transforms_ovw_animating_transforms

XAML

Storyboard.TargetName=“myTransform”

Storyboard.TargetProperty=“Angle”

From=“0” To=“360” Duration=“0:0:5”

RepeatBehavior=“Forever” />

MouseLeftButtonDown=“StartAnimation”>

VB

Private Sub StartAnimation(ByVal sender As Object, ByVal e As MouseEventArgs)

myStoryboard.Begin()

End Sub

有关变换的更多信息,请参见变换。

动画结束后会发生什么情况

FillBehavior. 属性指定时间线结束时的行为方式。此属性的默认值为 HoldEnd,表示在动画结束后,进行动画处理的对象将保持其最终值。例如,如果 对 Rectangle 的 Opacity 属性进行动画处理,使其在 2 秒内从 1 转换为 0, 该矩形的默认行为是:在 2 秒后保持不透明度为 0 的状态。如果将 FillBehavior. 设置为 Stop,该矩形的不透明度将在动画结束后还原为初始值 1 。

缓动函数

通过缓动函数,您可以将自定义算术公式应用于动画。例如,您可能希望某一 对象逼真地弹回或其行为像弹簧一样。您可以使用关键帧动画甚至 From/To/By 动画来大致模拟这些效果,但可能需要执行大量的工作,并且与使用算术公式相 比动画的精确性将降低。

除了通过从 EasingFunctionBase 继承来创建您自己的自定义缓动函数外,您 还可以使用运行时提供的若干缓动函数之一来创建常见效果。

BackEase:在某一动画开始沿指示的路径进行动画处理前稍稍收回该动画的移 动。

BounceEase:创建弹回效果。

CircleEase:创建使用循环函数加速和/或减速的动画。

CubicEase:创建使用公式 f(t) = t3 加速和/或减速的动画。

ElasticEase:创建表示弹簧在停止前来回振荡的动画。

ExponentialEase:创建使用指数公式加速和/或减速的动画。

PowerEase:创建使用公式 f(t) = tp(其中,p 等于 Power 属性)加速和/ 或减速的动画。

QuadraticEase:创建使用公式 f(t) = t2 加速和/或减速的动画。

QuarticEase:创建使用公式 f(t) = t4 加速和/或减速的动画。

QuinticEase:创建使用公式 f(t) = t5 加速和/或减速的动画。

SineEase:创建使用正弦公式加速和/或减速的动画。

可使用下面的示例查看这些缓动函数的行为。

运行此示例: samples.msdn.microsoft.com/Silverlight/silverlight_next/Animatio ns/easing_functions_gallery/testpage.html

若要将缓动函数应用于某一动画,请使用该动画的 EasingFunction 属性指定 要应用于该动画的缓动函数。下面的示例将 BounceEase 缓动函数应用于 DoubleAnimation 以创建弹回效果。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=BounceEase

XAML

Storyboard.TargetName=“myRectangle”

Storyboard.TargetProperty=“Height”>

Bounciness=“2” />

Fill=“Blue” Width=“200” Height=“30” />

VB

' When the user clicks the rectangle, the animation

' begins.

Private Sub Mouse_Clicked(ByVal sender As Object, ByVal e As MouseEventArgs)

myStoryboard.Begin()

End Sub

在上一个示例中,该缓动函数已应用于某一 From/To/By 动画。还可以将缓动 函数应用于关键帧动画(请参见关键帧动画)。下面的示例演示如何将关键帧用 于和它们相关联的缓动函数,以便创建一个矩形动画,该动画向上缩回、速度放 慢、然后向下展开(就像下落),再弹回到某一停止点。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=EasingFunctionDoubleKeyFrame

XAML

Storyboard.TargetProperty=“Height”

Storyboard.TargetName=“myRectangle”>

Fill=“Blue” Width=“200” Height=“200” />

VB

' When the user clicks the rectangle, the animation

' begins.

Private Sub Mouse_Clicked(ByVal sender As Object, ByVal e As MouseEventArgs)

myStoryboard.Begin()

End Sub

您可以使用 EasingMode 属性更改缓动函数的行为方式,也就是说,更改动画 的内插方式。有三个可能的值可赋予 EasingMode:

EaseIn:内插遵循与缓动函数相关联的算术公式。

EaseOut:内插遵循 100% 内插减去与缓动函数相关联的公式输出。

EaseInOut:内插将 EaseIn 用于动画的前半部分,将 EaseOut 用于动画的后 半部分。

下图演示了 EasingMode 的不同值,其中 f(x) 表示动画进度,t 表示时间。

BackEase

BounceEase

CircleEase

CubicEase

ElasticEase

ExponentialEase

PowerEase

QuadraticEase

QuarticEase

QuinticEase

SineEase

说明:

您可以使用 PowerEase 创建与使用 Power 属性的 CubicEase、QuadraticEase、QuarticEase 和 QuinticEase 的相同的行为。例如,如果您要 使用 PowerEase 来替换 CubicEase,则将 Power 的值指定为 3。

除了使用在运行时中包括的缓动函数外,您还可以通过从 EasingFunctionBase 继承来创建自己的自定义缓动函数。下面的示例演示如何创 建简单的自定义缓动函数。您可以通过覆盖 EaseInCore 方法,针对缓动函数的 行为方式添加您自己的数学逻辑。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=CustomEasingFunction

C#

namespace CustomEasingFunction

{

public class CustomSeventhPowerEasingFunction : EasingFunctionBase

{

public CustomSeventhPowerEasingFunction() : base()

{

}

// Specify your own logic for the easing function by overriding

// the EaseInCore method. Note that this logic applies to the “EaseIn”

// mode of interpolation.

protected override double EaseInCore(double normalizedTime)

{

// applies the formula of time to the seventh power.

return Math.Pow(normalizedTime, 7);

}

}

}

篇2:Silverlight代码创建动画

代码中使用了 C# 3.0 语法

效果是一个红色矩形从右下角移动到左上角

仅仅是示例,演示如何在代码中动态创建动画

MainPage.xaml

xmlns=“schemas. microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=“schemas.microsoft. com/winfx/2006/xaml”>

MainPage.xaml.cs

1 using System;

2 using System.Windows;

3 using System.Windows.Controls;

4 using System.Windows.Media;

5 using System.Windows.Media.Animation;

6 using System.Windows.Shapes;

7

8 namespace Hongcing.Silverlight

9 {

10 public partial class Create_And_Run_Animation : UserControl

11 {

12 public Create_And_Run_Animation()

13 {

14 InitializeComponent();

15 }

16

17 private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)

18 {

19 var redRectangle = new Rectangle

20 {

21 Width = 300,

22 Height = 200,

23 Fill = new SolidColorBrush(Colors.Red),

24 Stroke = new SolidColorBrush(Colors.Black)

25 };

26

27 (sender as Panel).Children.Add(redRectangle);

28

29 var leftAnimation = new DoubleAnimation

30 {

31 Duration = new Duration(TimeSpan.FromSeconds(5)),

32 From = 700,

33 To = 0

34 };

35

36 var topAnimation = new DoubleAnimation

37 {

38 Duration = leftAnimation.Duration,

39 From = 350,

40 To = 0

41 };

42

43 Storyboard.SetTarget(leftAnimation, redRectangle);

44 Storyboard.SetTarget(topAnimation, redRectangle);

45

46 // 属性路径也可以用 new PropertyPath(“(Canvas.Left)”)、new PropertyPath (“(Canvas.Top)”)

47 Storyboard.SetTargetProperty(leftAnimation, new PropertyPath(Canvas.LeftProperty));

48 Storyboard.SetTargetProperty(topAnimation, new PropertyPath (Canvas.TopProperty));

49

50 //此处没有添加到资源中,而是直接 启动动画,

Silverlight代码创建动画

51 new Storyboard { Children = { leftAnimation, topAnimation } }.Begin();

52 }

53 }

54 }

55

篇3:Silverlight:以编程方式使用动画

有时您可能要动态(即时)更改动画的属性,例如,您可能要调整应用到对象 的动画行为,这取决于对象当前在布局中的位置、对象包含何种内容等等。可以 通过使用程序代码(例如 C# 或 Visual Basic)动态操作动画。

先决条件

您应熟悉 Silverlight 动画。有关简介,请参见动画概述。

通过名称访问动画

访问动画对象以更改其属性的最直接方法是:命名该动画对象,然后在代码中 通过该名称引用它。下面的示例包含一个 Ellipse,当您在屏幕上单击时它将显 示动画效果。为了实现此动画,在单击 Canvas 时,事件处理程序更改 PointAnimation 对象的 To 属性,然后启动动画。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=change_animation_properties_1

XAML

Background=“Gray” Width=“600” Height=“500”>

x:Name=“myPointAnimation”

Storyboard.TargetProperty=“Center”

Storyboard.TargetName=“MyAnimatedEllipseGeometry”

Duration=“0:0:2”/>

Center=“200,100” RadiusX=“15” RadiusY=“15” />

VB

Private Sub Handle_MouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)

' Retrieve current mouse coordinates.

Dim newX As Double = e.GetPosition(Nothing).X

Dim newY As Double = e.GetPosition(Nothing).Y

Dim myPoint As Point = New Point

myPoint.X = newX

myPoint.Y = newY

myPointAnimation.To = myPoint

myStoryboard.Begin

End Sub

C#

private void Handle_MouseDown(object sender, MouseButtonEventArgs e)

{

// Retrieve current mouse coordinates.

double newX = e.GetPosition(null).X;

double newY = e.GetPosition(null).Y;

Point myPoint = new Point();

myPoint.X = newX;

myPoint.Y = newY;

myPointAnimation.To = myPoint;

myStoryboard.Begin();

}

使所有动画具有唯一的名称有时不易做到。这时您可以使用集合来访问动画或 动画的关键帧。例如,如果要以编程方式访问 DoubleAnimationUsingKeyFrames 对象中的所有关键帧,可以使用与以下代码类似的代码:

XAML

Width=“600” Height=“500” Background=“Gray”>

x:Name=“myPointAnimationUsingKeyFrames”

Storyboard.TargetProperty=“Center”

Storyboard.TargetName=“MyAnimatedEllipseGeometry”

Duration=“0:0:3”>

Center=“200,100” RadiusX=“15” RadiusY=“15” />

C#

public void Handle_MouseDown(object sender, MouseEventArgs e)

{

int i;

for (i = 0; i < myPointAnimationUsingKeyFrames.KeyFrames.Count; i++)

{

// Do something with each keyframe; for example, set values.

}

}

下面的示例与上一个示例类似,即在用户单击屏幕的地方出现椭圆,只是此示 例中使用了关键帧。对关键帧的集合进行迭代并为关键帧动态设置值,以便使椭 圆动画出现在合适的位置。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=access_keyframe_collection

XAML

Width=“600” Height=“500” Background=“Gray”>

x:Name=“myPointAnimationUsingKeyFrames”

Storyboard.TargetProperty=“Center”

Storyboard.TargetName=“MyAnimatedEllipseGeometry”

Duration=“0:0:3”>

Center=“200,100” RadiusX=“15” RadiusY=“15” />

C#

' Global variables that keep track of the end point

' of the last animation.

Private lastX As Double = 200

Private lastY As Double = 100

Private Sub Handle_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

' Retrieve current mouse coordinates.

Dim newX As Double = e.GetPosition(Nothing).X

Dim newY As Double = e.GetPosition(Nothing).Y

Dim i As Integer

i = 0

Do While (i < myPointAnimationUsingKeyFrames.KeyFrames.Count)

Dim keyFrame. As PointKeyFrame. = myPointAnimationUsingKeyFrames.KeyFrames(i)

If (keyFrame.GetType.Name = “DiscretePointKeyFrame”) Then

keyFrame.SetValue (DiscretePointKeyFrame.ValueProperty, New Point(lastX, lastY))

ElseIf (keyFrame.GetType.Name = “LinearPointKeyFrame”) Then

' The LinearKeyFrame. has a value that is part way to the

' final end point. In addition, this value has to be on

' the correct line, therefore, you need to use the line

' formula y = mx + b to find the values of x and y.

' Calculate the slope

Dim m As Double = ((newY - lastY) / (newX - lastX))

' Calculate the y-intercept.

Dim b As Double = (newY - (m * newX))

' Set X to a third of the way to the end point.

Dim intermediateX As Double = (lastX + ((newX - lastX) / 3))

' Find the value Y from X and the line formula.

Dim intermediateY As Double = ((m * intermediateX) + b)

' Set the keyframe. value to the intermediate x and y value.

keyFrame.SetValue (LinearPointKeyFrame.ValueProperty, New Point(intermediateX, intermediateY))

ElseIf (keyFrame.GetType.Name = “SplinePointKeyFrame”) Then

keyFrame.SetValue (SplinePointKeyFrame.ValueProperty, New Point(newX, newY))

End If

i = (i + 1)

Loop

myStoryboard.Stop()

myStoryboard.Begin()

lastX = newX

lastY = newY

End Sub

注意说明:

Storyboard 具有 Children 属性,该属性允许您访问指定 Storyboard 中的 所有动画对象。

动态更改 TargetName

动态更改 Storyboard.TargetName 属性最常见的情况是您想将同一动画应用 到多个对象。当具有要应用相似动画的大量对象时,这特别有用。例如,您可能 要显示几行图像并使用动画突出显示鼠标当前所指示的图像。为每个图像创建单 独的 Storyboard 对象非常麻烦。重用同一 Storyboard 更为合适。

下面的示例涉及很多矩形,当您单击这些矩形时,它们会逐渐消失,接着重新 显示。所有这些矩形使用同一 Storyboard,因为呈现 Opacity 动画效果的 DoubleAnimation 将 TargetName 更改为所单击的矩形。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=change_targetname_1

XAML

Storyboard.TargetProperty=“Opacity”

From=“1.0” To=“0.0” Duration=“0:0:2”

AutoReverse=“True” />

x:Name=“MyAnimatedRectangle1”

Margin=“3” Width=“100” Height=“100” Fill=“Blue”

MouseLeftButtonDown=“Start_Animation” />

x:Name=“MyAnimatedRectangle2”

Margin=“3” Width=“100” Height=“100” Fill=“Blue”

MouseLeftButtonDown=“Start_Animation” />

x:Name=“MyAnimatedRectangle3”

Margin=“3” Width=“100” Height=“100” Fill=“Blue”

MouseLeftButtonDown=“Start_Animation” />

x:Name=“MyAnimatedRectangle4”

Margin=“3” Width=“100” Height=“100” Fill=“Blue”

MouseLeftButtonDown=“Start_Animation” />

VB

Private Sub Start_Animation(ByVal sender As Object, ByVal e As MouseEventArgs)

' If the Storyboard is running and you try to change

' properties of its animation objects programmatically,

' an error will occur.

myStoryboard.Stop()

' Get a reference to the rectangle that was clicked.

Dim myRect As Rectangle = CType(sender, Rectangle)

' Change the TargetName of the animation to the name of the

' rectangle that was clicked.

myDoubleAnimation.SetValue(Storyboard.TargetNameProperty, myRect.Name)

' Begin the animation.

myStoryboard.Begin()

End Sub

在前面的代码中,请注意您在动态更改动画对象的属性前必须停止 Storyboard,否则将出错,在此示例中,可能不希望只有停止一个矩形的动画才 能启动另一个矩形的动画。可能您想同时运行这两个动画。但是,您不能使用同 一个动画对象同时运行两个独立的动画,因为只有一个 TargetName。这并不意味 着您不得不重新为每个对象创建单独的 Storyboard。您只需要为并发(同步)运 行的每个动画提供一个 Storyboard。下面的示例与上一个示例类似,只是它包含 三个而不是一个 Storyboard 对象。您单击矩形时,事件处理程序查找当前未使 用的 Storyboard 并使用它来创建动画。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=change_targetname_2

XAML

Storyboard.TargetProperty=“Opacity”

From=“1.0” To=“0.0” Duration=“0:0:2” AutoReverse=“True” />

Storyboard.TargetProperty=“Opacity”

From=“1.0” To=“0.0” Duration=“0:0:2”

AutoReverse=“True” />

Storyboard.TargetProperty=“Opacity”

From=“1.0” To=“0.0” Duration=“0:0:2”

AutoReverse=“True” />

Margin=“3” Width=“100” Height=“100” Fill=“Blue”

MouseLeftButtonDown=“Start_Animation” />

Margin=“3” Width=“100” Height=“100” Fill=“Blue”

MouseLeftButtonDown=“Start_Animation” />

Margin=“3” Width=“100” Height=“100” Fill=“Blue”

MouseLeftButtonDown=“Start_Animation” />

Margin=“3” Width=“100” Height=“100” Fill=“Blue”

MouseLeftButtonDown=“Start_Animation” />

VB

Private storyboard1Active As Boolean = False

Private storyboard2Active As Boolean = False

Private storyboard3Active As Boolean = False

Private Sub Start_Animation(ByVal sender As Object, ByVal e As MouseEventArgs)

' Get a reference to the rectangle that was clicked.

Dim myRect As Rectangle = CType(sender, Rectangle)

If Not storyboard1Active Then

myStoryboard1.Stop()

myDoubleAnimation1.SetValue (Storyboard.TargetNameProperty, myRect.Name)

myStoryboard1.Begin()

storyboard1Active = True

ElseIf Not storyboard2Active Then

myStoryboard2.Stop()

myDoubleAnimation2.SetValue (Storyboard.TargetNameProperty, myRect.Name)

myStoryboard2.Begin()

storyboard2Active = True

ElseIf Not storyboard3Active Then

myStoryboard3.Stop()

myDoubleAnimation3.SetValue (Storyboard.TargetNameProperty, myRect.Name)

myStoryboard3.Begin()

storyboard3Active = True

End If

End Sub

Private Sub Storyboard_Completed(ByVal sender As Object, ByVal e As EventArgs)

Dim myStoryboard As Storyboard = CType(sender, Storyboard)

Select Case (myStoryboard.GetValue (NameProperty).ToString)

Case “myStoryboard1”

storyboard1Active = False

Case “myStoryboard2”

storyboard2Active = False

Case “myStoryboard3”

storyboard3Active = False

End Select

End Sub

在上面的示例中,同时只能运行三个动画(等于 Storyboard 对象的数目)。 如果您不需要同时运行更多动画,这个示例就可以满足要求了,否则将需要更多 的 Storyboard 对象。如果要同时运行很多独立的动画,可能要动态创建 Storyboard 对象。有关在代码中创建演示图板对象的示例,请参见下一节。

在程序代码中创建动画

您还可以完全在程序代码中创建动画。下面的示例演示如何创建一个动画,在 其中用动画呈现矩形的 Canvas.Top 和 Canvas.Left 附加属性。

运行此示例:go.microsoft.com/fwlink/? LinkId=139798&sref=programmatic_animation

VB

Private Sub Create_And_Run_Animation(ByVal sender As Object, ByVal e As EventArgs)

' Create a red rectangle that will be the target

' of the animation.

Dim myRectangle As Rectangle = New Rectangle

myRectangle.Width = 200

myRectangle.Height = 200

Dim myColor As Color = Color.FromArgb(255, 255, 0, 0)

Dim myBrush As SolidColorBrush = New SolidColorBrush

myBrush.Color = myColor

myRectangle.Fill = myBrush

' Add the rectangle to the tree.

LayoutRoot.Children.Add(myRectangle)

' Create a duration of 2 seconds.

Dim duration As Duration = New Duration (TimeSpan.FromSeconds(2))

' Create two DoubleAnimations and set their properties.

Dim myDoubleAnimation1 As DoubleAnimation = New DoubleAnimation

Dim myDoubleAnimation2 As DoubleAnimation = New DoubleAnimation

myDoubleAnimation1.Duration = duration

myDoubleAnimation2.Duration = duration

Dim sb As Storyboard = New Storyboard

sb.Duration = duration

sb.Children.Add(myDoubleAnimation1)

sb.Children.Add(myDoubleAnimation2)

Storyboard.SetTarget(myDoubleAnimation1, myRectangle)

Storyboard.SetTarget(myDoubleAnimation2, myRectangle)

' Set the attached properties of Canvas.Left and Canvas.Top

' to be the target properties of the two respective DoubleAnimations

Storyboard.SetTargetProperty(myDoubleAnimation1, New PropertyPath(“(Canvas.Left)”))

Storyboard.SetTargetProperty(myDoubleAnimation2, New PropertyPath(“(Canvas.Top)”))

myDoubleAnimation1.To = 200

myDoubleAnimation2.To = 200

' Make the Storyboard a resource.

LayoutRoot.Resources.Add(“unique_id”, sb)

' Begin the animation.

sb.Begin()

End Sub

注意说明:

不要试图在页面的构造函数中调用 Storyboard 成员(例如 Begin 方法)。 这将导致动画失败,且无任何提示。

篇4:Silverlight制作逐帧动画网页设计

打算用sl来制作一个游戏,我曾经有flash开发游戏的经验.现在想用sl来做.打算记录下我开发游戏探索的过程.

打开www.emu-zone.org/www3/host/emugif/ 这个网站.这里有很多游戏的gif动画.选一个存到本地用Fireworks打开.将其中的不同帧的图片取出做成一个png的图形.如下图所示:

用vs08建立sl项目.

新建立文件夹Controls.在这里我们放自定义用户控件.新建用户控件p.xaml

将刚才制作的png图片放入项目.

编辑用户控件

Image图片的Stretch的属性设置为None.图片处理请参考 TerryLee大侠 的 一步一步学Silverlight

2系列(28):图片处理.

将Image放入Canvas中

在新建立的Canvas的外层制作一个矩形.刚好挡住其中的一个人物.

选中矩形和Canvas设置遮照

建立storyboard

设置StoryBoard放大300倍这样方便处理.

对Image进行操作.采用DiscreteDoubleKeyFrame设置其X坐标.

至此动画制作完成

更多请参见我的blog::nasa.cnblogs.com/

经典论坛交流:bbs.blueidea.com/thread-2840261-1-1.html

篇5:Silverlight & Blend动画设计系列二:旋转动画(RotateTransfor

Silverlight的基础动画包括偏移、旋转、缩放、倾斜和翻转动画,这些基础动画毫无疑 问是在Silverlight中使用得最多的动画效果,其使用也是非常简单的,相信看过上一篇《偏 移动画(TranslateTransform)》文章的朋友大多数对Silverlight & Blend动画设计已 经产生了莫大的兴趣,本篇将继续介绍Silverlight中的基础动画之旋转动画 (RotateTransform)。

所谓旋转动画(RotateTransform)也就是一个元素以一个坐标点为旋转中心点旋转,在 使用旋转动画(RotateTransform)的时候需要注意的有两点:旋转中心点(Center)和旋转 角度(Angle)。同样当我们设计好动画元素后要为其创建动画效果,首先得添加动画容器时间 线(Storyboard),直接在Blend设计界面既可完成该操作。如下图所示:

当动画容器时间线创建好后,只需要选中需要进行创建动画的元素,然后再属性面板下进 行可视化属性设置就可以完成动画的创建,转到“转换”属性面板,然后选择“旋转”,可 以看到如下图所示的属性设置面板。

如上图示,将动画旋转角度(Angle)设置为了360,这表示动画作用元素将以旋转中心坐标 进行旋转360度。此时切换到XAML编码视图可以发现Blend自动生成了如下动画代码块:

Storyboard.TargetProperty=“(UIElement.RenderTransform). (TransformGroup.Children)[2].(RotateTransform.Angle)”>

执行这个旋转动画可以看到效果,名为“fan”的元素将在一秒钟内旋转360度。同样也可 以通过编程的方式来动态的创建该旋转动画,详细如下代码块:

private void CreateStoryboard

{

//创建动画容器时间线

Storyboard storyboard = new Storyboard();

//创建旋转动画

DoubleAnimation doubleAnimation = new DoubleAnimation();

doubleAnimation.To = 360;

doubleAnimation.Duration = new Duration(new TimeSpan(0, 0, 1));

Storyboard.SetTarget(doubleAnimation, fan);

Storyboard.SetTargetProperty(doubleAnimation,

new PropertyPath(“(UIElement.RenderTransform). (TransformGroup.Children)[2].(RotateTransform.Angle)”));

storyboard.Children.Add(doubleAnimation);

storyboard.Begin();

}

或许有人会问,旋转动画我该怎么去用,什么样的场景适合使用旋转动画?其实很多地方 都可以使用旋转动画,比如游戏中的地图场景中的风车,实际上也就是一个旋转动画效果, 旋转动画下面是一条线作为风车的柱子,

我们直接在本文的示例项目中加入一条竖线,线的 一端对应于旋转动画的中心,通过Blend 设计后动态生成的XAML编码如下:

Canvas.Left=“303” Canvas.Top=“184” Data=“M408,256 L408,449.49417” StrokeThickness=“6” pacity=“0.78”

StrokeStartLineCap=“Round” StrokeEndLineCap=“Round”>

注意上面设置ZIndex值是为了将线条呈现到旋转动画的后面去,这样给人一种旋转动画是 在线条的一端不停的选择,看起就像是一个风车在旋转一样,实际上就是一种视觉欺骗,记 得我一个做3D游戏开发的朋友给我说过,3D游戏里的大多数效果全都是视觉欺骗,嘎嘎 ~~~~~~~~~~,OK,现在运行动画的效果则如下截图:

这样的效果貌似不好看,我们可以为其他加入背景图片进行装饰,根据背景图片进行调整 适当的位置、元素颜色、形状等,以更为真实的效果呈现在用户面前。具体的调整过程这里 就不作过多的介绍,给个上了背景的截图演示下吧:

文章出处:beniao.cnblogs.com/

篇6:Silverlight & Blend动画设计系列四:倾斜动画(SkewTransform)

Silverlight中的倾斜变化动画(SkewTransform)能够实现对象元素的水平、垂直方向的 倾斜变化动画效果,我们现实生活中的倾斜变化效果是非常常见的,比如翻书的纸张效果, 关门开门的时候门缝图形倾斜变换。在Silverlight中实现一个倾斜变化的动画效果是非常简 单的,如果利用Blend这种强大的设计工具来实现那更是锦上添花。

倾斜效果的动画应用效果其实非常好看,使用倾斜变换需要注意的有两点:倾斜方向和倾 斜中心点。可以以某点为倾斜中心点进行X或Y坐标方向进行倾斜,如下以默认中心店进行的 各种不同程度倾斜的简单示例:

如果使 用Blend来设计倾斜效果的变换动画就非常简单了,只需要在属性面板里设置相关的实现就可 以完成整个倾斜变换动画的设计,如下绘制了一扇门并为其设置开门的效果,开门的时候门 缝间的呈现效果就是以倾斜变换的效果实现的:

查看XAML文件可以发现,Blend生成了如下代码,用于实现开门的效果,需要注意的是这 里应用到了倾斜中心点,们是朝一个方向(X)开关,其呈现的倾斜效果就是Y坐标方向的倾 斜变换,

如上图所示,设置了Y坐标方向倾斜-17,既形成门向水平下方向倾斜效果。

Storyboard.TargetName=“door” Storyboard.TargetProperty=“(UIElement.RenderTransformOrigin)”>

Storyboard.TargetProperty=“(UIElement.RenderTransform).(TransformGroup.Children) [1].(SkewTransform.AngleY)”>

文章出处:beniao.cnblogs.com/

篇7:Silverlight & Blend动画设计系列一:偏移动画(TranslateTrans

用户界面组件、图像元素和多媒体功能可以让我们的界面生动活泼,除此之外, Silverlight还具备动画功能,它可以让应用程序“动起来”,实际上,英文中Animation这 个单词的意思是给某物带来生命。在界面中添加动画效果,给人以印象深刻可视化提示,可 以让用户的注意力集中到我们想让他们关注的地方。

动画主要是通过计时器来完成,在Silverlight中开发动画程序通常是使用微软主推的设 计工具Microsoft Expression Blend,Silverlight 中提供了优秀的动画系统,我们可以通 过Microsoft Expression Blend 快速的完成动画元素的设计制作,然后通过Visual Studio 作为编码环境进行后面的管理动画定时器和刷新用户界面的工作。当使用Microsoft Expression Blend 时,可以用拖拽的方式在时间线中进行动画的定义,这样可以很容易快速 定义负责的动画,因为 Microsoft Expression Blend 将自动生成对应的XAML代码。

Silverlight中的偏移动画和Flash中的补间动画基本一样,其实很好理解,也就是一个动 画元素(组件)从一个位置移动到另一个位置,这个过着中有三个关键点:动画起点、动画 终点和动画时间。这三点可以理解为一个动画元素从始点坐标向终点坐标偏移的缓冲时间是 多少。

当我们定义好了动画元素后,可以直接通过Blend中的对象和时间线面板来为动画元素添 加动画容器时间线(Storyboard),如下图所示:

当动画容器时间线创建好后,只需要选中需要进行创建动画的元素,然后再属性面板下进 行可视化属性设置就可以完成动画的创建,如下图示为创建元素darkMoon的偏移动画。

通过上面的一系列操作设置后切换到XAML视图,可以发现在XAML编码文件里Blend自动生 成了如下代码片段,其作用就是为名为“darkMoon”的动画元素创建两个方向(X,Y)的偏移 动画。

Storyboard.TargetProperty=“(UIElement.RenderTransform). (TransformGroup.Children)[3].(TranslateTransform.X)”>

Storyboard.TargetProperty=“(UIElement.RenderTransform). (TransformGroup.Children)[3].(TranslateTransform.Y)”>

上图为 动画执行过程截图,在某些情况下要实现一些动画效果是不能预先确定的,这就需要在程序 编码中去动态的创建动画效果,

使用Blend进行动画设计是非常简单的,尤其是对Adobe Flash熟悉的人员,其实在程序编码中使用程序创建动画也一样的简单,相比之下只是需要人 为的去编写很多的程序代码,另外就是通过程序代码动态的创建动画效果需要开发人员对 Silverlight的动画框架非常熟悉,只有在熟练应用Silverlight动画框架所提供的动画接口 的情况下才能够和使用 Blend设计动画一样随心应手实现各种不同的动画效果。

编写 程序代码创建动画其实也是非常简单的,只要理清了思路和熟练应用Silverlight的动画框架 提供的编程

接口就可以完成动画的开发。首先需要弄清楚动画容器时间线、动画动作、动画类型、动 画效果等基本概念。就以本篇的示例来分析,通过程序动态的创建动画效果的代码如下:

///

/// 创建动画

///

private void CreateStoryboard

{

//元 素当前所在的坐标点

Point currentPoint = new Point (Canvas.GetLeft(darkMoon), Canvas.GetTop(darkMoon));

//目标点坐int 标

Point point = new Point(280, -245);

//创建动画 容器时间线

Storyboard storyboard = new Storyboard();

DoubleAnimation doubleAnimation = new DoubleAnimation();

//创 建X轴方向动画

doubleAnimation.From = currentPoint.X;

doubleAnimation.To = point.X;

doubleAnimation.Duration = new Duration(new TimeSpan(0, 0, 1));

Storyboard.SetTarget (doubleAnimation, darkMoon);

Storyboard.SetTargetProperty (doubleAnimation,

new PropertyPath (“(UIElement.RenderTransform).(TransformGroup.Children)[3]. (TranslateTransform.X)”));

storyboard.Children.Add (doubleAnimation);

//创建Y轴方向动画

doubleAnimation = new DoubleAnimation();

doubleAnimation.SetValue (DoubleAnimation.FromProperty, currentPoint.Y);

doubleAnimation.SetValue(DoubleAnimation.ToProperty, point.Y);

doubleAnimation.SetValue(DoubleAnimation.DurationProperty, new Duration(new TimeSpan(0, 0, 1)));

Storyboard.SetTarget(doubleAnimation, darkMoon);

Storyboard.SetTargetProperty(doubleAnimation,

new PropertyPath(“(UIElement.RenderTransform). (TransformGroup.Children)[3].(TranslateTransform.Y)”));

storyboard.Children.Add(doubleAnimation);

storyboard.Begin();

}

文章出处:beniao.cnblogs.com/

篇8:Silverlight for Windows Phone 7开发系列(4):动画开发

前言

上一篇文章述了如何使用MediaElement控件来播放网络电台,讲述了MediaElement控件支持的媒体文件格式以及其一些限制性,同时讲述了Slider控件的使用和数据绑定的方法,这篇文章讲述如何使用Silverlight进行动画的开发。

新增图片和配置文件

我使用Paint.net做了些电台的(徽标)logo文件,存放在Images文件里面。

我把电台的配置信息保存到XML文件里面,以后可以通过修改该XML文件来扩展电台信息,提高可扩展性。甚至可以把配置文件存放到网上,程序每次启动的时候自动更新。

Stations标签是各个Station标签的容器,id是电台的唯一标识,name是电台的名字,url是在线播放的地址,不确定是否为模拟器 的原因,我试过网络电台只是支持mp3格式,我想在真机会像官方文档所说那样支持多种格式,所有支持的媒体文件格式请看上篇文章。image是电台的 logo图片文件的名字,存放在上述的Images文件夹下面。

把Config和Images文件夹拷贝到项目文件夹下面,然后把文件加入到项目中

先点击“Show All Files”按钮,然后右击Config和Images文件夹,选择“Include In Project”菜单。

需要注意的是,需要检查这些文件的属性,例如SilverRadio.xml文件的属性如下:

Logo图片文件的属性如下:

注意XML文件和Logo图片的文件属性不一样的,我发现新增文件的时候,Silverlight for Windows Phone已经自动设置好,不需要改动。但是我做Silverlight 4的时候,需要手工改动相应的属性,XML的Build Action为Content,而图片的为Resource。还有一个需要注意的是Silverlight不支持Embedded Resource,第一眼会觉得嵌入资源(Embedded Resource)比资源文件(Resource)更加贴切(我自己开始这样认为),可是Silverlight不支持这种文件形式,这种形式只支持在 WPF使用,但是Silverlight的Build Action保留了好多不支持的文件形式的选项,使用时需要注意哪些是Silverlight支持的,否则程序会出现找不到文件等异常错误。

LINQ for XML读取配置文件

使用LINQ读取XML配置文件,先定义一个实体类如下:

public class Station

{

public int Id { get; set; }

public string Name { get; set; }

public Uri Url { get; set; }

public string Image { get; set; }

}

下面是电台容器类Stations

public class Stations : List

{

public void Load(string xmlFile)

{

XDocument xDoc = XDocument.Load(xmlFile);

var query = from xElem in xDoc.Descendants(“station”)

select new Station

{

Id = Convert.ToInt32(xElem.Attribute(“id”).Value),

Name = xElem.Attribute(“name”).Value,

Url = new Uri(xElem.Attribute(“url”).Value, UriKind.Absolute),

Image = xElem.Attribute(“image”).Value,

};

Clear();

AddRange(query);

}

}

关于容器类的命名

我喜欢使用名词加上s作为容器类的名称,之前写blog过程中,有好几个读者说名词加s不好理解,我想是由于中文的名词没有复数的缘故,但是我看过 一些.NET Framework的源码,容器类也是使用名称加s的方式表示,习惯就好了,使用加s(例如Stations)而不是加上具体容器的名称(例如 StationList)的好处是重构容器类型的时候不需要修改容器的名称,

因为具体容器可能会修改,例如从List修改为Dictionary,但是可 以继续保留原有容器的名称Stations。

LINQ for XML

LINQ for XML主要有三个关键的类组成XDocumentXElement和XAttribute。我觉得弄明白这三个类已经能入门LINQ for XML了。XDocument表示XML文档,调用静态函数Load就可以把文档加载到XDocument的对象中。XElement表示一个元素(节点),通过XElement的属性Attribute(容器)就能取出该节点的相关属性。XAttribute表示节点的属性。,

在上述例子中把Station的每个节点取出来,然后把属性值传递给新建的Station对象。最后把所有Station对象存放到List容器中。

图片转盘的实现

先看效果图,有了感性认识比较好理解最终的实现效果。这是一个使用动态图片转盘的方式呈现所有电台,效果见上图,用户可以转动的方式换电台,选中的 电台会显示在最中间和最前方,显示最大比例,其他图根据与选中图的距离按比例缩小,模拟3D转盘的效果。下面详细讲述这个转盘类的实现。这个类的实现参考 了Flash vs Silverlight: Image Carousel.

XAML

xmlns=“schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=“schemas.microsoft.com/winfx/2006/xaml”

Width=“480” Height=“300”>

定义一个用户控件StationCarousel,该控件继承于UserControl,包含了一个Canvas容器,在这个例子中,使用 Canvas容器而不是Grid和StackPanel的原因是Canvas具有最大的可控性,由于Canvas是绝对值定位的,所以在Canvas里面 的图片(Image)可以计算出位置信息的绝对值,然后进行呈现。

【Silverlight:动画概述】相关文章:

1.动画策划书

2.滑动轴承概述

3.活动概述

4.创业项目概述

5.网络安全概述

6.年度工作概述

7.动画制作自荐信

8.动画作文600字

9.动画课件制作

10.公益广告动画策划书

下载word文档
《Silverlight:动画概述.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度: 评级1星 评级2星 评级3星 评级4星 评级5星
点击下载文档

文档为doc格式

  • 返回顶部