程序要显示动态,日志之类的东西,在一个区域中显示一个文本,需要替换时,直接就换了也没啥,可是想要弄的美观一点,加个动画就美滋滋了
这次主要是讲实现方法,手动点击按钮时执行动画
实现思路很简单,两个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;
}
}
}
效果图:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容