< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="GreenBall"
Storyboard.TargetProperty ="(UIElement.Rendertransform).(transformGroup.Children)[3].(Translatetransform.X)" >
< EasingDoubleKeyFrame KeyTime ="00:00:02" Value ="540" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard > StoryBoard提供了六个常用的动画属性选项,它们分别是:autoReverse,BeginTime,Duration,FillBehavior,RepeatBehavior,Speedratio。通过这六个属性可以用来控制动画的基本行为动作,比如想要实现动画的缓冲时间,就需要用到Duration属性;设置动画的开始时间则用BeginTime;如果动画执行完后根据执行路线反向执行到原始状态则需要使用autoReverse;如果需要设置动画的运行速度则使用Speedratio就可以完成。以下代码块演示了autoReverse属性的使用,动画运行完后将按着原来的运行路线进行反向运行。更多详细可参考这篇博文介绍:《 动画基础快速入门Animation》或者MSDN。 < Storyboard x:name ="MoveBall" autoReverse ="True" >
< DoubleAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="GreenBall"
Storyboard.TargetProperty ="(UIElement.Rendertransform).(transformGroup.Children)[3].(Translatetransform.X)" >
< EasingDoubleKeyFrame KeyTime ="00:00:02" Value ="540" />
</ DoubleAnimationUsingKeyFrames >
</ Storyboard > Storyboard的属性是可以组合应用的,如上代码块给动画设置了autoReverse属性,使其在动画执行完后通过原来的运行路径进行回滚动画,可以给该动画时间线容器添加一个BeginTime属性,使其在程序加载后5秒钟才开始执行动画,这样就使用到了两个属性,如下代码块: < Storyboard x:name ="MoveBall" autoReverse ="True" BeginTime ="00:00:05" >
......
</ Storyboard > 二、动画类型(Types of animation) Silverlight中的动画主要分 From/To/By和 关键帧动画两种类型。 From/To/By动画也称为线性插值动画(linear Interpolation),是Silverlight类型中最简单的一种动画,只需要设置开始(From)、结束(To)和动画值(By)就可以完成动画的创建,Silverlight 3中提供了三种基础的From/To/By动画类型:DoubleAnimation、colorAnimation和PointAnimation. 关键帧动画比From/To/By动画更加强大,无需指定动画的开始、结束以及动画缓冲时间等相关参数,只需要关注关键帧和如何去控制动画就行了。Silverlight 3中提供了四种基本的关键帧动画: 缓和(Easing)、线性(linear)、样条(Spline)和离散(discreet)。 DoubleAnimation的使用是非常简单的,只需要搞清楚From/To/By三要素就基本掌握了该类型动画的使用,下面是一个简单的通过DoubleAnimation实现的圆形移动的示例。 < UserControl x:Class ="DoubleByAnimation.MainPage"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
WIDth ="800" Height ="600" >
< UserControl.Resources >
< Storyboard x:name ="Storyboard1" >
< DoubleAnimation Storyboard.Targetname ="Ellipse"
Storyboard.TargetProperty ="(UIElement.Rendertransform).(transformGroup.Children)[3].(Translatetransform.X)"
From ="0"
By ="150"
Duration ="00:00:01" />
</ Storyboard >
</ UserControl.Resources >
< Canvas x:name ="LayoutRoot" Background ="White" >
< Ellipse Height ="200" WIDth ="200" Fill ="#FFFF0000" Canvas.top ="181" Canvas.left ="92" RendertransformOrigin ="0.5,0.5" x:name ="Ellipse" >
< Ellipse.Rendertransform >
< transformGroup >
< Scaletransform />
< Skewtransform />
< Rotatetransform />
< Translatetransform />
</ transformGroup >
</ Ellipse.Rendertransform >
</ Ellipse >
</ Canvas >
</ UserControl > 下面通过一个稍复杂的示例来演示DoubleAnimation动画的使用,如下动画代码块实现了名称为SlIDer对象的X坐标方向的移动动画: < Storyboard x:name ="SlIDeOut" >
< DoubleAnimation Storyboard.Targetname ="SlIDer"
Storyboard.TargetProperty ="(UIElement.Rendertransform).(transformGroup.Children)[3].(Translatetransform.X)"
Duration ="00:00:00.50" To ="150" />
</ Storyboard >
< Storyboard x:name ="SlIDeIn" >
< DoubleAnimation Storyboard.Targetname ="SlIDer"
Storyboard.TargetProperty ="(UIElement.Rendertransform).(transformGroup.Children)[3].(Translatetransform.X)"
Duration ="00:00:00.50" To ="0" />
</ Storyboard > 如上动画定义代码块中定义了两个动画,一个实现将对象SlIDer向X坐标方向移动到150像素点的位置,第二个动画实现将名为Silder的对象向X方向移动到0像素点的坐标位置,这样其实就实现了一个呈现与不呈现的效果。这里我们发挥大脑的潜力想象一下,如果SlIDer是一个面板对象,通过鼠标指向和离开的事件使用上面两个动画进行控制其不就实现了面板的鼠标指向就显示,离开就退回到原来的位置了?答案却是如此,详细见下代码块: public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
SlIDer.MouseEnter += new MouseEventHandler(SlIDer_MouseEnter);
SlIDer.MouseLeave += new MouseEventHandler(SlIDer_MouseLeave);
}
private voID SlIDer_MouseLeave( object sender, MouseEventArgs e)
{
SlIDeIn.Begin();
}
private voID SlIDer_MouseEnter( object sender, MouseEventArgs e)
{
SlIDeOut.Begin();
}
} 通过上面的示例,是否觉得要在Silverlight中实现一个动画是非常简单的?上面的示例就是掩饰了如何使用From/To/By的DoubleAnimation实现了对象的移动动画,同样也可以使用关键帧动画(DoubleusingKeyframes)来实现这一功能。如下代码片段演示了元素Silder向X坐标方向移动到150像素点。 Storyboard MoveRight = new Storyboard();
DoubleAnimationUsingKeyFrames Anim = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTargetname(Anim, " SlIDer " );
Anim.SetValue(Storyboard.TargetPropertyProperty,
new PropertyPath( " (UIElement.Rendertransform).(transformGroup.Children)[3].(Translatetransform.X) " ));
Anim.BeginTime = new TimeSpan( 0 , 0 , 0 );
SplineDoubleKeyFrame SKeyFrame = new SplineDoubleKeyFrame();
SKeyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds( 0.5 ));
SKeyFrame.Value = 150 ;
Anim.KeyFrames.Add(SKeyFrame);
MoveRight.Children.Add(Anim); ...... colorAnimation类型动画主要应用于颜色上的变换动画效果,比如有一个圆,默认的填充颜色是红色(Red),设计一个动画通过2秒钟的动画换成将圆的填充颜色变换为蓝色。 < Ellipse Height ="218" WIDth ="218" Canvas.left ="294" Canvas.top ="195" Fill ="#FFFF0000" Cursor ="Hand" x:name ="RedEllipse" /> 可以通过Blend的设计器来完成这样的动画的创建,在对象和时间轴面板里新建动画容器时间线,然后选中RadEllipse圆形对象在0秒的时间线上填充其颜色为红色,2秒的时间线上填充其颜色为蓝色,详细设计如下图: Blend最终生成的XAML代码如下: < Storyboard x:name ="Storyboard1" >
< colorAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="RedEllipse"
Storyboard.TargetProperty ="(Shape.Fill).(SolIDcolorBrush.color)" >
< EasingcolorKeyFrame KeyTime ="00:00:00" Value ="Red" />
< EasingcolorKeyFrame KeyTime ="00:00:02" Value ="Blue" />
</ colorAnimationUsingKeyFrames >
</ Storyboard > 有Blend这样强大的设计工具是很幸运的,对于设计人员来说要实现一些动画需求直接通过界面设计就完成了。对于程序员来说就烦恼了,由于不熟悉设计工具,要实现某种动画只能通过编写程序代码来完成,这将是一个很庞大的工程,比如说当鼠标指向一圆的时候其填充颜色慢慢的向蓝色(Blue)渐变,鼠标离开的时候慢慢的恢复其默认颜色红色。实现这动画效果则需要写上如下长篇的程序代码: private Storyboard TurnBlue = new Storyboard();
private Storyboard TurnRed = new Storyboard();
private colorAnimation Bluecolor = new colorAnimation();
private colorAnimation Redcolor = new colorAnimation();
public MainPage()
{
InitializeComponent();
Bluecolor.SetValue(Storyboard.TargetnameProperty, " RedEllipse " );
Bluecolor.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath( " (Shape.Fill).(SolIDcolorBrush.color) " ));
Bluecolor.To = colors.Blue;
TurnBlue.Children.Add(Bluecolor);
LayoutRoot.Resources.Add( " TurnBlue " , TurnBlue);
Redcolor.SetValue(Storyboard.TargetnameProperty, " RedEllipse " );
Redcolor.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath( " (Shape.Fill).(SolIDcolorBrush.color) " ))
Redcolor.To = colors.Red;
TurnRed.Children.Add(Redcolor);
LayoutRoot.Resources.Add( " TurnRed " , TurnRed);
RedEllipse.MouseEnter += (senderRed, eRed) =>
{
TurnRed.Begin();
};
RedEllipse.MouseLeave += (senderBlue, eBlue) =>
{
TurnBlue.Begin();
};
} 这样的代码实现其实就是用程序代码创建了两个动画,一个由红色变换到蓝色,一个则由蓝色变换到红色,按照前面的方法直接在Blend中设计也是可以的。最终的运行效果如下图: PointAnimation类型动画更好理解,也就是动画效果是通过不同的坐标点来控制的。比如说浪涛的效果,下面就以浪涛为示例来演示PointAnimation类型动画的使用。 代码 < Storyboard x:name ="Storyboard1" RepeatBehavior ="Forever" FillBehavior ="HoldEnd" >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[1].(BezIErSegment.Point2)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="351.732116699219,36.4064197540283" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="415.732116699219,84.4064178466797" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[1].(BezIErSegment.Point3)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="425.502014160156,32.8349914550781" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="489.502014160156,80.8349914550781" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[2].(BezIErSegment.Point1)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="499.271911621094,29.2635669708252" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="563.271911621094,77.2635650634765" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[0].(BezIErSegment.Point2)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="112.729011535644,80.834991455078" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="104.729011535645,32.8349914550781" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[0].(BezIErSegment.Point3)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="185.502014160156,80.834991455078" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="177.502014160156,32.8349914550781" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[1].(BezIErSegment.Point1)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="258.275024414062,80.834991455078" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="250.275024414063,32.8349914550781" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[2].(BezIErSegment.Point2)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="600.704162597656,72.7879943847655" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="608.704162597656,32.8349229097365" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[2].(BezIErSegment.Point3)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="665.502014160156,72.7879943847655" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="673.502014160156,32.8349229097365" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[3].(BezIErSegment.Point1)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="730.299926757813,72.7879943847655" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="738.299926757813,32.8349229097365" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[3].(BezIErSegment.Point2)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="801.502014160156,40.8349914550781" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="801.502014160156,56.8349229097366" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[3].(BezIErSegment.Point3)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="801.502014160156,56.8349229097366" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[4].(BezIErSegment.Point1)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="801.502014160156,56.8349229097366" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.StartPoint)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="1.50201416015619,32.834991455078" />
< EasingPointKeyFrame KeyTime ="00:00:03" Value ="1.50201416015625,88.8349914550779" />
</ PointAnimationUsingKeyFrames >
< PointAnimationUsingKeyFrames BeginTime ="00:00:00" Storyboard.Targetname ="water" Storyboard.TargetProperty ="(Path.Data).(PathGeometry.figures)[0].(Pathfigure.Segments)[0].(BezIErSegment.Point1)" >
< EasingPointKeyFrame KeyTime ="00:00:00" Value ="1.50201416015619,88.8349914550779" />
</ PointAnimationUsingKeyFrames >
</ Storyboard > 关于Silverlight中的动画相关的理论概论暂时就介绍这些,希望上面的简单介绍能帮助大家深入的理解和使用Blend进行各种不同的动画功能设计。更多信息可查阅下面的推资源连接。 推荐资源: MSDN: http://msdn.microsoft.com/zh-cn/library/cc189090(VS.95).aspx http://www.silverlight.net/learn/quickstarts/animations/ http://blog.csdn.net/ghj1976/archive/2009/12/22/5056916.aspx Silverlight & Blend动画设计系列一:偏移动画(TranslateTransform) Silverlight & Blend动画设计系列二:旋转动画(RotateTransform) Silverlight & Blend动画设计系列三:缩放动画(ScaleTransform) Silverlight & Blend动画设计系列四:倾斜动画(SkewTransform) 总结
以上是内存溢出为你收集整理的Silverlight & Blend动画设计系列五:故事板(StoryBoards)和动画(Animations)全部内容,希望文章能够帮你解决Silverlight & Blend动画设计系列五:故事板(StoryBoards)和动画(Animations)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)