由于UIElement类实现IAnimatable接口,而FramworkElement又派生自UIElement,所以大部分的UI控件都包含IAnimatable属性的对象。如Button,TabControl,Panel和Shape等,且大多数属性都是依赖属性。如果要实现一个Rectangle元素从视野中逐渐消失,然后再次出现,并循环播放,应该怎么实现呢?
设置触发事件:通常情况下,可以将开始故事板(BeginStoryboard)与事件触发器(EventTrigger)进行关联。并添加到EventTrigger的Actions中,并指定RoutedEvent属性设置启动Storyboard的路由事件。
<StackPanel Margin="10"> <Rectangle Name="MyRectangle" Width="100" Height="100" Fill="Blue"> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </StackPanel>上述代码在C#中实现,如下所示:
StackPanel panel = new StackPanel(); panel.Margin = new Thickness(10); Rectangle rectangle = new Rectangle(); rectangle.Name = "rectangle"; this.RegisterName(rectangle.Name, rectangle); rectangle.Width = 100; rectangle.Height = 100; rectangle.Fill = Brushes.Blue; DoubleAnimation animation = new DoubleAnimation(); animation.From = 1.0; animation.To = 0.0; animation.Duration = new Duration(TimeSpan.FromSeconds(5)); animation.AutoReverse = true; animation.RepeatBehavior = RepeatBehavior.Forever; // 堆代码 duidaima.com var storyboard = new Storyboard(); storyboard.Children.Add(animation); Storyboard.SetTargetName(animation, rectangle.Name); Storyboard.SetTargetProperty(animation, new PropertyPath(Rectangle.OpacityProperty)); rectangle.Loaded += new RoutedEventHandler((sender,e) =>{ storyboard.Begin(this); }); panel.Children.Add(rectangle); this.Content = panel;动画类型
时间线
所有的动画类型均继承自Timeline类。所以所有的动画都是专用类型的时间线。Timeline定义一个时间段,通过Timeline的相关属性,可以指定时间线的计时方式,三个经常使用的计时属性分别为Duration,AutoReverse,和RepeatBehavior。<Border Width="400" BorderBrush="Black"> <Rectangle Fill="Blue" Width="50" Height="50" HorizontalAlignment="Left"> <Rectangle.RenderTransform> <TranslateTransform x:Name="MyAnimatedTranslateTransform" X="0" Y="0" /> </Rectangle.RenderTransform> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.MouseLeftButtonDown"> <BeginStoryboard> <Storyboard> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="MyAnimatedTranslateTransform" Storyboard.TargetProperty="X" Duration="0:0:10"> <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0" /> <LinearDoubleKeyFrame Value="350" KeyTime="0:0:2" /> <LinearDoubleKeyFrame Value="50" KeyTime="0:0:7" /> <LinearDoubleKeyFrame Value="200" KeyTime="0:0:8" /> </DoubleAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </Border>关键帧动画类位于System.Windows.Media.Animation命名空间,且都遵循“<类型>AnimationUsingKeyFrames”格式的命名约定。关键帧动画支持三种不同的插值类型,因此关键帧定义格式遵循如下规则“<InterpolationMethod><Type>KeyFrame”,其中 <InterpolationMethod> 是关键帧使用的内插方法,<Type> 是类进行动画处理的值的类型。 例如,可以针对 DoubleAnimationUsingKeyFrames 使用三种关键帧类型:DiscreteDoubleKeyFrame(离散内插关键帧)、LinearDoubleKeyFrame(线性内插关键帧) 和 SplineDoubleKeyFrame(曲线内插关键帧)。
MatrixAnimationUsingPath 从其 PathGeometry 生成 Matrix 值。与 MatrixTransform 一起使用时,MatrixAnimationUsingPath 可以沿路径移动对象。如果将 MatrixAnimationUsingPath 的 DoesRotateWithTangent 属性设置为 true,它还会沿路径曲线旋转对象。
PointAnimationUsingPath 从其 PathGeometry 的 x 和 y 坐标生成 Point 值。通过使用 PointAnimationUsingPath 对采用 Point 值的属性进行动画处理,可以沿路径移动对象。PointAnimationUsingPath 不能旋转对象。
<Window x:Class="WpfApp5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp5" xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Canvas Width="400" Height="400"> <Button MinWidth="100" Content="A Button"> <Button.RenderTransform> <MatrixTransform x:Name="ButtonMatrixTransform"> <MatrixTransform.Matrix > <Matrix /> </MatrixTransform.Matrix> </MatrixTransform> </Button.RenderTransform> <Button.Triggers> <EventTrigger RoutedEvent="Button.Loaded"> <BeginStoryboard> <Storyboard> <MatrixAnimationUsingPath Storyboard.TargetName="ButtonMatrixTransform" Storyboard.TargetProperty="Matrix" DoesRotateWithTangent="True" Duration="0:0:5" RepeatBehavior="Forever" > <MatrixAnimationUsingPath.PathGeometry> <PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" PresentationOptions:Freeze="True" /> </MatrixAnimationUsingPath.PathGeometry> </MatrixAnimationUsingPath> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> </Button> </Canvas> </Window>