WPF 动画效果

本文介绍了一个使用WPF实现的动画实例,包括使标签内容闪烁显示并循环,以及平移动画的实现方式。通过XAML和C#代码展示了如何设置关键帧动画来控制UI元素的可见性和位置变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线性插值动画、关键帧动画、路径动画

 

1. (Visibility)闪烁三下,停下两秒,循环:

XAML:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label Name="WarningShineLabel" Content="充电" Background="DarkRed"></Label>
    </Grid>

CS

     private void SetVisibilityShine()
        {
            ObjectAnimationUsingKeyFrames okf = new ObjectAnimationUsingKeyFrames();
            okf.Duration = new TimeSpan(0, 0, 0, 0, 4500);
            okf.RepeatBehavior = RepeatBehavior.Forever;
            okf.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Hidden, new TimeSpan(0, 0, 0)));
            okf.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Visible, new TimeSpan(0, 0, 0, 0, 500)));
            okf.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Hidden, new TimeSpan(0, 0, 0, 0, 1000)));
            okf.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Visible, new TimeSpan(0, 0, 0, 0, 1500)));
            okf.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Hidden, new TimeSpan(0, 0, 0, 0, 2000)));
            okf.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Visible, new TimeSpan(0, 0, 0, 0, 2500)));
            okf.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Hidden, new TimeSpan(0, 0, 0, 0, 4500)));

            WarningShineLabel.BeginAnimation(Label.VisibilityProperty, okf);
        }

2. 平移:

XAML

       <Label  Name="AirOutAnimation">
                <Label.Content>
                    <Image Source="../Icons/flow.png"></Image> <!--一张图片-->
                </Label.Content>
            </Label>

CS

        ThicknessAnimation ta = new ThicknessAnimation();
            ta.From = new Thickness(60, 0, 0, 0);
            ta.To = new Thickness(0, 0, 0, 0);
            ta.Duration = TimeSpan.FromSeconds(1.5);
            ta.RepeatBehavior = RepeatBehavior.Forever;
            AirOutAnimation.BeginAnimation(Label.MarginProperty, ta);

 

转载于:https://www.cnblogs.com/pangkang/p/6251594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值