WPF 实现滚动字幕动画

程序要显示动态,日志之类的东西,在一个区域中显示一个文本,需要替换时,直接就换了也没啥,可是想要弄的美观一点,加个动画就美滋滋了

看看效果,这次主要是讲实现方法,手动点击按钮时执行动画:

实现思路很简单,两个TextBlock,轮流显示出来。

动画就是一个ThicknessAnimation 和一个DoubleAnimation

下面看看代码喽:

窗体全部xaml代码:

<Window x:Class="WPFDemos.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:WPFDemos"
        mc:Ignorable="d"
        x:Name="widnow"
        FontSize="30"
        WindowStartupLocation="CenterScreen"
        UseLayoutRounding="True"
        Background="#3e3e3e"
        Title="研究StoryBoard" Height="300" Width="500">
    <Grid>
        <Grid Width="300" VerticalAlignment="Center" Background="LightBlue">
            <TextBlock x:Name="t1" Text="欢迎关注 WPF UI" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" />
            <TextBlock x:Name="t2" Text="test animation" Visibility="Hidden" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" />
        </Grid>
        <Button Content="start" HorizontalAlignment="Right" VerticalAlignment="Center" Click="Button_Click" Margin="50"/>
    </Grid>
</Window>

窗体后台代码:

using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;
namespace WPFDemos
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private bool Animationed = false;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Animationed)
            {
                StartAnimationIn(t1, 0.5f);
                StartAnimationOut(t2, 0.5f);
            }
            else
            {
                StartAnimationIn(t2, 0.5f);
                StartAnimationOut(t1, 0.5f);
            }
            Animationed = !Animationed;
        }
        private async void StartAnimationIn(FrameworkElement element, float seconds)
        {
            var sb = new Storyboard();
            var offset = element.ActualHeight;
            var animation = new ThicknessAnimation
            {
                Duration = new Duration(TimeSpan.FromSeconds(seconds)),
                From = new Thickness(0, -offset, -0, offset),
                To = new Thickness(0)
            };
            Storyboard.SetTargetProperty(animation, new PropertyPath("Margin"));
            sb.Children.Add(animation);
            var fadeIn = new DoubleAnimation
            {
                Duration = new Duration(TimeSpan.FromSeconds(seconds)),
                From = 0,
                To = 1,
            };
            Storyboard.SetTargetProperty(fadeIn, new PropertyPath("Opacity"));
            sb.Children.Add(fadeIn);
            sb.Begin(element);
            element.Visibility = Visibility.Visible;
            await Task.Delay((int)(seconds * 1000));
        }
        private async void StartAnimationOut(FrameworkElement element, float seconds)
        {
            var sb = new Storyboard();
            var offset = element.ActualHeight;
            var animation = new ThicknessAnimation
            {
                Duration = new Duration(TimeSpan.FromSeconds(seconds)),
                From = new Thickness(0),
                To = new Thickness(0, offset, 0, -offset)
            };
            Storyboard.SetTargetProperty(animation, new PropertyPath("Margin"));
            sb.Children.Add(animation);
            var fadeIn = new DoubleAnimation
            {
                Duration = new Duration(TimeSpan.FromSeconds(seconds)),
                From = 1,
                To = 0,
            };
            Storyboard.SetTargetProperty(fadeIn, new PropertyPath("Opacity"));
            sb.Children.Add(fadeIn);
            sb.Begin(element);
            await Task.Delay((int)(seconds * 1000));
            element.Visibility = Visibility.Hidden;
        }
    }
}

效果图:

d9f4b8921d00f2fc5b3c56f503ee806e.png

如果喜欢,点个赞呗~

-----------------------------------

公众号【Csharp编程大全】,需要进技术群交流的,请添加小编mm1552923!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值