MSDN 教程短片 WPF 15(DoubleAnimation)

DoubleAnimation

接着上一篇使用的是XAML来实现动画的效果,今天我们用代码来实现下:

我们在窗体上放一个Rectangle,对这个Rectangle实现宽度的变化。当我们按下按钮来触发它的宽度变化。

具体代码如下:

  private void button1_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 0;
            doubleAnimation.To = 60;
            //TimeSpan时间的绝对值
            doubleAnimation.Duration =new Duration(new TimeSpan(0,0,0,3));
            //无限次重复
            doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
            //按相反的顺序播放
            doubleAnimation.AutoReverse = true;
            //启动动画,并把变化赋给宽度
            rectangle1.BeginAnimation(Rectangle.WidthProperty,doubleAnimation);
            
        }

我们在加点颜色的变化如何。

在XAML中我们先要给个SolidColorBrush的Name。

<Rectangle.Fill>
       <SolidColorBrush x:Name="solid1" Color="Green"/>
</Rectangle.Fill>

然后再按钮代码中添加:

  ColorAnimation colorAnimation=new ColorAnimation(
               Color.FromRgb(255,0,0),Color.FromRgb(0,0,255),
               new Duration(new TimeSpan(0,0,3)));
            colorAnimation.RepeatBehavior = RepeatBehavior.Forever;
            colorAnimation.AutoReverse = true;
            solid1.BeginAnimation(SolidColorBrush.ColorProperty,colorAnimation);

效果图:

开始前

image

开始后的某一秒:

image

转载于:https://www.cnblogs.com/dingli/archive/2011/01/10/1931601.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,对于 WPF 中的 DoubleAnimation 卡顿问题,可能有几种可能的原因。以下是一些常见的解决方法: 1. 减少动画的帧率:可以尝试将动画的帧率降低,这样可以减少动画的计算量,从而减轻卡顿现象。您可以尝试将 Duration 的值增加,或者将 RepeatBehavior 的值减小。 2. 使用缓存模式:可以通过设置缓存模式来提高性能。将 UIElement.CacheMode 属性设置为 BitmapCache 可以在动画过程中缓存渲染结果,从而减少计算量。 3. 使用硬件加速:启用硬件加速可以提高 WPF 应用程序的性能。可以通过在应用程序的 App.xaml 文件中添加以下代码来启用硬件加速: ```xaml <Application ...> <Application.Resources> <ResourceDictionary> <Style TargetType="FrameworkElement"> <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality"/> <Setter Property="RenderOptions.EdgeMode" Value="Aliased"/> <Setter Property="RenderOptions.ClearTypeHint" Value="Enabled"/> <Setter Property="SnapsToDevicePixels" Value="True"/> </Style> </ResourceDictionary> </Application.Resources> </Application> ``` 4. 避免过多的重绘:可以通过减少 UIElement 的重绘次数来提高性能。可以尝试使用 VisualBrush 代替动画,或者使用 RenderTransform 替代每帧的重新布局。 希望这些解决方法能帮助您解决 WPF DoubleAnimation 卡顿的问题。如果问题仍然存在,请提供更多的细节,以便我可以给出更具体的建议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值