silverlight中用代码动态控制Storyboard(动画)属性的三种方法
时间:2022-04-23
本文章向大家介绍silverlight中用代码动态控制Storyboard(动画)属性的三种方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
先准备一个基本的xaml页面
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,然后引用赋值
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)">
<EasingDoubleKeyFrame KeyTime="00:00:01" Value="360" x:Name="yFrom"/>
<EasingDoubleKeyFrame KeyTime="00:00:02" Value="720" x:Name="yTo"/>
</DoubleAnimationUsingKeyFrames>
注意高亮部分,然后象这样引用
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中的动画设定全部清空 了,同时增加了一个三帧的动画
欢迎转载,但请注明来自菩提树下的杨过
- Elasticsearch聚合 之 Terms
- Elasticsearch聚合初探——metric篇
- AngularJS API之$injector ---- 依赖注入
- AngularJS API之extend扩展对象
- AngularJS API之equal比较对象
- Elasticsearch之_default_—— 为索引添加默认映射
- Elasticsearch 动态映射——自动检测
- Elaticsearch REST API常用技巧
- C++拷贝构造函数
- 记录安装oracle的那些事(一)之oracle我很大
- Elasticsearch 连接查询
- 小程序加戏成功!变身游戏成新版微信最大主角,用户玩得不亦乐乎
- 堆实例
- 大数加法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 别再暴力匹配字符串了,高效的KMP,才是真的香
- 一文透析SpringCloud,关于Bus消息总线,总算梳理清楚了
- LeetCode 102. 二叉树的层序遍历
- 剑指Offer LeetCode 面试题10- II. 青蛙跳台阶问题
- 剑指Offer LeetCode 面试题10- I. 斐波那契数列
- 蓝桥杯 1的个数
- 蓝桥杯-试题 算法训练 数据交换
- 解决mysql导入新数据库大小写问题(Table 'zup.Domain_System' doesn't exist)
- 蓝桥杯vip试题 报时助手
- 蓝桥杯-基础练习 查找整数
- 蓝桥杯-基础练习 数列排序
- 无线网络-何为ISM频段?
- 蓝桥杯vip测试题系统试题-算法提高 矩阵转置
- 蓝桥杯vip测试题系统-数组求和(解题思路以及解题代码,手画思路图虽然丑丑的)
- 蓝桥杯vip测试题-找零钱(解题思路以及解题代码)