Easing Animation(缓动动画)

 

 1 namespace Easing2
2 {
3
4
5 //EasingFunctionBase 为所有缓动函数提供基类
6 class CustomPowerEase : EasingFunctionBase
7 {
8
9 // Using a DependencyProperty as the backing store for Power.
10 // This enables animation, styling, binding, etc...
11
12 // UIPropertyMetadata(object DefaultValue)用属性的指定默认值初始化 System.Windows.UIPropertyMetadata 类的新实例。
13
14 public static readonly DependencyProperty PowerProperty =
15 DependencyProperty.Register("Power", typeof(double), typeof(CustomPowerEase), new UIPropertyMetadata(6.0));
16
17 //添加属性包装器
18 public double Power
19 {
20 get { return (double)GetValue(PowerProperty); }
21 set { SetValue(PowerProperty, value); }
22 }
23
24 //System.Windows.Freezable, 定义一个对象,该对象具有可修改状态和只读(冻结)状态。
25 //派生自 System.Windows.Freezable 的类提供详细的更改通知,可以是不可变的,并且可以进行自我克隆。
26 protected override System.Windows.Freezable CreateInstanceCore()
27 {
28 return new CustomPowerEase();
29 }
30
31 protected override double EaseInCore(double normalizedTime)
32 {
33 //Math.Pow(double x,double y)返回指定数字的指定次幂
34 return Math.Pow(normalizedTime, Power);
35 }
36 }
37 }
 XAML Code
 1 <Window x:Class="Easing2.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:local="clr-namespace:Easing2"
5 Title="MainWindow" Height="350" Width="525">
6
7 <Window.Resources>
8 <Storyboard x:Key="AnimateTarget">
9 <!--Storyboard.TargetName,获取或设置要进行动画处理的对象的名称。
10 Storyboard.TargetProperty,获取或设置应进行动画处理的属性。-->
11
12 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Transform" Storyboard.TargetProperty="X">
13 <!--EasingDoubleKeyFrame类,通过它可以将缓动函数与 DoubleAnimationUsingKeyFrames 关键帧动画相关联-->
14 <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0.0">
15 <!--EasingDoubleKeyFrame,Value获取或设置关键帧的目标值。-->
16 </EasingDoubleKeyFrame>
17
18 <EasingDoubleKeyFrame KeyTime="0:0:4" Value="100.0">
19 <!--EasingDoubleKeyFrame.EasingFunction获取或设置应用于关键帧的缓动函数。-->
20 <EasingDoubleKeyFrame.EasingFunction>
21 <!--这里使用了DP Power-->
22 <local:CustomPowerEase Power="20" />
23 </EasingDoubleKeyFrame.EasingFunction>
24 </EasingDoubleKeyFrame>
25
26 </DoubleAnimationUsingKeyFrames>
27
28 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Transform" Storyboard.TargetProperty="Y">
29
30 <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0.0">
31 </EasingDoubleKeyFrame>
32
33 <EasingDoubleKeyFrame KeyTime="0:0:4" Value="100.0">
34
35 <EasingDoubleKeyFrame.EasingFunction>
36
37 <!--<PowerEase EasingMode="EaseIn" Power="5"/>-->
38 <!--PowerEase.EaseInCore,此成员重写 EasingFunctionBase.EaseInCore(Double),
39 提供缓动函数的逻辑部分,通过重写此部分可以生成自定义缓动函数的 EaseIn 模式。
40 -->
41 <local:CustomPowerEase Power="10" />
42 </EasingDoubleKeyFrame.EasingFunction>
43
44 </EasingDoubleKeyFrame>
45 </DoubleAnimationUsingKeyFrames>
46
47 </Storyboard>
48 </Window.Resources>
49
50 <Grid>
51 <Rectangle Width="20" Height="20" Fill="Red" RenderTransformOrigin="0.5, 0.5">
52 <Rectangle.RenderTransform>
53 <TranslateTransform x:Name="Transform" />
54 </Rectangle.RenderTransform>
55 </Rectangle>
56
57 <Button Width="100" Height="40" Content="Start Animation" Click="Button_Click"
58 HorizontalAlignment="Center"
59 VerticalAlignment="Bottom" />
60 </Grid>
61 </Window>

 

1 private void Button_Click(object sender, RoutedEventArgs e)
2 {
3 ((Storyboard)this.Resources["AnimateTarget"]).Begin();
4 }





转载于:https://www.cnblogs.com/January/archive/2012/04/06/2434778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值