首页 文章

在WPF中创建'wipe'动画

提问于
浏览
2

我正在寻找使用WPF在Microsoft PowerPoint中创建类似于“擦除”动画的动画 .

简而言之,我希望图像在1秒钟内从左向右淡入 .

这是我到目前为止的XAML,它同时淡化了所有图像:

<Window x:Class="WpfApplication2.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:WpfApplication2"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Background="Purple" Loaded="Window_Loaded">
    <Window.Resources>
        <Storyboard x:Key="fade">
            <DoubleAnimation From="0" To="1" Duration="0:0:1"
                             Storyboard.TargetName="logo"
                             Storyboard.TargetProperty="Opacity"/>

        </Storyboard>
    </Window.Resources>
    <Grid>
        <Image Source="image.png" x:Name="logo"/>
    </Grid>
</Window>

在后面的代码中,我只是在窗口加载时播放动画:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    BeginStoryboard((Storyboard)FindResource("fade"));
}

我需要添加到Storyboard标签以使图像淡入淡出类似于PowerPoint上的擦除动画?

1 回答

  • 1

    this博客文章中找到答案 . 我需要实现我需要的效果的代码与帖子中的代码相同,但我不需要第一个图像 . (UFO.jpg)

    <Window x:Class="LearnWPF.WipeEffect.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="LearnWPF.WipeEffect" Height="500" Width="700" Name="mainWindow">
        <Grid>
          <!--First image not needed.-->
          <!--<Image Source="Images/UFO.jpg" />-->
          <Image Source="Images/ladder.jpg">
            <Image.OpacityMask>
              <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                  <GradientStop Offset="0" Color="Black" x:Name="BlackStop"/>
                  <GradientStop Offset="0" Color="Transparent" x:Name="TransparentStop"/>
              </LinearGradientBrush>
            </Image.OpacityMask>
          </Image> 
        </Grid>
      <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
          <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard>
                <!--Duration changed to half a second as this is what I need. -->
                <DoubleAnimation Storyboard.TargetName="TransparentStop"
                                 Storyboard.TargetProperty="Offset" By="1"  Duration="0:0:0.5"   />
                <DoubleAnimation Storyboard.TargetName="BlackStop"
                                 Storyboard.TargetProperty="Offset" By="1" Duration="0:0:0.5"
                                 BeginTime="0:0:0.05" />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger.Actions>
        </EventTrigger>
      </Window.Triggers>
      </Window>
    

相关问题