打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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

时间:2011-06-05 07:51来源:博客园 作者:焦涛 点击: 206次
(一)通过名称访问动画 (1)命名该动画对象,然后在代码中通过该名称引用它 (2)以使用集合来访问动画或动画的关键帧 a)访问动画集合:Storyboard 具有 Children 属性,该属性允许您访问指定 Storyboard 中的所有动画对象 b)访问关键帧集合:关键帧动画名称.KeyFrames[i] 实例讲解: Canvas MouseLeftButtonDown =Handle_Mouse
  

  (一)通过名称访问动画

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

本文来自焦涛的博客,原文地址:http://www.cnblogs.com/Joetao/articles/2054323.html

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
一步一步学Silverlight 2系列(32):图形图像综合实例—“功夫之王”剧照播放
WPF中的动画 by xirihanlin 幸福像花儿一样开放
衣柜安装演示动画
谈谈Silverlight 2中的视觉状态管理 Part2-李会军
Windows8动画
一起学Windows phone7开发(五.一个时钟的例子)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服