闪烁动画 WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4954254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Blinking animation WPF
提问by Sangeetha
I have this animation with me, a sort of blinking animation, such that when the button is clicked, the rectangle "blinks". I've written a code for the animation, just wanted to know if there is a better way to achieve this animation. Any suggestions?
我有这个动画,一种闪烁的动画,这样当按钮被点击时,矩形“闪烁”。我已经为动画编写了代码,只是想知道是否有更好的方法来实现这个动画。有什么建议?
Code is as below:
代码如下:
<Window.Resources>
<Storyboard x:Key="OnClick1">
<ObjectAnimationUsingKeyFrames Duration="0:0:10" Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="rectangle">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="{x:Static Visibility.Visible}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{x:Static Visibility.Collapsed}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.3" Value="{x:Static Visibility.Visible}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.4" Value="{x:Static Visibility.Collapsed}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Visible}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.6" Value="{x:Static Visibility.Collapsed}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.7" Value="{x:Static Visibility.Visible}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.8" Value="{x:Static Visibility.Collapsed}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.9" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
<BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
</EventTrigger>
</Window.Triggers>
<Grid x:Name="LayoutRoot">
<Rectangle x:Name="rectangle" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="35" Margin="129,166,0,0" Stroke="Black" VerticalAlignment="Top" Width="73"/>
<Button x:Name="button" Content="Button" Margin="272,158,263,0" Height="37" VerticalAlignment="Top"/>
</Grid>
回答by Pavlo Glazkov
Instead of ObjectAnimationUsingKeyFrames
animation, you can use simple DoubleAnimation
on the Opacity
property of your rectangle:
ObjectAnimationUsingKeyFrames
您可以DoubleAnimation
在Opacity
矩形的属性上使用 simple代替动画:
<Storyboard x:Key="OnClick1">
<DoubleAnimation Storyboard.TargetName="rectangle"
Storyboard.TargetProperty="Opacity"
From="0"
To="1"
RepeatBehavior="10x"
AutoReverse="True"
Duration="0:0:0.1"/>
</Storyboard>
回答by EdwardM
I know this is an old thread, but to add-on to Pavlo's answer, which helped me and is correct. I wanted more of an actual flicker effect, than a quick "fade-in fade-out". I used his animation code and modified it a bit:
我知道这是一个旧线程,但是要添加到 Pavlo 的答案中,这对我有帮助并且是正确的。我想要更多的实际闪烁效果,而不是快速的“淡入淡出”。我使用了他的动画代码并对其进行了一些修改:
In your resources:
在您的资源中:
<!-- Animation to flicker, like a cursor when typing -->
<Storyboard x:Key="AnimateFlicker" RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetProperty="Opacity"
From="0"
To="1"
AutoReverse="True"
BeginTime="0:0:1"
Duration="0:0:0.08" />
<DoubleAnimation Storyboard.TargetProperty="Opacity"
From="1"
To="1"
AutoReverse="True"
Duration="0:0:0.4" />
<DoubleAnimation Storyboard.TargetProperty="Opacity"
From="1"
To="0"
AutoReverse="True"
Duration="0:0:0.08" />
</Storyboard>
In your XAML:
在您的 XAML 中:
<TextBlock Text="Flicker Me" FontSize="14" Margin="0">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard Storyboard="{StaticResource AnimateFlicker}" />
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
回答by Developer
Here is C# code version for someone who need it...
这是为需要它的人提供的 C# 代码版本...
if (IsImageBlinking)
{
DoubleAnimation da = new DoubleAnimation();
da.From = 1.0;
da.To = 0.0;
da.RepeatBehavior = RepeatBehavior.Forever;
da.AutoReverse = true;
sb.Children.Add(da);
Storyboard.SetTargetProperty(da, new PropertyPath("(Image.Opacity)"));
Storyboard.SetTarget(da, image1);
sb.Begin();
}
From other hand there you can implement blinking for any control like this.
从另一方面来说,您可以为这样的任何控件实现闪烁。
<UserControl.Resources>
<Thickness x:Key="ControlMargin">0 5 0 0</Thickness>
<Storyboard x:Key="AlertArea" >
<DoubleAnimation Storyboard.TargetName="gdPersonData"
Storyboard.TargetProperty="Opacity"
From="0"
To="1"
RepeatBehavior="3x"
AutoReverse="True"
Duration="0:0:0.1"/>
</Storyboard>
<Storyboard x:Key="AlertArea2" >
<DoubleAnimation Storyboard.TargetName="gdPersonData"
Storyboard.TargetProperty="Opacity"
From="1"
To="0"
RepeatBehavior="1x"
AutoReverse="True"
Duration="0:0:0.1"/>
</Storyboard>
</UserControl.Resources>
AlertAreais to generate blinking 3 times and when it is finished we have to restore Opacity
using AlertArea2.
AlertArea将生成闪烁 3 次,完成后我们必须Opacity
使用AlertArea2进行恢复。
In the constructor of UserControl/Window
在构造函数中 UserControl/Window
..
Storyboard sb = this.FindResource("AlertArea") as Storyboard;
sb.Completed += Sb_Completed;
..
private void Sb_Completed(object sender, EventArgs e)
{
Storyboard sb2 = this.FindResource("AlertArea2") as Storyboard;
sb2.Begin();
}
In the place you need to start blinking do this
在你需要开始眨眼的地方做这个
Dispatcher.BeginInvoke((Action)(() =>
{
Storyboard sb = this.FindResource("AlertArea") as Storyboard;
sb.Begin();
}));