我想为三个帧的不透明度设置动画,并且它们的不透明度值的时间轴应该在图像中看起来像 . 动画应该是永远的,但我也想在每个周期之间延迟 . 我的意思是不透明度应该保持在指定时间内的最后一个值“1”,然后时间线应该再次重复 . 我尝试使用带有三个双动画的故事板,但我无法弄清楚每个周期之间的延迟和每个动画之间的延迟 . 设置每个动画的开始时间对我来说不起作用 .
由于WPF中动画的灵活性,有很多方法可以解决这个问题 . 在这里,我介绍最直观的解决方案 . 从您的图像中,您必须使用某种离散的KeyFrame . 在这种情况下,我们需要3 DoubleAnimationUsingKeyFrames ,每个应该有2 DiscreteDoubleKeyFrame . 所有这些关键帧应该是相同的 . 我们只需要适当地设置 BeginTime 的 DoubleAnimationUsingKeyFrames . 我们不需要设置 Duration ,它将根据 KeyTime ( DiscreteDoubleKeyFrame )和 BeginTime ( DoubleAnimationUsingKeyFrames )结合自动延迟 . 但是,如果您希望在循环之间有一些延迟,我们必须将 Duration 设置为最后一个 DoubleAnimationUsingKeyFrames .
DoubleAnimationUsingKeyFrames
DiscreteDoubleKeyFrame
BeginTime
Duration
KeyTime
这是一个简单的例子:
<StackPanel> <StackPanel.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard> <Storyboard Storyboard.TargetProperty="Opacity" RepeatBehavior="Forever"> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="i1" BeginTime="0:0:0"> <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/> <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="1"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="i2" BeginTime="0:0:1"> <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/> <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="1"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="i3" BeginTime="0:0:2" Duration="0:0:3"> <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/> <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="1"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </StackPanel.Triggers> <TextBlock Name="i1">Item 1</TextBlock> <TextBlock Name="i2">Item 2</TextBlock> <TextBlock Name="i3">Item 3</TextBlock> </StackPanel>
为简单起见,我在此代码中使用了 TextBlock . 在这里你可以看到 0 不透明度(对于每个 TextBlock )的时间是1秒 . 从那里你可以为每个 DoubleAnimationUsingKeyFrames 正确派生 BeginTime . 最后一个具有 Duration 的 3 秒,这意味着延迟大约是 2 秒(最后一个动画的1秒从总数3中扣除) .
TextBlock
0
3
2
1 回答
由于WPF中动画的灵活性,有很多方法可以解决这个问题 . 在这里,我介绍最直观的解决方案 . 从您的图像中,您必须使用某种离散的KeyFrame . 在这种情况下,我们需要3
DoubleAnimationUsingKeyFrames
,每个应该有2DiscreteDoubleKeyFrame
. 所有这些关键帧应该是相同的 . 我们只需要适当地设置BeginTime
的DoubleAnimationUsingKeyFrames
. 我们不需要设置Duration
,它将根据KeyTime
(DiscreteDoubleKeyFrame
)和BeginTime
(DoubleAnimationUsingKeyFrames
)结合自动延迟 . 但是,如果您希望在循环之间有一些延迟,我们必须将Duration
设置为最后一个DoubleAnimationUsingKeyFrames
.这是一个简单的例子:
为简单起见,我在此代码中使用了
TextBlock
. 在这里你可以看到0
不透明度(对于每个TextBlock
)的时间是1秒 . 从那里你可以为每个DoubleAnimationUsingKeyFrames
正确派生BeginTime
. 最后一个具有Duration
的3
秒,这意味着延迟大约是2
秒(最后一个动画的1秒从总数3中扣除) .