silverlight中用代码动态控制Storyboard(动画)属性的三种方法

简介: 先准备一个基本的xaml页面 Code 111    12        13        14            15            16            17                18                19            20       ...

先准备一个基本的xaml页面

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
 1<navigation:Page 
 2           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 4           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6           mc:Ignorable="d"
 7           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
 8           x:Class="Projection.Test.TestPage"
 9           d:DesignWidth="640" d:DesignHeight="480"
10           Title="TestPage Page">
11    <navigation:Page.Resources>
12        
13        <Storyboard x:Name="storyTest">
14            
15            <!--绕y轴旋转,从0到360,再转到720度-->
16            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)">
17                <EasingDoubleKeyFrame KeyTime="00:00:01" Value="360"/>
18                <EasingDoubleKeyFrame KeyTime="00:00:02" Value="720"/>
19            </DoubleAnimationUsingKeyFrames>
20            
21            <!--宽度按比例放大缩小,先放大到4位,再还原-->
22            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
23                <EasingDoubleKeyFrame KeyTime="00:00:01" Value="4"/>
24                <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
25            </DoubleAnimationUsingKeyFrames>
26            
27            <!--高度按比例放大缩小,先放大到4位,再还原-->
28            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
29                <EasingDoubleKeyFrame KeyTime="00:00:01" Value="4"/>
30                <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
31            </DoubleAnimationUsingKeyFrames>            
32            
33            <!--不透明度变化,从1到0.1,再还原为1-->
34            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.Opacity)">
35                <EasingDoubleKeyFrame KeyTime="00:00:01" Value="0.1"/>
36                <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
37            </DoubleAnimationUsingKeyFrames>
38        </Storyboard>
39        
40    </navigation:Page.Resources>
41    <Grid x:Name="LayoutRoot">
42
43        <Image x:Name="img" Source="/Projection;Component/img/img0.jpg" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
44            <Image.RenderTransform>
45                <TransformGroup>
46                    <ScaleTransform/>
47                    <SkewTransform/>
48                    <RotateTransform/>
49                    <TranslateTransform/>
50                </TransformGroup>
51            </Image.RenderTransform>
52            <Image.Projection>
53                <PlaneProjection/>
54            </Image.Projection>
55            <Image.Effect>
56                <DropShadowEffect/>
57            </Image.Effect>
58        </Image>
59
60    </Grid>
61</navigation:Page>

结构不复杂,里面就放了一张图片,同时预置了一个动画storyTest,里面把几种常见的动画形式都列在里面了,下面就来看看如何动态改变storyTest动画的属性(比如改变旋转的起始角度之类)

1.直接加x:Name,然后引用赋值

1 < DoubleAnimationUsingKeyFrames  BeginTime ="00:00:00"  Storyboard.TargetName ="img"  Storyboard.TargetProperty ="(UIElement.Projection).(PlaneProjection.RotationY)" >
2      < EasingDoubleKeyFrame  KeyTime ="00:00:01"  Value ="360"   x:Name ="yFrom" />
3      < EasingDoubleKeyFrame  KeyTime ="00:00:02"  Value ="720"   x:Name ="yTo" />
4 </ DoubleAnimationUsingKeyFrames >
5

注意高亮部分,然后象这样引用

System.Windows.Media.Animation.EasingDoubleKeyFrame yFrom  =   this .FindName( " yFrom " as  System.Windows.Media.Animation.EasingDoubleKeyFrame;

yFrom.Value 
=   20 ;

这样我们就把00:00:01秒时角度由360改为20度了

2.利用StoryBoard的Children属性

 DoubleAnimationUsingKeyFrames _rotate  =  storyTest.Children[ 0 as  DoubleAnimationUsingKeyFrames;
 _rotate.KeyFrames[
0 ].Value  =   90 ; // 修改旋转角度的值
 _rotate.KeyFrames[ 1 ].Value  =   180 ;

 storyTest.RepeatBehavior 
=  RepeatBehavior.Forever; // 指定为一直播放
 
// this.storyTest.RepeatBehavior = new RepeatBehavior(2); // 播放2次

这样我们就把旋转的角度值从360,720改为90,180了

说明:StoryBoard的Children属性得到的是一个Timeline的集合,而DoubleAnimationUsingKeyFrames等这些基本的关键帧类型都是继承自Timeline,因此可以用as安全的把Timeline向下转化为DoubleAnimationUsingKeyFrames

3.动态添加/删除关键帧

当xaml中预定义的关键帧不满足要求时,这种方法就几乎成了唯一选择

storyTest.Children.Clear(); // 清除原来的动画设定,仅保留一个空壳           

DoubleAnimationUsingKeyFrames _new_rotate 
=   new  DoubleAnimationUsingKeyFrames();

EasingDoubleKeyFrame _frame1 
=   new  EasingDoubleKeyFrame();
_frame1.Value 
=   180 ;
_frame1.KeyTime 
=   new  TimeSpan( 0 0 0 1 );

EasingDoubleKeyFrame _frame2 
=   new  EasingDoubleKeyFrame();
_frame2.Value 
=   0 ;
_frame2.KeyTime 
=   new  TimeSpan( 0 0 0 2 );


EasingDoubleKeyFrame _frame3 
=   new  EasingDoubleKeyFrame();
_frame3.Value 
=   90 ;
_frame3.KeyTime 
=   new  TimeSpan( 0 0 0 5 );

_new_rotate.KeyFrames.Add(_frame1);
_new_rotate.KeyFrames.Add(_frame2);
_new_rotate.KeyFrames.Add(_frame3);

storyTest.Children.Add(_new_rotate);

Storyboard.SetTarget(_new_rotate, 
this .img);
Storyboard.SetTargetProperty(_new_rotate, 
new  PropertyPath( " (UIElement.Projection).(PlaneProjection.RotationY) " ));

storyTest.AutoReverse 
=   false ;

// storyTest.FillBehavior = FillBehavior.Stop; // 加入这一行后,不管AutoReverse设置为何值,都会回复原状            

storyTest.Begin();

在这里我们把原来storyTest中的动画设定全部清空 了,同时增加了一个三帧的动画

欢迎转载,但请注明来自菩提树下的杨过(http://yjmyzz.cnblogs.com/)

目录
相关文章
|
容器
Silverlight & Blend动画设计系列二:旋转动画(RotateTransform)
原文:Silverlight & Blend动画设计系列二:旋转动画(RotateTransform)   Silverlight的基础动画包括偏移、旋转、缩放、倾斜和翻转动画,这些基础动画毫无疑问是在Silverlight中使用得最多的动画效果,其使用也是非常简单的。
1056 0
|
容器 数据可视化 内存技术
Silverlight & Blend动画设计系列一:偏移动画(TranslateTransform)
原文:Silverlight & Blend动画设计系列一:偏移动画(TranslateTransform)   用户界面组件、图像元素和多媒体功能可以让我们的界面生动活泼,除此之外,Silverlight还具备动画功能,它可以让应用程序“动起来”。
838 0
Silverlight & Blend动画设计系列四:倾斜动画(SkewTransform)
原文:Silverlight & Blend动画设计系列四:倾斜动画(SkewTransform)   Silverlight中的倾斜变化动画(SkewTransform)能够实现对象元素的水平、垂直方向的倾斜变化动画效果。
865 0
|
容器
Silverlight & Blend动画设计系列五:故事板(StoryBoards)和动画(Animations)
原文:Silverlight & Blend动画设计系列五:故事板(StoryBoards)和动画(Animations)   正如你所看到的,Blend是一个非常强大的节约时间的设计工具,在Blend下能够设计出很多满意的动画作品,或许他具体是怎么实现的,通过什么方式实现的我们还是一无所知。
961 0
Silverlight & Blend动画设计系列七:模糊效果(BlurEffect)与阴影效果(DropShadowEffect)
原文:Silverlight & Blend动画设计系列七:模糊效果(BlurEffect)与阴影效果(DropShadowEffect)   模糊效果(BlurEffect)与阴影效果(DropShadowEffect)是两个非常实用和常用的两个特效,比如在开发相册中,可以对照片的缩略图添加模糊效果,在放大照片的过程中动态改变照片的大小和模糊的透明度来达到一个放大透明的效果。
1105 0
Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效
原文:Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效   当我们在进行Silverlight & Blend进行动画设计的过程中,可能需要设计出很多效果不一的图形图像出来作为动画的基本组成元素。
1082 0
|
API
Silverlight & Blend动画设计系列十:Silverlight中的坐标系统(Coordinate System)与向量(Vector)运动
原文:Silverlight & Blend动画设计系列十:Silverlight中的坐标系统(Coordinate System)与向量(Vector)运动   如果我们习惯于数学坐标系,那么对于Silverlight中的坐标系可能会有些不习惯。
1270 0
|
UED
Silverlight & Blend动画设计系列九:动画(Animation)与视图状态管理(Visual State Manager)
原文:Silverlight & Blend动画设计系列九:动画(Animation)与视图状态管理(Visual State Manager)   Silverlight中的动画(Animation)与视图状态管理(Visual State Manager) 结合使用是非常常见的,动画用于管理对象在某段事件段内执行的动画动作,视图状态管理则用于控制对象在多个不同的视觉状态之间切换、导航。
813 0
Silverlight & Blend动画设计系列八:拖放(Drag-Drop)操作与拖放行为(DragBehavior)
原文:Silverlight & Blend动画设计系列八:拖放(Drag-Drop)操作与拖放行为(DragBehavior)   在Silverlight中自身并没有提供拖放功能的相关实现,要实现拖放功能得借助其事件支持(MouseLeftButtonDown、MouseLeftButtonUp和MouseMove)来完成,实际应用中我们可以通过行为(Behavior)特性将拖放操作封装为行为,这样可达到代码复用的效果。
1093 0
Silverlight & Blend动画设计系列十三:三角函数(Trigonometry)动画之飘落的雪花(Falling Snow)
原文:Silverlight & Blend动画设计系列十三:三角函数(Trigonometry)动画之飘落的雪花(Falling Snow)   平时我们所看到的雪花(Falling Snow)飘飘的效果实际上也是一个动画,是由许多的动画对象共同完成的一个界面效果。
981 0

热门文章

最新文章