[源码下载]

背水一战 Windows 10 (14) - 动画: 线性动画, 关键帧动画

作者:webabcd

介绍
背水一战 Windows 10 之 动画

  • 线性动画 - ColorAnimation, DoubleAnimation, PointAnimation
  • 关键帧动画 - ColorAnimationUsingKeyFrames, DoubleAnimationUsingKeyFrames, PointAnimationUsingKeyFrames, ObjectAnimationUsingKeyFrames

示例
1、演示线性动画的应用
Animation/LinearAnimation.xaml

<Page
x:Class="Windows10.Animation.LinearAnimation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Animation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent"> <!--
线性动画一共有 3 种:ColorAnimation, DoubleAnimation, PointAnimation, 它们均继承自 Timeline Storyboard.TargetName - 附加属性,要进行动画处理的对象的名称
Storyboard.TargetProperty - 附加属性,要进行动画处理的对象的属性
BeginTime - 时间线在被触发 BeginTime 的时间后才能开始播放
TimeSpan - [-][日.]时:分:秒[.1位到7为的秒后的小数](可为正;可为负;可为空;默认值为 0)
From - 动画的起始值
To - 动画的结束值
By - 动画从起始值开始计算,所需变化的总量(To 优先于 By)
Duration - 时间线的持续时间
TimeSpan - [-][日.]时:分:秒[.1位到7为的秒后的小数]
Automatic - 自动确定
Forever - 无限长
AutoReverse - 动画完成后是否要原路返回。默认值为 false
RepeatBehavior - 动画重复播放的时间、次数或类型
TimeSpan - [-][日.]时:分:秒[.1位到7为的秒后的小数]
nx - 播放次数。1x, 2x, 3x
Forever - 永久播放
SpeedRatio - 时间线的速率的倍数。默认值 1
FillBehavior - 动画结束后的行为(System.Windows.Media.Animation.FillBehavior 枚举)
FillBehavior.HoldEnd - 动画结束后,UI 保留动画后的状态。默认值
FillBehavior.Stop - 动画结束后,UI 恢复为动画前的状态 注意:
1、在 WinRT 中为了流畅的体验,部分动画被优化成了“独立动画”,即动画不依赖于 UI 线程
2、但是也有一部分动画无法优化成“独立动画”,我们把这类动画称作“依赖动画”,其需要在 UI 线程上运行
3、通过将 EnableDependentAnimation 设置为 true(默认为 false),开启“依赖动画”
4、通过将 Timeline.AllowDependentAnimations 设置为 false(默认为 true),可以全局禁止开启“依赖动画” Independent Animation - 独立动画
Dependent Animation - 依赖动画
--> <Grid.Resources>
<BeginStoryboard x:Name="storyboardColor">
<Storyboard>
<!--Color 值线性动画-->
<!--
动画要修改的属性是 Ellipse.Fill,Fill 是 Brush 类型,先把其转换为 SolidColorBrush 类型,然后设置 SolidColorBrush 的 Color 属性
所以将 Storyboard.TargetProperty 设置为 (Ellipse.Fill).(SolidColorBrush.Color),也可以设置为 (Fill).(SolidColorBrush.Color),也可以设置为 (Ellipse.Fill).Color,也可以设置为 (Fill).(Color)
类似的比如:(UIElement.RenderTransform).(CompositeTransform.TranslateY) 以及 (UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX) 等
-->
<ColorAnimation
Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)"
BeginTime="00:00:00"
From="Red"
To="Yellow"
Duration="0:0:3"
AutoReverse="true"
RepeatBehavior="3x">
</ColorAnimation>
</Storyboard>
</BeginStoryboard> <BeginStoryboard x:Name="storyboardDouble">
<Storyboard>
<!--Double 值线性动画-->
<!--
动画要修改的属性是 Canvas.Left(附加属性)
将 Storyboard.TargetProperty 设置为 (Canvas.Left)
-->
<DoubleAnimation
Storyboard.TargetName="rectangle"
Storyboard.TargetProperty="(Canvas.Left)"
From="100"
By="100"
BeginTime="0:0:0"
Duration="00:00:03"
AutoReverse="true"
RepeatBehavior="Forever">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard> <Storyboard x:Name="storyboardPoint">
<!--Point 值线性动画-->
<!--
动画要修改的属性是 Center
将 Storyboard.TargetProperty 设置为 Center,也可以将其设置为 (EllipseGeometry.Center)
-->
<PointAnimation
EnableDependentAnimation="True"
Storyboard.TargetName="ellipseGeometry"
Storyboard.TargetProperty="Center"
BeginTime="00:00:00"
From="50,50"
To="200,200"
Duration="00:00:03"
AutoReverse="true"
RepeatBehavior="Forever">
</PointAnimation>
</Storyboard>
</Grid.Resources> <StackPanel Margin="10 0 10 10"> <Ellipse x:Name="ellipse" Fill="Orange" Width="200" Height="100" HorizontalAlignment="Left" /> <Canvas Width="400" Height="100" HorizontalAlignment="Left" Margin="0 10 0 0">
<Rectangle x:Name="rectangle" Fill="Orange" Width="200" Height="100" Canvas.Left="100" />
</Canvas> <Path Fill="Orange">
<Path.Data>
<EllipseGeometry x:Name="ellipseGeometry" Center="50,50" RadiusX="15" RadiusY="15" />
</Path.Data>
</Path> <!--用于演示如何在 CodeBehind 端定义 Storyboard-->
<Ellipse x:Name="ellipse2" Fill="Orange" Width="200" Height="100" HorizontalAlignment="Left" /> </StackPanel>
</Grid>
</Page>

Animation/LinearAnimation.xaml.cs

/*
* 本例用于演示如何通过 Storyboard 使用线性动画,线性动画一共有 3 种类型:ColorAnimation, DoubleAnimation, PointAnimation, 它们均继承自 Timeline
*/ using System;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation; namespace Windows10.Animation
{
public sealed partial class LinearAnimation : Page
{
public LinearAnimation()
{
this.InitializeComponent(); this.Loaded += LinearAnimation_Loaded;
} private void LinearAnimation_Loaded(object sender, RoutedEventArgs e)
{
// 启动动画
storyboardPoint.Begin(); // 停止动画
// storyboardPoint.Stop(); // 用于演示如何在 CodeBehind 端定义 Storyboard
// 定义一个 ColorAnimation
ColorAnimation ca = new ColorAnimation();
ca.BeginTime = TimeSpan.Zero;
ca.From = Colors.Red;
ca.To = Colors.Yellow;
ca.Duration = TimeSpan.FromSeconds();
ca.AutoReverse = true;
ca.RepeatBehavior = new RepeatBehavior();
Storyboard.SetTarget(ca, ellipse2);
Storyboard.SetTargetProperty(ca, "(Fill).(Color)"); // 定义一个 DoubleAnimation
DoubleAnimation da = new DoubleAnimation()
{
To = 0.9,
Duration = TimeSpan.FromSeconds(),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
Storyboard.SetTarget(da, ellipse2);
Storyboard.SetTargetProperty(da, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)");
// 注:要用 Storyboard 控制 ScaleTransform 则必须先为元素声明好 ScaleTransform(否则运行时会报错)
TransformGroup Group = new TransformGroup();
Group.Children.Add(new ScaleTransform());
ellipse2.RenderTransform = Group; // 将定义好的动画添加进 Storyboard 然后启动动画
Storyboard sb = new Storyboard();
sb.Children.Add(ca);
sb.Children.Add(da);
sb.Begin(); /*
* 注意:
* 1、在 WinRT 中为了流畅的体验,部分动画被优化成了“独立动画”,即动画不依赖于 UI 线程
* 2、但是也有一部分动画无法优化成“独立动画”,我们把这类动画称作“依赖动画”,其需要在 UI 线程上运行
* 3、通过将 EnableDependentAnimation 设置为 true(默认为 false),开启“依赖动画”
* 4、通过将 Timeline.AllowDependentAnimations 设置为 false(默认为 true),可以全局禁止开启“依赖动画”
*/
}
}
}

2、演示关键帧动画的应用
Animation/KeyFrameAnimation.xaml

<Page
x:Class="Windows10.Animation.KeyFrameAnimation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Animation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--
关键帧动画一共有 4 种:
ColorAnimationUsingKeyFrames, DoubleAnimationUsingKeyFrames, PointAnimationUsingKeyFrames, ObjectAnimationUsingKeyFrames 它们均继承自 Timeline ColorAnimationUsingKeyFrames 中的 KeyFrame 支持:
LinearColorKeyFrame, DiscreteColorKeyFrame, SplineColorKeyFrame, EasingColorKeyFrame DoubleAnimationUsingKeyFrames 中的 KeyFrame 支持:
LinearDoubleKeyFrame, DiscreteDoubleKeyFrame, SplineDoubleKeyFrame, EasingDoubleKeyFrame PointAnimationUsingKeyFrames 中的 KeyFrame 支持:
LinearPointKeyFrame, DiscretePointKeyFrame, SplinePointKeyFrame, EasingPointKeyFrame ObjectAnimationUsingKeyFrames 中的 KeyFrame 支持:
DiscreteObjectKeyFrame Linear 代表线性,Discrete 代表离散,Spline 代表三次贝塞尔曲线,Easing 代表缓动 Value - 关键帧的目标值
KeyTime - 到达关键帧目标值的时间
KeySpline - 三次贝塞尔曲线的两个控制点:x1,y1 x2,y2(该三次贝塞尔曲线的起点为0,0,终点为1,1)
--> <!--
ColorAnimationUsingKeyFrames
-->
<Grid Margin="5" HorizontalAlignment="Left">
<Grid.Resources>
<BeginStoryboard x:Name="storyboardColor">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="solidColorBrush" Storyboard.TargetProperty="Color" Duration="0:0:10">
<LinearColorKeyFrame Value="Green" KeyTime="0:0:3" />
<DiscreteColorKeyFrame Value="Blue" KeyTime="0:0:4" />
<SplineColorKeyFrame Value="Red" KeySpline="0.6,0.0 0.9,0.00" KeyTime="0:0:6" />
<EasingColorKeyFrame Value="Orange" KeyTime="0:0:8">
<EasingColorKeyFrame.EasingFunction>
<ElasticEase EasingMode="EaseInOut" />
</EasingColorKeyFrame.EasingFunction>
</EasingColorKeyFrame>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Grid.Resources>
<Rectangle Width="100" Height="50">
<Rectangle.Fill>
<SolidColorBrush x:Name="solidColorBrush" Color="Red" />
</Rectangle.Fill>
</Rectangle>
</Grid> <!--
DoubleAnimationUsingKeyFrames
-->
<Grid Margin="5" HorizontalAlignment="Left">
<Grid.Resources>
<BeginStoryboard x:Name="storyboardDouble">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="translateTransform" Storyboard.TargetProperty="X" Duration="0:0:10">
<LinearDoubleKeyFrame Value="500" KeyTime="0:0:3" />
<DiscreteDoubleKeyFrame Value="400" KeyTime="0:0:4" />
<SplineDoubleKeyFrame Value="300" KeySpline="0.6,0.0 0.9,0.00" KeyTime="0:0:6" />
<EasingDoubleKeyFrame Value="200" KeyTime="0:0:8">
<EasingDoubleKeyFrame.EasingFunction>
<ElasticEase EasingMode="EaseInOut" />
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Grid.Resources>
<Rectangle Fill="Red" Width="100" Height="50">
<Rectangle.RenderTransform>
<TranslateTransform x:Name="translateTransform" X="0" Y="0" />
</Rectangle.RenderTransform>
</Rectangle>
</Grid> <!--
PointAnimationUsingKeyFrames
-->
<Grid Margin="5" HorizontalAlignment="Left">
<Grid.Resources>
<BeginStoryboard x:Name="storyboardPoint">
<Storyboard>
<PointAnimationUsingKeyFrames Storyboard.TargetName="ellipseGeometry" Storyboard.TargetProperty="Center" Duration="0:0:10"
EnableDependentAnimation="True">
<LinearPointKeyFrame Value="100,100" KeyTime="0:0:3" />
<DiscretePointKeyFrame Value="200,200" KeyTime="0:0:4" />
<SplinePointKeyFrame Value="300,300" KeySpline="0.6,0.0 0.9,0.00" KeyTime="0:0:6" />
<EasingPointKeyFrame Value="400,400" KeyTime="0:0:8">
<EasingPointKeyFrame.EasingFunction>
<ElasticEase EasingMode="EaseInOut" />
</EasingPointKeyFrame.EasingFunction>
</EasingPointKeyFrame>
</PointAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Grid.Resources>
<Path Fill="Red">
<Path.Data>
<EllipseGeometry x:Name="ellipseGeometry" Center="50,50" RadiusX="15" RadiusY="15" />
</Path.Data>
</Path>
</Grid> <!--
ObjectAnimationUsingKeyFrames
-->
<Grid Margin="5" HorizontalAlignment="Left">
<Grid.Resources>
<BeginStoryboard x:Name="storyboardObject">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="textBlock" Storyboard.TargetProperty="Text" Duration="0:0:10">
<DiscreteObjectKeyFrame KeyTime="0:0:1" Value="w" />
<DiscreteObjectKeyFrame KeyTime="0:0:2" Value="we" />
<DiscreteObjectKeyFrame KeyTime="0:0:3" Value="web" />
<DiscreteObjectKeyFrame KeyTime="0:0:4" Value="weba" />
<DiscreteObjectKeyFrame KeyTime="0:0:5" Value="webab" />
<DiscreteObjectKeyFrame KeyTime="0:0:6" Value="webabc" />
<DiscreteObjectKeyFrame KeyTime="0:0:7" Value="webabcd" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Grid.Resources>
<TextBlock x:Name="textBlock" Width="200" FontSize="32" Text="" />
</Grid> </StackPanel>
</Grid>
</Page>

OK
[源码下载]

背水一战 Windows 10 (14) - 动画: 线性动画, 关键帧动画的更多相关文章

  1. 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果)

    [源码下载] 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果) 作者:webabcd 介绍背水一战 Windows 10 之 动画 ThemeTrans ...

  2. 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画)

    [源码下载] 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画) 作者:webabcd 介绍背水一战 Windows 10 之 动画 PopInThemeA ...

  3. 背水一战 Windows 10 (15) - 动画: 缓动动画

    [源码下载] 背水一战 Windows 10 (15) - 动画: 缓动动画 作者:webabcd 介绍背水一战 Windows 10 之 动画 缓动动画 - easing 示例演示缓动(easing ...

  4. 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画

    [源码下载] 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) Frame 动画 示例An ...

  5. 背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox

    [源码下载] 背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox 作者:webabcd ...

  6. 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null

    [源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...

  7. 背水一战 Windows 10 (4) - UI: 多窗口

    [源码下载] 背水一战 Windows 10 (4) - UI: 多窗口 作者:webabcd 介绍背水一战 Windows 10 之 UI 多窗口 示例1.自定义帮助类,用于简化 Secondary ...

  8. 背水一战 Windows 10 (111) - 通知(Tile): secondary tile 模板之图片, secondary tile 模板之分组

    [源码下载] 背水一战 Windows 10 (111) - 通知(Tile): secondary tile 模板之图片, secondary tile 模板之分组 作者:webabcd 介绍背水一 ...

  9. 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性

    [源码下载] 背水一战 Windows 10 (78) - 自定义控件: 基础知识, 依赖属性, 附加属性 作者:webabcd 介绍背水一战 Windows 10 之 控件(自定义控件) 自定义控件 ...

随机推荐

  1. HTML和CSS经典布局2

    如下图: 需求: 1. 如图 2. 可以从body标签开始. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xht ...

  2. ASP.NET MVC 从零开始 - 请求处理

    这篇文章是从我的 github 博客 lxconan.github.io 导入的. 这是这个系列的第三篇了.前两篇文章请参见: ASP.NET MVC 从零开始 - Create and Run AS ...

  3. volatile用法

    1.volatile 主要是 其 "可见性",在java内存模型中,变量都是放在主内存中,每条线程里面有自己的工作内存,当一个变量被volatile 修饰时候,其他的线程会得到该变 ...

  4. HTML5_03之Canvas绘图

    1.Canvas绘图--JS绘图: <canvas id='c1' width='' height=''></canvas> * Canvas尺寸不能用CSS设置: c1.he ...

  5. 日志log

    public class LoggerHelper { private static Queue<string> qMsg = null; private static string lo ...

  6. Ajax概要:

    Ajax概要: Ajax不是个全新的技术,它是多种技术合并在一起产生的,包括XHTML,CSS,JavaScript,XmlHttpRequest,XML,JSON,DOM等 优点:(这也解释了为何我 ...

  7. spring4mvc返回json(bean,list,map)

    因为spring3和spring4的mvc在前端返回json所需要的jar包不一样,所以索性写一篇关于spring4mvc在前端返回json的博文. 首先,新建一个web项目,项目格式如图所示: co ...

  8. Kali信息收集系列:(都是我以前的笔记整理了一下,就没加水印,习惯就好)

    好几天没发微信公众号了,今天一起发下.(最近有点事情) 前些天老业界的一位朋友问我一些Safe新时代信息收集的问题 逆天虽然好多年不干老本行,但隔段时间都会关注一下 于是就花了点时间整理了一下,你们就 ...

  9. 前端学PHP之面向对象系列第三篇——三大特性

    × 目录 [1]封装 [2]继承[3]多态 前面的话 php面向对象编程的三大特性是封装性.继承性和多态性.本文将介绍php的这三大特性 封装 封装就是把对象中的成员属性和成员方法加上访问修饰符( p ...

  10. 深入学习jQuery选择器系列第三篇——过滤选择器之索引选择器

    × 目录 [1]通用形式 [2]首尾索引 [3]奇偶索引[4]范围索引 前面的话 上一篇介绍了过滤选择器中的子元素选择器部分,本文开始介绍极易与之混淆的索引选择器 通用形式 $(':eq(index) ...