优化WPF故事板动画

g52tjvyc  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(109)

我有简单的动画移动图像内的页面和反映它从边界像DVD标志动画。但是这个动画消耗了我视频卡大约20%的电量(我有1660 TI和i7第10代)。如果我在较弱的硬件上启动它,它有很多滞后。如何优化它?我的XAML:

<Page.Triggers>

    <EventTrigger RoutedEvent="Loaded">
        <EventTrigger.Actions>
            
            <BeginStoryboard>
                <Storyboard TargetName="ImagePassCard" >
                    <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)"
                                     AutoReverse="True"
                                     From="0" To="{Binding ElementName=CanvasMain, Path=ActualWidth, Converter={StaticResource SubtractValueConverter}, ConverterParameter=400}" 
                                     Duration="0:0:10"
                                     RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>

            <BeginStoryboard>
                <Storyboard TargetName="ImagePassCard">
                    <DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)" 
                                     AutoReverse="True"
                                     From="0" To="{Binding ElementName=CanvasMain, Path=ActualHeight, Converter={StaticResource SubtractValueConverter}, ConverterParameter=250}"
                                     Duration="0:0:15"
                                     RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>

        </EventTrigger.Actions>
    </EventTrigger>

</Page.Triggers>

<Canvas x:Name="CanvasMain">

    <Image x:Name="ImagePassCard" 
           Source="{Binding Source={x:Static res:Resources.PassCard}, Converter={StaticResource BitmapToImageConverter}}"
           Height="250"
           Width="400"/>

</Canvas>

我试着移动一个矢量XAML图像和这个位图-它是相同的结果。

yquaqz18

yquaqz181#

我不确定这是否解决了性能问题,但您可以尝试修改TranslateTransformXY值,而不是Canvas.Left附加值。
就像这样:

<Image x:Name="ImagePassCard" 
       Source="{Binding Source={x:Static res:Resources.PassCard}, Converter={StaticResource BitmapToImageConverter}}"
       Height="250"
       Width="400">
       <Image.RenderTransform>
            <TransformGroup>
                <TranslateTransform x:Name="AnimatedTranslateTransform" X="0" Y="0" />
             </TransformGroup>
        </Image.RenderTransform>
            <Image.Triggers>
                <EventTrigger RoutedEvent="<YourEvent>">
                    <BeginStoryboard>
                        <Storyboard>
                           <DoubleAnimation Storyboard.TargetProperty="AnimatedTranslateTransform"
                                            Storyboard.TargetProperty="X"
                                            AutoReverse="True"
                                            From="0" To="{Binding ElementName=CanvasMain, Path=ActualWidth, Converter={StaticResource SubtractValueConverter}, ConverterParameter=400}" 
                                            Duration="0:0:10"
                                            RepeatBehavior="Forever"/>

                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Image.Triggers>
  </Image>

相关问题