前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF 实现滚动字幕动画

WPF 实现滚动字幕动画

作者头像
zls365
发布2021-04-23 12:21:18
1.8K0
发布2021-04-23 12:21:18
举报
文章被收录于专栏:CSharp编程大全

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

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

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

动画就是一个ThicknessAnimation 和一个DoubleAnimation

下面看看代码喽:

窗体全部xaml代码:

代码语言:javascript
复制
<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>

窗体后台代码:

代码语言:javascript
复制
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;
        }
    }
}

效果图:

如果喜欢,点个赞呗~

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档