Silverlight动画学习笔记(四):以编程方式使用动画

(一)通过名称访问动画

(1)命名该动画对象,然后在代码中通过该名称引用它

(2)以使用集合来访问动画或动画的关键帧

      a)访问动画集合:Storyboard 具有 Children 属性,该属性允许您访问指定 Storyboard 中的所有动画对象

      b)访问关键帧集合:关键帧动画名称.KeyFrames[i]

      实例讲解: 

<Canvas MouseLeftButtonDown="Handle_MouseDown"
Width
="600" Height="500" Background="Gray">
<Canvas.Resources>
<Storyboard x:Name="myStoryboard">
<PointAnimationUsingKeyFrames
x:Name="myPointAnimationUsingKeyFrames"
Storyboard.TargetProperty
="Center"
Storyboard.TargetName
="MyAnimatedEllipseGeometry"
Duration
="0:0:3">

<DiscretePointKeyFrame KeyTime="0:0:0" />
<LinearPointKeyFrame KeyTime="0:0:0.5" />
<SplinePointKeyFrame KeySpline="0.6,0.0 0.9,0.00" KeyTime="0:0:2" />

</PointAnimationUsingKeyFrames>

</Storyboard>
</Canvas.Resources>

<Path Fill="Blue">
<Path.Data>

<!-- Describes an ellipse. -->
<EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
Center
="200,100" RadiusX="15" RadiusY="15" />
</Path.Data>
</Path>
</Canvas>

 

// Global variables that keep track of the end point
// of the last animation.
double lastX = 200;
double lastY = 100;
private void Handle_MouseDown(object sender, MouseEventArgs e)
{
// Retrieve current mouse coordinates.
double newX = e.GetPosition(null).X;
double newY = e.GetPosition(null).Y;

int i;
for (i = 0; i < myPointAnimationUsingKeyFrames.KeyFrames.Count; i++)
{
PointKeyFrame keyFrame
= myPointAnimationUsingKeyFrames.KeyFrames[i];
if (keyFrame.GetType().Name == "DiscretePointKeyFrame")
{
keyFrame.SetValue(DiscretePointKeyFrame.ValueProperty,
new Point(lastX, lastY));
}
else if (keyFrame.GetType().Name == "LinearPointKeyFrame")
{
// Calculate the slope.
double m = (newY - lastY) / (newX - lastX);

// Calculate the y-intercept.
double b = newY - (m * newX);

// Set X to a third of the way to the end point.
double intermediateX = lastX + (newX - lastX) / 3;

// Find the value Y from X and the line formula.
double intermediateY = (m * intermediateX) + b;

// Set the keyframe value to the intermediate x and y value.
keyFrame.SetValue(LinearPointKeyFrame.ValueProperty,
new Point(intermediateX, intermediateY));
}
else if (keyFrame.GetType().Name == "SplinePointKeyFrame")
{
keyFrame.SetValue(SplinePointKeyFrame.ValueProperty,
new Point(newX, newY));
}
}
myStoryboard.Stop();
myStoryboard.Begin();
lastX
= newX;
lastY
= newY;
}

(二)动态更改TargetName

     动态更改 Storyboard.TargetName 属性最常见的情况是您想将同一动画应用到多个对象

     优点:当具有要应用相似动画的大量对象时,这特别有用。例如,您可能要显示几行图像并使用动画突出显示鼠标当前所指示的图像。为每个图像创建单独的 Storyboard 对象非常麻烦。重用同一 Storyboard 更为合适。

     实例讲解:这里讲同一动画动态赋给选中的矩形图形,给人的感觉是每个矩形都具有动画效果。这里要注意的是,在赋给当前图形动画时,要将上一个动画播放停止。

 

private void Start_Animation(object sender, MouseEventArgs e)
{

// 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.
Rectangle myRect = (Rectangle)sender;

// 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();
}

  

     

  

posted @ 2011-05-23 14:47  焦涛  阅读(433)  评论(0)    收藏  举报