WPF动画和在序列图像的变化不透明度(WPF Animate and change Opacity

2019-10-16 21:26发布

我有三张图片,其中两个图像,动画如下和第三图像应该闪烁:

<Window.Resources>
    <Storyboard x:Key="AnimateTarget" RepeatBehavior="Forever">

        <DoubleAnimationUsingKeyFrames BeginTime="0:0:0" Duration="0:00:03" Storyboard.TargetName="img1" Storyboard.TargetProperty="Y">
            <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0" />
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="200" />
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0" />
        </DoubleAnimationUsingKeyFrames>

        <DoubleAnimationUsingKeyFrames BeginTime="0:0:2" Duration="0:00:03" Storyboard.TargetName="img2" Storyboard.TargetProperty="x">
            <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0" />
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="200" />
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0" />
        </DoubleAnimationUsingKeyFrames>

        <DoubleAnimation BeginTime="0:0:4" Duration="0:0:0.5" Storyboard.TargetProperty="(Image.Opacity)" Storyboard.TargetName="img3"  From="1.0" To="0.0" RepeatBehavior="Forever" AutoReverse="True" />

    </Storyboard>
</Window.Resources>

前两个图像,动画不错,但第三图像不闪烁,它什么也不做,只是呆在那里,你可以看到我用下面的代码闪烁第三图像:

<DoubleAnimation BeginTime="0:0:4" Duration="0:0:0.5" Storyboard.TargetProperty="(Image.Opacity)" Storyboard.TargetName="img3"  From="1.0" To="0.0" RepeatBehavior="Forever" AutoReverse="True" />

同样在此为第三图像的代码:

<Image Height="65" Name="image1" Stretch="Fill" Width="67" Source="/PicTakeWPF;component/Images/422505_110594629067212_100003500265268_37406_1212153553_n.jpg">
    <Image.RenderTransform>
         <TranslateTransform x:Name="img3"></TranslateTransform>
    </Image.RenderTransform>
</Image>

如果有人可以帮助我在此感谢我将不胜感激,

Answer 1:

尝试使用图像对象为Storyboard.TargetName的名称(图像1)不透明度动画

<DoubleAnimation BeginTime="0:0:4" Duration="0:0:0.5" Storyboard.TargetProperty="(Image.Opacity)" Storyboard.TargetName="image1"  From="1.0" To="0.0" RepeatBehavior="Forever" AutoReverse="True" />

因为透明度是图像本身的属性,其他2个动画影响图像对象的翻译,这就是为什么你使用TranslateTransform名称为这些动画。

你甚至都不需要添加

<Image.RenderTransform>
    <TranslateTransform x:Name="img3"></TranslateTransform>
</Image.RenderTransform>

第三张图片(如果你不打算在动画的翻译)。



文章来源: WPF Animate and change Opacity of Image in sequence